当前位置:网站首页>Redis的简单使用
Redis的简单使用
2022-07-31 09:55:00 【The_theme】
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>xxx</version>
</dependency>
- 配置yml,其中jedis是同步,lettuce是异步
spring:
redis:
host: 101.43.170.12
port: 6379
# 连接超时时间(毫秒)
connect-timeout: 30000
lettuce:
pool:
# 连接池最大阻塞等待时间(负数表示没有限制)
max-wait: -1
# 连接池最大连接数(负数表示没有限制)
max-active: 8
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
- 基本使用方法
Redis中opsForValue()方法的使用介绍:
在使用之前要注入Bean
@Autowired
private StringRedisTemplate template;
1、set(K key, V value)
新增一个字符串类型的值,key是键,value是值。
redisTemplate.opsForValue().set("key","value");
2、get(Object key)
获取key键对应的值。
redisTemplate.opsForValue().get("key")
3、append(K key, String value)
在原有的值基础上新增字符串到末尾。
redisTemplate.opsForValue().append("key", "appendValue");
4、get(K key, long start, long end)
截取key键对应值得字符串,从开始下标位置开始到结束下标的位置(包含结束下标)的字符串。
SredisTemplate.opsForValue().get("key", 0, 10);
5、getAndSet(K key, V value)
获取原来key键对应的值并重新赋新值。
redisTemplate.opsForValue().getAndSet("key", "newValue");
6、setBit(K key, long offset, boolean value)
key键对应的值value对应的ascii码,在offset的位置(从左向右数)变为value。
redisTemplate.opsForValue().setBit("key",1,false);
7、getBit(K key, long offset)
判断指定的位置ASCII码的bit位是否为1。
8、size(K key)
获取指定字符串的长度
redisTemplate.opsForValue().size("key");
9、increment(K key, double delta)
以增量的方式将double值存储在变量中。(每次调用key对应的值+5)
redisTemplate.opsForValue().increment("doubleKey",5);
10、increment(K key, long delta)
以增量的方式将long值存储在变量中。(每次调用key对应的值+6)
可以用来对用户每日某种行为的数量进行限制
redisTemplate.opsForValue().increment("longKey",6);
11、setIfAbsent(K key, V value)
如果键不存在则新增,存在则不改变已经有的值。
redisTemplate.opsForValue().setIfAbsent("absentKey","fff");
12、set(K key, V value, long timeout, TimeUnit unit)
设置变量值的过期时间。
redisTemplate.opsForValue().set("timeOutKey", "timeOut", 5, TimeUnit.SECONDS);
13、set(K key, V value, long offset)
覆盖从指定位置开始的值。
redisTemplate.opsForValue().set("key","value",1);
14、multiSet(Map<? extends K,? extends V> map)
设置map集合到redis。
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
redisTemplate.opsForValue().multiSet(valueMap);
15、multiGet(Collection<K> keys)
根据集合取出对应的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("通过multiGet(Collection<K> keys)方法获取map值:" + value);
}
16、multiSetIfAbsent(Map<? extends K,? extends V> map)
如果对应的map集合名称不存在,则添加,如果存在则不做修改。
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
redisTemplate.opsForValue().multiSetIfAbsent(valueMap);
边栏推荐
- Implement a thread pool
- The fifth chapter
- Are postgresql range queries faster than index queries?
- 小程序如何使用订阅消息(PHP代码+小程序js代码)
- 第二十四课、二十五课,高级光照(blinn),Gamma矫正
- NowCoderTOP23-27 Binary tree traversal - continuous update ing
- loadrunner录制问题
- Scala基础【seq、set、map、元组、WordCount、队列、并行】
- Source code analysis of GZIPInputStream class
- 自定义v-drag指令(横向拖拽滚动)
猜你喜欢
PyQt5快速开发与实战 9.4 Matplotlib在PyQt中的应用
Source code analysis of GZIPInputStream class
Implement a thread pool
djangoWeb应用框架+MySQL数据4
Open Kylin openKylin automation developer platform officially released
NowCoderTOP17-22 二分查找/排序——持续更新ing
开放麒麟 openKylin 自动化开发者平台正式发布
第五章
js department budget and expenditure radar chart
js部门预算和支出雷达图
随机推荐
如何将虚拟机上的文件复制到主机上
Kotlin—基本语法 (四)
Qt compile error: C2228: '.key' must have class/struct/union on the left
生成随机数
数字加分隔符
作为面试官,关于线程池的问题我一般这样套路...
Build finished with errors/Executable Not Found
js以变量为键
loadrunner脚本--添加检查点
js implements the 2020 New Year's Day countdown bulletin board
[NLP] Interpretation of Transformer Theory
Solve rpc error: code = Unimplemented desc = method CheckLicense not implemented
Scala basics [seq, set, map, tuple, WordCount, queue, parallel]
centos7安装mysql5.7
Implement a thread pool
NowCoderTOP23-27二叉树遍历——持续更新ing
自定义v-drag指令(横向拖拽滚动)
【NLP】Transformer理论解读
项目管理工具之燃尽图:动态考核团队工作能力
如何判断自己是否适合IT行业?方法很简单