stringRedisTemplate.opsForValue().setIfAbsent
时间: 2024-05-17 09:18:59 浏览: 141
stringRedisTemplate.opsForValue().setIfAbsent方法是RedisTemplate中的一个操作,用于在键不存在的情况下设置键的值。具体使用方式如下:
```java
boolean lock = stringRedisTemplate.opsForValue().setIfAbsent("testKey", "value");
```
该方法会将键"testKey"的值设为"value",并返回一个布尔值表示操作是否成功。如果键"testKey"已经存在,则该方法不会进行任何操作并返回false;如果键"testKey"不存在,则该方法会将键的值设为"value"并返回true。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [RedisTemplate opsForValue()的 setIfAbsent() 和 getAndSet()](https://blog.csdn.net/weixin_44163891/article/details/124388049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [stringRedisTemplate之opsForValue使用说明](https://blog.csdn.net/qq_43409811/article/details/108876910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文