当前位置:网站首页>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 .
边栏推荐
- Rustdesk builds its own remote desktop relay server
- Download, install and use NVM of node, and related use of node and NRM
- 实践分享:如何安全快速地从 Centos迁移到openEuler
- C Advanced - data storage (Part 1)
- B站刘二大人-多元逻辑回归 Lecture 7
- Promise summary
- How to download GB files from Google cloud hard disk
- Embedded interview questions (I: process and thread)
- Sword finger offer II 039 Maximum rectangular area of histogram
- 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
猜你喜欢
应用安全系列之三十七:日志注入
Vulhub vulnerability recurrence 71_ Unomi
First knowledge database
[SQL Server Express Way] - authentification et création et gestion de comptes utilisateurs
P2802 回家
What is independent IP and how about independent IP host?
P2802 go home
H3C防火墙RBM+VRRP 组网配置
Station B, Master Liu Er - back propagation
C Advanced - data storage (Part 1)
随机推荐
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
[cloud native] 3.1 kubernetes platform installation kubespher
CoDeSys note 2: set coil and reset coil
What impact will frequent job hopping have on your career?
进程和线程
[Jiudu OJ 08] simple search x
01. Project introduction of blog development project
How to download GB files from Google cloud hard disk
Installation de la Bibliothèque de processus PDK - csmc
RustDesk 搭建一个自己的远程桌面中继服务器
Classes and objects (I) detailed explanation of this pointer
Deep learning -yolov5 introduction to actual combat click data set training
【华为机试真题详解】检查是否存在满足条件的数字组合
HAC cluster modifying administrator user password
【华为机试真题详解】统计射击比赛成绩
JS array list actual use summary
Problems encountered in installing mysql8 on MAC
Go language -- language constants
Clear floating mode
04. 项目博客之日志