SpringBoot项目中,Redis的初次使用


1.引入Redis依赖包,在application.yml中配置redis

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
Spring:
  redis:
    host: 192.168.1.105
    port: 6379
    password: 

 

2.引入Redis模板,这里我只使用了,StringRedisTemplate

    @Autowired
    private StringRedisTemplate redisTemplate;

 

3.数据存入Redis中

        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPORE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS);
redisTemplate.opsForValue().set()方法中
第一个参数:存放的数据的名称String.format(RedisConstant.TOKEN_PREFIX, token)
第二个参数:存放的内容:openid
第三个参数:存放的时间:expire
第四个参数:存放的格式:TimeUnit.SECONDS

4.在Redis中查询内容

 

 

 

5.注销Redis中内容

redisTemplate.opsForValue().getOperations().delete((String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue())));
redisTemplate.opsForValue().getOperations().delete()
第一个参数:注销的内容的名称(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()))

借用了redis的桌面可视化工具方便查看数据

 

 

 







免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM