Browse Source

解决RedisTemplate.opsForValue()存取List<Object>时候的类型转换异常,改为手动序列化,使用stringRedisTemplate

dev_shibei_match
wxz 5 years ago
parent
commit
952ea2fcb3
  1. 39
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java
  2. 8
      epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/redis/RoleOpeScopeRedis.java

39
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java

@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
@ -30,6 +31,9 @@ public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 默认过期时长为24小时单位
*/
@ -171,4 +175,39 @@ public class RedisUtils {
return redisTemplate.opsForList().rightPop(key);
}
/**
* 获取String
* @param key
* @param expire
* @return
*/
public String getString(String key, long expire) {
String value = stringRedisTemplate.opsForValue().get(key);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
return value;
}
public String getString(String key) {
return getString(key, DEFAULT_EXPIRE);
}
/**
* 存储String
* @param key
* @param value
* @param expire
*/
public void setString(String key, String value, long expire) {
stringRedisTemplate.opsForValue().set(key, value);
if (expire != NOT_EXPIRE) {
expire(key, expire);
}
}
public void setString(String key, String value) {
setString(key, value, DEFAULT_EXPIRE);
}
}

8
epmet-module/gov-access/gov-access-server/src/main/java/com/epmet/redis/RoleOpeScopeRedis.java

@ -1,5 +1,7 @@
package com.epmet.redis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.dto.result.RoleOpeScopeResultDTO;
@ -36,7 +38,7 @@ public class RoleOpeScopeRedis {
*/
public void setRoleAllOpeScopes(String roleId, List<RoleOpeScopeResultDTO> scopes) {
String roleAllOpeScopesKey = RedisKeys.getRoleAllOpeScopesKey(roleId);
redisUtils.set(roleAllOpeScopesKey, scopes);
redisUtils.setString(roleAllOpeScopesKey, JSON.toJSONString(scopes));
}
/**
@ -57,8 +59,8 @@ public class RoleOpeScopeRedis {
*/
public List<RoleOpeScopeResultDTO> getRoleAllOpeScopes(String roleId) {
String roleOpeScopesKey = RedisKeys.getRoleAllOpeScopesKey(roleId);
Object o = redisUtils.get(roleOpeScopesKey);
return (List<RoleOpeScopeResultDTO>)o;
String stringValue = redisUtils.getString(roleOpeScopesKey);
return JSON.parseObject(stringValue, new TypeReference<List<RoleOpeScopeResultDTO>>(){});
}
}

Loading…
Cancel
Save