SpringBoot項目中,Redis的初次使用
阿新 • • 發佈:2019-01-28
oot framework spa ref pan expire ati nbsp 名稱
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的桌面可視化工具方便查看數據
SpringBoot項目中,Redis的初次使用