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的桌面可视化工具方便查看数据

 

 

 






转载于:https://www.cnblogs.com/xzmxddx/p/10329718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值