业务逻辑如下
- 查询缓存是否命中
- 命中
- 则返回缓存中的数据
- 未命中
- 查询数据库返回结果
- 将数据添加到缓存中
- 命中
关于SpringBoot整合Redis可以看这篇文章
https://blog.csdn.net/qq_36781505/article/details/86612988
先看业务层的逻辑
@Override
public int createGroup(String groupId, String rootId, String groupName, String password) {
//查询缓存
Object group=redisTemplate.getHash("groups",groupId);
//如果为空则查询db
if(group==null){
group = rootMapper.getGroupByGroupId(groupId);
//如果db不为空,添加到缓存
if(group!=null) {
redisTemplate.setHash("groups", ((Group) group).getGroupId(), group);
return -1;
}
}
//如果db为空,可以创建群组
if (group == null) {
Group group1 = new Group();
group1.setRoot(new Root());
group1.getRoot().setRootId(rootId);
group1.setGroupId(groupId);
group1.setGroupName(groupName);
group1.setPassword(password);
//先添加到缓存
redisTemplate.setHash("groups",group1.getGroupId(),group1);
//添加到db
rootMapper.insertGroup(group1);
return 1;
}
return -1;
}
简单封装了一下RedisTemlate
public void setHash(String name, String key, Object obj) {
HashOperations<String, String, Object> hash = redisTemplate.opsForHash();
hash.put(name, key, obj);
//设置过期时间
redisTemplate.expire(name,1000,TimeUnit.SECONDS);
log.info("put in hash " + name + " ==========> the key is " + key + " the value is " + obj.toString());
}
public Object getHash(String name, String key) {
HashOperations<String, String, Object> hash = redisTemplate.opsForHash();
Object obj = hash.get(name, key);
if(obj!=null)
log.info("get from hash " + name + " ==========> the key is " + key + " the value is "+obj.toString());
else log.info("=============> query null from hash "+name);
return obj;
}
entity
@Data
public class Group implements Serializable{
//群组id
private String groupId;
//组长的id
private Root root;
//组名
private String groupName;
//创建时间
private String date;
//密码
private String password;
}
运行结果
2019-04-13 22:04:58.002 INFO --- [http-nio-8080-exec-5] xyh.common.utils.MyRedisTemplate: =============> query null from hash groups
2019-04-13 22:04:58.003 DEBUG --- [http-nio-8080-exec-5] x.c.dao.RootMapper.getGroupByGroupId: ==> Preparing: select * from `group` where group_id=?
2019-04-13 22:04:58.003 DEBUG --- [http-nio-8080-exec-5] x.c.dao.RootMapper.getGroupByGroupId: ==> Parameters: 9(String)
2019-04-13 22:04:58.004 DEBUG --- [http-nio-8080-exec-5] x.c.dao.RootMapper.getGroupByGroupId: <== Total: 0
2019-04-13 22:04:58.007 INFO --- [http-nio-8080-exec-5] xyh.common.utils.MyRedisTemplate: put in hash groups ==========> the key is 9 the value is Group(groupId=9, root=Root(rootId=1622, rootName=null, date=null, password=null), groupName=yida, date=null, password=123)
2019-04-13 22:04:58.008 DEBUG --- [http-nio-8080-exec-5] xyh.crm.dao.RootMapper.insertGroup: ==> Preparing: INSERT INTO `group` (group_id,root_id,group_name,`PASSWORD`) VALUES (?,?,?,?);
2019-04-13 22:04:58.014 DEBUG --- [http-nio-8080-exec-5] xyh.crm.dao.RootMapper.insertGroup: ==> Parameters: 9(String), 1622(String), yida(String), 123(String)
2019-04-13 22:04:58.019 DEBUG --- [http-nio-8080-exec-5] xyh.crm.dao.RootMapper.insertGroup: <== Updates: 1