欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

小白与java和redis的故事(一)

程序员文章站 2022-03-05 09:54:53
...

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

相关标签: redis