美文网首页
2018年9月3——spring boot引入redis

2018年9月3——spring boot引入redis

作者: 兔子Tony的主人 | 来源:发表于2018-09-05 09:16 被阅读0次

    前端的工作,暂告一段落,这里开始spring boot引入redis之旅

    • 前提:
      要安装好redis,并且可以顺利访问
      用IDE创建好一个spring boot项目

    • 首先在pom文件中引入

            <!-- 整合redis -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
    • 然后再yml文件中配置redis
    spring:
        redis:
            database: 0
            host: 192.168.1.170
            port: 6379
            password: ******
            pool:
                max-idle: 8
                min-idle: 0
                max-active: 8
                max-wait: 1
            timeout: 3000
    
    • 添加一个简单的redis工具类,想要更多的方法,网上找找,有很多。
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    
    /**
     * 功能描述:redis工具类
     * 对于redisTpl.opsForValue().set(key, value)进行了一次封装,不然每次都要这样保存值
     * 而封装后只需:new RedisClient().set(key,value);
     */
    @Component
    public class Redis {
    
        @Autowired
        private StringRedisTemplate redisTpl; //jdbcTemplate    
    
         // 功能描述:设置key-value到redis中    
        public boolean set(String key ,String value){
            try{
                redisTpl.opsForValue().set(key, value);
                return true;
            }catch(Exception e){
                e.printStackTrace();
                return false;
            }    
        }        
    
         // 功能描述:通过key获取缓存里面的值
        public String get(String key){
            return redisTpl.opsForValue().get(key);
        }        
    }
    
    • 写一个测试,测试redis是否连接成功
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.util.DigestUtils;
    
    @SpringBootTest
    @RunWith(SpringJUnit4ClassRunner.class)
    public class RedisTest {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @Test
        public void save(){
            stringRedisTemplate.opsForValue().set("xudod","supperxudod");
            Assert.assertEquals("supperxudod",stringRedisTemplate.opsForValue().get("xudod"));
        }
    }
    
    • 运行该测试类,测试通过后可以用redis可视化工具RedisDesktopManager查看刚才添加的键值
      至此,redis成功引入spring boot,这篇文章就先写到这里。下边留下一个引子,记录,redis在项目中的具体使用

    spring boot项目中使用redis--2018年9月5日添加

    • 说明,前端向后端发送登录请求时,会通过lua连接redis做用户校验,那么这里设定,用户保存和更新时,会同步保存和更新用户信息到redis。这是使用spring boot使用redis的第一个场景
    /**
    这段代码是用户管理实现类中的一个添加用户的方法
    **/
    @Override
    public BaseResp<Integer> add(UserBase userBase) {
        BaseResp<Integer> baseResp = new BaseResp<>();
        try {
            userBase.setId(UUIDTool.getUUID());
            //将用户信息保存如数据库
            int insertSelective = userBaseMapper.insertSelective(userBase);
            //将用户信息写入redis
            boolean redisResult = redis.set(userBase.getAccount(), userBase.getPassword());
            if(insertSelective > 0 && redisResult == true) {
                baseResp.setCode(BaseResp.SUCCESS);
                baseResp.setData(insertSelective);
            }else{
                baseResp.setCode(BaseResp.ERROR);
                baseResp.setData(null);
                baseResp.setMessage("无异常,添加数据失败。或者用redis缓存数据失败!");
            }
        } catch (Exception e) {
            baseResp.setCode(BaseResp.ERROR);
            baseResp.setData(null);
            baseResp.setMessage("异常,添加数据失败:" + e.getMessage());
        }
        return baseResp;
    }
    

    相关文章

      网友评论

          本文标题:2018年9月3——spring boot引入redis

          本文链接:https://www.haomeiwen.com/subject/nzqbwftx.html