这是我参与11月更文挑战的第14天,活动详情查看:2021最后一次更文挑战
前言
Redis是比较著名的NoSql数据库,主要用于存放KV型数据等非关系行数据,但随着Redis的发展,它所能做的功能越来越多,能够实现的场景包括但不限于:缓存,配置,排行榜,计数,分布式锁,限流,消息队列等等,当然我们提到他最多的时候是应用在缓存场景,因为redis是为缓存而生.
Redis集成请看前置文章SpringBoot基础之集成使用Redis
如果当前小节没有定义对象,默认的注入对象为
@Autowired
private RedisTemplate<String,String> template;
@Autowired
private RedisTemplate<String,Integer> intTemplate;
基本增删改查,以String举例
单增
template.opsForValue().set("zdc","zdc");
批量增
HashMap<String, String> map = new HashMap<>();
template.opsForValue().multiSet(map);
批量插入不要一直用set
啦
管道方式批量增
HashMap<String, String> map = new HashMap<>();
template.executePipelined((RedisCallback) redisConnection ->{
for (Map.Entry<String, String> entry : map.entrySet()) {
byte[] bytes = entry.getKey().getBytes();
redisConnection.set(bytes,bytes);
}
return null;
});
管道的方式能操作多种类型,具体操作可以自测一下. 另一种写法如下
HashMap<String, String> map = new HashMap<>();
template.executePipelined(new RedisCallback(){
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
for (Map.Entry<String, String> entry : map.entrySet()) {
byte[] bytes = entry.getKey().getBytes();
connection.set(bytes,bytes);
}
return null;
}
});
不存在则增加,存在不修改
template.opsForValue().setIfAbsent("zdc","zdc");
key不存在则设置值,存在则不变
存在则修改,不存在不增加
template.opsForValue().setIfPresent("zdc","cdz");
自增自减
自增步幅为2,执行一次加2
intTemplate.opsForValue().increment("zzz",2);
自减步幅为3,执行一次减少3
intTemplate.opsForValue().decrement("zzz",3);
单查
template.opsForValue().get("zdc");
批量查
List<String> values = template.opsForValue().multiGet(new ArrayList());
删除
intTemplate.delete("zdc");
事务
开启事务并提交
List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
public List<Object> execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.opsForSet().add("zdc", "z");
return operations.exec();
}
});
开启事务multi()
提交事务exec()
回滚事务discard()
简单计数
intTemplate.opsForValue().increment("zdc_count");
并发限流
根据KEY限制2最少两秒请求一次,KEY可以使用户id,也可以使请求参数的HASH
Boolean userStatus = template.opsForValue().setIfAbsent("key", "", 2, TimeUnit.SECONDS);
if(userStatus){
//执行业务
}
实现栈功能 List
从左侧添加一个元素
template.opsForList().leftPush("zdc","UUID");
从左侧获取一个元素
String zdc = template.opsForList().leftPop("zdc");
实现队列功能 List
从左侧添加一个元素
template.opsForList().leftPush("zdc","UUID");
从右侧获取一个元素
template.opsForList().rightPop("zdc")
交集,并集,差集 Set
template.opsForSet().add("zzz","1","2","3");
template.opsForSet().add("ddd","3","4","5");
交集
Set<String> union = template.opsForSet().union("zzz", "ddd");
并集
Set<String> intersect = template.opsForSet().intersect("zzz", "ddd");
差集
Set<String> difference = template.opsForSet().difference("zzz", "ddd");
排行 ZSET
template.opsForZSet().add("zdc_score",UUID.randomUUID().toString(),(Math.random() * 50));
获取前十名
Set<String> zdc_score = template.opsForZSet().range("zdc_score", 0, 9);
结束语
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
如果您喜欢我的文章,可以[关注]+[点赞]+[评论],您的三连是我前进的动力,期待与您共同成长~
作者:ZOUZDC
链接:https://juejin.cn/post/7028963866063306760
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。