当前位置:网站首页>Redistemplate common collection instructions opsforvalue (II)
Redistemplate common collection instructions opsforvalue (II)
2022-07-06 05:45:00 【Python's path to immortality】
The basic configuration has been introduced in the previous 《RedisTemplate Instructions for common sets ( One )》 Has been introduced in , Now let's introduce it directly opsForValue() Use of methods :
1、set(K key, V value)
Add a value of string type ,key It's a key ,value Is the value .
redisTemplate.opsForValue().set("stringValue","bbb");
2、get(Object key)
obtain key The value of the key .
String stringValue = redisTemplate.opsForValue().get("stringValue")+"";
System.out.println(" adopt get(Object key) Method to get set(K key, V value) Method to add a new string value :" + stringValue);
3、append(K key, String value)
Add a new string to the end based on the original value .
redisTemplate.opsForValue().append("stringValue","aaa");
String stringValueAppend = redisTemplate.opsForValue().get("stringValue")+"";
System.out.println(" adopt append(K key, String value) Method to modify the string :"+stringValueAppend);
4、get(K key, long start, long end)
Intercept key The key corresponds to the value string , From the start subscript position to the end subscript position ( Include end subscript ) String .
String cutString = redisTemplate.opsForValue().get("stringValue",0,3);
System.out.println(" adopt get(K key, long start, long end) Method to get the intercepted string :"+cutString);
5、getAndSet(K key, V value)
Get the original key Key and assign a new value .
String oldAndNewStringValue = redisTemplate.opsForValue().getAndSet("stringValue","ccc")+"";
System.out.print(" adopt getAndSet(K key, V value) Method to get the original " + oldAndNewStringValue + ",");
String newStringValue = redisTemplate.opsForValue().get("stringValue")+"";
System.out.println(" Modified value :"+newStringValue);
6、setBit(K key, long offset, boolean value)
key The value of the key value Corresponding ascii code , stay offset The location of ( Count from left to right ) Turn into value.
redisTemplate.opsForValue().setBit("stringValue",1,false);
newStringValue = redisTemplate.opsForValue().get("stringValue")+"";
System.out.println(" adopt setBit(K key,long offset,boolean value) Method modified value :"+newStringValue);
7、getBit(K key, long offset)
Determine the specified location ASCII The code bit Whether a is 1.
boolean bitBoolean = redisTemplate.opsForValue().getBit("stringValue",1);
System.out.println(" adopt getBit(K key,long offset) Method judgment assignment bit The value of bits is :" + bitBoolean);
8、size(K key)
Gets the length of the specified string .
Long stringValueLength = redisTemplate.opsForValue().size("stringValue");
System.out.println(" adopt size(K key) Method to get the length of the string :"+stringValueLength);
9、increment(K key, double delta)
Incrementally move double Values are stored in variables .
double stringValueDouble = redisTemplate.opsForValue().increment("doubleValue",5);
System.out.println(" adopt increment(K key, double delta) Method to store... Incrementally double value :" + stringValueDouble);
10、increment(K key, long delta)
Incrementally move long Values are stored in variables .
double stringValueLong = redisTemplate.opsForValue().increment("longValue",6);
System.out.println(" adopt increment(K key, long delta) Method to store... Incrementally long value :" + stringValueLong);
11、setIfAbsent(K key, V value)
If the key does not exist, add it , Being does not change the value that is already there .
boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent("absentValue","fff");
System.out.println(" adopt setIfAbsent(K key, V value) Method to determine the value of the variable absentValue Whether there is :" + absentBoolean);
if(absentBoolean){
String absentValue = redisTemplate.opsForValue().get("absentValue")+"";
System.out.print(", non-existent , Then the new value is :"+absentValue);
boolean existBoolean = redisTemplate.opsForValue().setIfAbsent("absentValue","eee");
System.out.print(", Call again setIfAbsent(K key, V value) Judge absentValue Whether it exists and reassigns :" + existBoolean);
if(!existBoolean){
absentValue = redisTemplate.opsForValue().get("absentValue")+"";
System.out.print(" If there is , After re assignment absentValue The value of the variable is :" + absentValue);
}
}
12、set(K key, V value, long timeout, TimeUnit unit)
Set the expiration time of the variable value .
redisTemplate.opsForValue().set("timeOutValue","timeOut",5,TimeUnit.SECONDS);
String timeOutValue = redisTemplate.opsForValue().get("timeOutValue")+"";
System.out.println(" adopt set(K key, V value, long timeout, TimeUnit unit) Method to set the expiration time , Data obtained before expiration :"+timeOutValue);
Thread.sleep(5*1000);
timeOutValue = redisTemplate.opsForValue().get("timeOutValue")+"";
System.out.print(", wait for 10s later , Get the value of :"+timeOutValue);
13、set(K key, V value, long offset)
Overwrite the value starting from the specified position .
redisTemplate.opsForValue().set("absentValue","dd",1);
String overrideString = redisTemplate.opsForValue().get("absentValue")+"";
System.out.println(" adopt set(K key, V value, long offset) Method covers the value of the part :"+overrideString);
14、multiSet(Map<? extends K,? extends V> map)
Set up map Assemble to redis.
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
redisTemplate.opsForValue().multiSet(valueMap);
15、multiGet(Collection keys)
Take out the corresponding... According to the set value value .
// according to List Set to get the corresponding value value
List paraList = new ArrayList();
paraList.add("valueMap1");
paraList.add("valueMap2");
paraList.add("valueMap3");
List<String> valueList = redisTemplate.opsForValue().multiGet(paraList);
for (String value : valueList){
System.out.println(" adopt multiGet(Collection<K> keys) Method to get map value :" + value);
}
16、multiSetIfAbsent(Map<? extends K,? extends V> map)
If the corresponding map Collection name does not exist , Then add , If it exists, it will not be modified .
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
redisTemplate.opsForValue().multiSetIfAbsent(valueMap);
This is the end of the introduction opsForValue() Use of methods , The specific code will be given in the last article .
边栏推荐
- The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
- B站刘二大人-反向传播
- How to use PHP string query function
- First knowledge database
- JDBC calls the stored procedure with call and reports an error
- Station B Liu Erden softmx classifier and MNIST implementation -structure 9
- Migrate Infones to stm32
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- [imgui] unity MenuItem shortcut key
- Anti shake and throttling are easy to understand
猜你喜欢
Embedded interview questions (IV. common algorithms)
What is independent IP and how about independent IP host?
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
P2802 go home
03. Login of development blog project
Clear floating mode
华为路由器如何配置静态路由
[cloud native] 3.1 kubernetes platform installation kubespher
Vulhub vulnerability recurrence 72_ uWSGI
Station B Liu Erden - linear regression and gradient descent
随机推荐
【华为机试真题详解】检查是否存在满足条件的数字组合
[email protected] raspberry pie
First knowledge database
PDK工艺库安装-CSMC
26file filter anonymous inner class and lambda optimization
[experience] install Visio on win11
Go language -- language constants
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
Zoom through the mouse wheel
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
04. 项目博客之日志
[detailed explanation of Huawei machine test] statistics of shooting competition results
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
JS array list actual use summary
类和对象(一)this指针详解
Sword finger offer II 039 Maximum rectangular area of histogram
Clear floating mode
ARTS Week 25