具体文字说明原理看上面链接的,说的蛮好的,下面我提供完整复制即可正常执行的源码.
//pom文件新增redis坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
//application.yml配置redis相关参数
redis:
# Redis服务器地址
host: 127.0.0.1
#Redis服务器连接端口
port: 6379
#password:
# Redis数据库索引(默认为0)
database: 1
# 连接超时时间(毫秒)
timeout: 5000
jedis:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 100
# 连接池中的最小空闲连接
max-idle: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 100000
//新建redisTemplate类
package com.futuredata.cloud.cube.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
/**
* redis 配置
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Override
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
//格式化缓存key字符串
StringBuilder sb = new StringBuilder();
//追加类名
sb.append(o.getClass().getName());
//追加方法名
sb.append(method.getName());
//遍历参数并且追加
for (Object obj : objects) {
if (obj != null) {
sb.append(obj.toString());
}
}
return sb.toString();
}
};
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间120秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(86400))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
// 创建redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用Jackson2JsonRedisSerialize替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//set value serializer
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
// key采用String的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
// value序列化方式采用jackson
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hash的key也采用String的序列化方式
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// hash的value序列化方式采用jackson
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
//新建RedisCacheAspect切面类
package com.futuredata.cloud.cube.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Logger;
@Aspect
@Component
@Slf4j
public class RedisCacheAspect {
// public static final Logger log = LogUtils.getlogger();
@Qualifier("redisTemplate")
@Autowired
private RedisTemplate rt;
/**
* 方法调用前,先查询缓存。如果存在缓存,则返回缓存数据,阻止方法调用;
* 如果没有缓存,则调用业务方法,然后将结果放到缓存中
* @param jp
* @return
* @throws Throwable
*/
@Around("execution(* com.futuredata.cloud.cube.mapper.EnvironmentalProtectionFeatureMapper.listSubjectFeatureData(..))")
public Object cache(ProceedingJoinPoint jp) throws Throwable {
// 得到类名、方法名和参数
String clazzName = jp.getTarget().getClass().getName();
String methodName = jp.getSignature().getName();
Object[] args = jp.getArgs();
// 根据类名,方法名和参数生成key
String key = genKey(clazzName, methodName, args);
if (log.isDebugEnabled()) {
log.debug("生成key:{}", key);
}
// 得到被代理的方法
Method me = ((MethodSignature) jp.getSignature()).getMethod();
// 得到被代理的方法上的注解
// Class modelType = me.getAnnotation(RedisCache.class).type();
Class modelType =me.getReturnType();
// 检查redis中是否有缓存
String value = (String)rt.opsForHash().get(modelType.getName(), key);
// result是方法的最终返回结果
Object result = null;
if (null == value) {
// 缓存未命中
if (log.isDebugEnabled()) {
log.debug("缓存未命中");
}
// 调用数据库查询方法
result = jp.proceed(args);
// 序列化查询结果
String json = serialize(result);
// 序列化结果放入缓存
rt.opsForHash().put(modelType.getName(), key, json);
} else {
// 缓存命中
if (log.isDebugEnabled()) {
log.debug("缓存命中, value = {}", value);
}
// 得到被代理方法的返回值类型
Class returnType = ((MethodSignature) jp.getSignature()).getReturnType();
// 反序列化从缓存中拿到的json
result = deserialize(value, returnType);
if (log.isDebugEnabled()) {
log.debug("反序列化结果 = {}", result);
}
}
return result;
}
/**
* 在方法调用前清除缓存,然后调用业务方法
* @param jp
* @return
* @throws Throwable
*/
@Around("execution(* com.futuredata.cloud.cube.mapper.EnvironmentalProtectionFeatureMapper.insert*(..))")
public Object evictCache(ProceedingJoinPoint jp) throws Throwable {
// 得到被代理的方法
Method me = ((MethodSignature) jp.getSignature()).getMethod();
// 得到被代理的方法上的注解
Class modelType = me.getAnnotation(RedisEvict.class).type();
if (log.isDebugEnabled()) {
log.debug("清空缓存:{}", modelType.getName());
}
// 清除对应缓存
rt.delete(modelType.getName());
return jp.proceed(jp.getArgs());
}
/**
* 根据类名、方法名和参数生成key
* @param clazzName
* @param methodName
* @param args 方法参数
* @return
*/
protected String genKey(String clazzName, String methodName, Object[] args) {
StringBuilder sb = new StringBuilder(clazzName);
sb.append("-");
sb.append(methodName);
sb.append("-");
for (Object obj : args) {
sb.append(obj.toString());
sb.append("-");
}
return sb.toString();
}
protected String serialize(Object target) {
return JSON.toJSONString(target);
}
protected Object deserialize(String jsonString, Class clazz) {
// 序列化结果应该是List对象
if (clazz.isAssignableFrom(List.class)) {
return JSONArray.parseArray(jsonString);
}
// 序列化结果是普通对象
return JSON.parseObject(jsonString, clazz);
}
}