SpringBoot整合Redis在实际业务中的使用

在这里插入图片描述

业务逻辑如下

  • 查询缓存是否命中
    • 命中
      • 则返回缓存中的数据
    • 未命中
      • 查询数据库返回结果
      • 将数据添加到缓存中

关于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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值