RedisTemplate中文乱码问题

编程

1. 创建类

@Component

public class SetRedisTemplate {

@Autowired

private RedisTemplate redisTemplate;

@Autowired(required = false)

public void setRedisTemplate(RedisTemplate redisTemplate) {

RedisSerializer stringSerializer = new StringRedisSerializer();

redisTemplate.setKeySerializer(stringSerializer);

redisTemplate.setValueSerializer(stringSerializer);

redisTemplate.setHashKeySerializer(stringSerializer);

redisTemplate.setHashValueSerializer(stringSerializer);

this.redisTemplate = redisTemplate;

}

}

2. 调用可以

    @Autowired

private RedisTemplate redisTemplate;

@Autowired

private SetRedisTemplate setRedisTemplate;

setRedisTemplate.setRedisTemplate(redisTemplate); //解决中文乱码

redisTemplate.opsForValue().set(token,userId+"",60l,TimeUnit.SECONDS); //set redis 60s

Long redisTime = redisTemplate.getExpire(token,TimeUnit.SECONDS); //用rediskey 获取剩余redis时间

if (redisTime!=-2){

redisTemplate.expire(token, redisTime+30, TimeUnit.SECONDS); //使用key重新 设置有效期时间

}

 

以上是 RedisTemplate中文乱码问题 的全部内容, 来源链接: utcz.com/z/511151.html

回到顶部