RedisTemplate中定义了5种数据结构操作
redisTemplate.opsForValue(); //操作字符串
redisTemplate.opsForHash(); //操作hash
redisTemplate.opsForList(); //操作list
redisTemplate.opsForSet(); //操作set
redisTemplate.opsForZSet(); //操作有序set
StringRedisTemplate常用操作
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作
stringRedisTemplate.opsForValue().get("test")//根据key获取缓存中的val
stringRedisTemplate.boundValueOps("test").increment(1);//val +1
stringRedisTemplate.getExpire("test")//根据key获取过期时间
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位
stringRedisTemplate.delete("test");//根据key删除缓存
stringRedisTemplate.hasKey("546545");//检查key是否存在,返回boolean值
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间
stringRedisTemplate.opsForSet().isMember("red_123", "1")//根据key查看集合中是否存在指定数据
stringRedisTemplate.opsForSet().members("red_123");//根据key获取set集合
https://www.bbsmax.com/A/n2d92oD6zD/
1.向redis里存入数据和设置缓存时间
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);
2.val做-1操作
stringRedisTemplate.boundValueOps("test").increment(-1);
3.根据key获取缓存中的val
stringRedisTemplate.opsForValue().get("test");
4.val +1
stringRedisTemplate.boundValueOps("test").increment(1);
5.根据key获取过期时间
stringRedisTemplate.getExpire("test");
6.根据key获取过期时间并换算成指定单位
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS);
7.根据key删除缓存
stringRedisTemplate.delete("test");
8.检查key是否存在,返回boolean值
stringRedisTemplate.hasKey("546545");
9.向指定key中存放set集合
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");
10.设置过期时间
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);
11.根据key查看集合中是否存在指定数据
stringRedisTemplate.opsForSet().isMember("red_123", "1");
12.根据key获取set集合
stringRedisTemplate.opsForSet().members("red_123");
https://blog.csdn.net/xyjcfucdi128/article/details/84939994
StringRedisTemplate自动关闭redis连接
//注入对象
@Autowired
private StringRedisTemplate stringRedisTemplate;
#获取ValueOperations操作String数据
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
valueOperations.set("strRedis","StringRedisTemplate");
valueOperations.get("strRedis");
#设置过期时间
set("timeStep", new Date().getTime()+"", 2 ,TimeUnit.MINUTES);
#获取SetOperations操作Set数据
SetOperations set = stringRedisTemplate.opsForSet();
set.add("set1","22");
set.add("set1","33");
set.add("set1","44");
Set resultSet =stringRedisTemplate.opsForSet().members("set1");
stringRedisTemplate.opsForSet().add("set2", "1","2","3");//向指定key中存放set集合
Set resultSet1 =stringRedisTemplate.opsForSet().members("set2");
log.info("resultSet:"+resultSet);
log.info("resultSet1:"+resultSet1);
#获取ListOperations操作List数据,list可以用来实现队列。
//将数据添加到key对应的现有数据的左边
Long redisList = stringRedisTemplate.opsForList().leftPush("redisList", "3");
stringRedisTemplate.opsForList().leftPush("redisList", "4");
//将数据添加到key对应的现有数据的右边
Long size = stringRedisTemplate.opsForList().size("redisList");
//从左往右遍历
String leftPop = stringRedisTemplate.opsForList().leftPop("redisList");
//从右往左遍历
String rightPop = stringRedisTemplate.opsForList().rightPop("redisList");
//查询全部元素
List range = stringRedisTemplate.opsForList().range("redisList", 0, -1);
//查询前三个元素
List range1 = stringRedisTemplate.opsForList().range("redisList", 0, 3);
//从左往右删除list中元素A (1:从左往右 -1:从右往左 0:删除全部)
Long remove = stringRedisTemplate.opsForList().remove("key", 1, "A");
log.info("redisList----"+redisList);
log.info("size----"+size);
log.info("leftPop----"+leftPop);
log.info("rightPop----"+rightPop);
log.info("range----"+range);
log.info("range1----"+range1);
log.info("remove----"+remove);
//判断key对应的map中是否存在hash
Boolean aBoolean = stringRedisTemplate.opsForHash().hasKey("hash", "hash1");
//往key对应的map中新增(key1,value1)
stringRedisTemplate.opsForHash().put("hash", "hash1", "value1");
//获取key对应的map中hash1的值
Object o = stringRedisTemplate.opsForHash().get("hash", "hash1");
//删除key对应的map中多个子hash(可变参数)
Long delete = stringRedisTemplate.opsForHash().delete("hash", "key1", "key2", "key3");
//获取hash对应的map
Map
https://segmentfault.com/a/1190000019952021?utm_source=tag-newest