小白與java和redis的故事(一)
阿新 • • 發佈:2018-11-04
1.引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.設定redis
# application.yml
spring:
redis:
host: 101.92.112.175
port: 6379
# password:
3.儲存redis
@Autowired private StringRedisTemplate redisTemplate; @GetMapping("/login") public void test(@RequestParam(value = "openid") String openid, HttpServletResponse response, Map<String,Object> map){ //儲存方式一 (key,value) redisTemplate.opsForValue().set("syy","abc"); String token = UUID.randomUUID().toString(); Integer expire = RedisConstant.EXPIRE; //token過期時間 //儲存redis:redis的key,redis的值(openid),過期時間,時間格式(s) redisTemplate.opsForValue().set( String.format(RedisConstant.TOKEN_PREFIX,token), openid, expire, TimeUnit.SECONDS); }
4. 資料儲存到cookie中
@Autowired private StringRedisTemplate redisTemplate; @GetMapping("/login") public void test(@RequestParam(value = "openid") String openid, HttpServletResponse response, Map<String,Object> map){ //儲存方式一 (key,value) redisTemplate.opsForValue().set("syy","abc"); String token = UUID.randomUUID().toString(); Integer expire = RedisConstant.EXPIRE; //token的過期時間設定到了RedisConstant常量類中 //儲存redis:redis的key,redis的值(openid),過期時間,時間格式(s) redisTemplate.opsForValue().set( String.format(RedisConstant.TOKEN_PREFIX,token), openid, expire, TimeUnit.SECONDS); Cookie cookie = new Cookie("token",token); cookie.setPath("/"); cookie.setMaxAge(7200); //cookie過期時間s response.addCookie(cookie); }
redis資料視覺化工具
連結:https://pan.baidu.com/s/1vFPKUFJaVzxVHwVyPpuGTg 密碼:oe8r