直接淦代码
配置内容请看上一篇文章:m0_58440053的博客_Enan._优快云博客https://blog.youkuaiyun.com/m0_58440053?spm=1000.2115.3001.5343&type=blog
KeyGeneratorConfig 自定义key的创建,这个配置类可写可不写
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
@Configuration
public class KeyGeneratorConfig {
@Bean
public KeyGenerator getKeyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
return method.getName();
}
};
}
}
CacheManagerConfig 缓存配置的配置类,这个需要写一写,缓存配置类有多种实现方式的结果都是返回CacheManager对象的
import org.redisson.api.RedissonClient;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
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 java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@EnableCaching //开启注解式缓存
@Configuration
public class CacheManagerConfig {
//使用redisson对redis缓存进行配置
// @Bean
// public CacheManager getCacheManager(RedissonClient redissonClient){
// Map<String, CacheConfig> config = new HashMap<>();
// // 创建一个名称为"testMap"的缓存,过期时间ttl为24分钟,同时最长空闲时maxIdleTime为12分钟。
// config.put("RedissonCacheManager",new CacheConfig(24*60*1000, 12*60*1000));
// return new RedissonSpringCacheManager(redissonClient,config);
// }
//采用spring的缓存方式
@Bean
public CacheManager getCacheManager(@Autowired RedisConnectionFactory redisConnectionFactory){
return RedisCacheManager
.builder(redisConnectionFactory)
//设置缓存过期时间
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl((Duration.ofMinutes(5))))
.transactionAware()
.build();
}
缓存注解介绍及使用,介绍我都放代码注释了,使用注意key的命名规范及可
mapper层接口的写法
//需要自己定义,写在mapper层里面的接口方法
//#root.methodName使用方法名做键名
@Cacheable(value = "OrderMapper",key = "#root.methodName+#root.args[0]")
public Order getOne(@Param("uuid") String uuid);
service层写法,值得注意的是,修改更新缓存时,切记,注解的方法上一定要有return 返回值,否则是无法更新的
@Cacheable(value = "OrderMapper", key = "#root.methodName+#root.args[0]")
public List<OrderVO> getOrderVO(String uuid) {
QueryWrapper<Order> wrapper = new QueryWrapper<>();
wrapper.eq("mst.uuid", uuid);
redisTemplate.opsForValue().set("abc", "领导我还没吃饭了");
return mapper.getOrderVO(wrapper);
}
public Order getOne(String uuid) {
redisTemplate.opsForValue().set("qwe", "战斗");
return mapper.getOne(uuid);
}
//找到指定更新缓存的对应方法,自己手写的需要用‘’
//更新必须有返回值才可以缓存
@CachePut(value = "OrderMapper", key = "'getOne'+#result.uuid")
public Order update(String uuid, OrderUpdateDTO dto) {
Order order = mapper.selectById(uuid);
BeanUtil.copyProperties(dto, order);
mapper.updateById(order);
return order;
}
/**
* 当不设置beforeInvocation的时候,默认为false,
* 表示在方法执行之后进行删除缓存,当设置为true时候,
* 则表示缓存在方法执行之前进行删除。
* (一般不进行设置,因为在方法执行之后进行删除缓存是有需求的,
* 比如当函数执行异常例如有除数为0的时候,则表示不可以进行缓存的删除)
*/
//allEntries为true时清除所有与value对应的缓存
// @CacheEvict(value = "OrderMapper",beforeInvocation = false,allEntries = true)
//一般都还是与更新缓存一样,对应key名清除
@CacheEvict(value = "OrderMapper",key = "'getOne'+#root.args[0]")
public void delete(String uuid) {
log.info("缓存已经被清除!");
}