RedisTemplate
1.redisTemplate.opsForList()
-
leftPush和rightPush区别
leftPush()
添加到头
rightPush()
添加到尾 -
参考:https://blog.csdn.net/weixin_40991408/article/details/104367669
2.redisTemplate.opsForValue()
set(K key, V value, long timeout, TimeUnit unit)
- key值为”poetList“,以json格式存入redis,过期时间为7,计时单位为 “天”
- 若要使用JSON.toJSONString,需要加入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
redisTemplate.opsForValue().set("poetList", JSON.toJSONString(poetList), 7, TimeUnit.DAYS);
get(K key, long start, long end)
- 从列表第0个元素开始取,-1表示取到倒数第一个元素,(0,-1)即取出整个列表
String string = redisTemplate.opsForValue().get("poetList",0,-1);
3.redisTemplate.opsForHash()
get(H key, Object hashKey)
- 获取map下是否有对应的key值,如果有该则获取,没有返回null
String string = redisTemplate.opsForHash().get("studentList","Tom").toString();
put(H key, HK hashKey, HV value)
- 在map中新增key-value
- 注意:存入redis时,先转为json字符串,再以json字符串格式存进去
redisTemplate.opsForHash().put("studentList","Tom", JSON.toJSONString(studentList));
redisTemplate.expire("studentList",7,TimeUnit.DAYS);//设置过期时间
hasKey(H key, Object hashKey)
- 判断map下是否存在key值,类型为boolean型
redisTemplate.opsForHash().hasKey("studentList","Tom")
4.redisTemplate.hasKey(H key)
- 判断是否存在key值,boolean类型