当前位置:网站首页>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 .
边栏推荐
- AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
- Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
- 华为BFD的配置规范
- [force buckle]43 String multiplication
- PDK process library installation -csmc
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- 清除浮动的方式
- [machine learning notes] univariate linear regression principle, formula and code implementation
- Vulhub vulnerability recurrence 72_ uWSGI
- 进程和线程
猜你喜欢
华为BFD的配置规范
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
ArcGIS application foundation 4 thematic map making
What is independent IP and how about independent IP host?
What preparations should be made for website server migration?
02. 开发博客项目之数据存储
Construction of yolox based on paste framework
Station B, Master Liu Er - dataset and data loading
01. Project introduction of blog development project
P2802 回家
随机推荐
Embedded interview questions (IV. common algorithms)
PDK工艺库安装-CSMC
59. Spiral matrix
(column 22) typical column questions of C language: delete the specified letters in the string.
PDK工藝庫安裝-CSMC
js Array 列表 实战使用总结
Web Security (V) what is a session? Why do I need a session?
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
查詢生產訂單中某個(些)工作中心對應的標准文本碼
How to use PHP string query function
Solution of QT TCP packet sticking
[force buckle]43 String multiplication
【华为机试真题详解】统计射击比赛成绩
02. Develop data storage of blog project
29io stream, byte output stream continue write line feed
网站进行服务器迁移前应做好哪些准备?
华为路由器忘记密码怎么恢复
JDBC calls the stored procedure with call and reports an error
B站刘二大人-多元逻辑回归 Lecture 7
Problems encountered in installing mysql8 on MAC