Cache – 实现缓存
定义缓存接口
public interface Cache<K, V> {
/**
* 从缓存中取数据
*/
V getFromCache(K key);
/**
* 将缓存中数据失效
*/
void invalidFromCache(K key);
}
public interface DBDataOperation<K, V> {
/**
* 根据key 从数据库中取数据
*
*/
V getFromDB(K key);
}
定义Redis工具类JSON转换方法
public static String getValue(final String key) {
if (StringUtils.isBlank(key)) {
return null;
}
try {
return redisClient.execute(new ShardedJedisAction<String>() {
public String doAction(ShardedJedis jedis) {
return jedis.get(key);
}
});
} catch (Exception e) {
LOGGER.error("Redis取值方法getValue发生异常,异常信息为:", e);
return null;
}
}
public static <T> List<T> getValueToList(String key, Class<T> beanClass) {
String value = getValue(key);
if (StringUtils.isBlank(value)) {
return null;
}
try {
return JSON.parseArray(value, beanClass);
} catch (JSONException e) {
LOGGER.error("JSON转换发生异常,异常信息为:", e);
return null;
}
}
定义实现类