首页 > 分享 > springboot增加注解缓存@Cacheable

springboot增加注解缓存@Cacheable

业务层使用:

@Cacheable(value = "dictionary#1800", key = "#root.targetClass.simpleName +':'+ #root.methodName +':'+ #code")

public Object findByCode(String code) {

//业务

}

配置:

import org.springframework.cache.Cache;

import org.springframework.data.redis.cache.RedisCache;

import org.springframework.data.redis.cache.RedisCacheManager;

import org.springframework.data.redis.core.RedisOperations;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.ConcurrentMap;

public class MyRedisCacheManager extends RedisCacheManager {

/**

private static final String TTLSEPARATOR = "#";

private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap(16);

/**

private long defaultExpiration = 0;

public MyRedisCacheManager(RedisOperations redisOperations) {

super(redisOperations);

}

@Override

public Cache getCache(String name) {

long expiredTime = defaultExpiration;

if (name.contains(TTLSEPARATOR)) {

String[] split = name.split(TTLSEPARATOR);

String cacheName = split[0];

try {

expiredTime = Double.valueOf(split[1]).longValue();

} catch (Exception e) {

e.printStackTrace();

}

Cache cache = this.cacheMap.get(name);

if (cache != null) {

return cache;

} else {

synchronized (this.cacheMap) {

cache = this.cacheMap.get(name);

if (cache == null) {

cache = new RedisCache(cacheName, null, super.getRedisOperations(), expiredTime);

if (cache != null) {

cache = this.decorateCache(cache);

this.cacheMap.put(name, cache);

}

}

return cache;

}

}

}

return super.getCache(name);

}

}

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.cache.CacheManager;

import org.springframework.cache.annotation.CacheConfig;

import org.springframework.cache.annotation.CachingConfigurerSupport;

import org.springframework.cache.annotation.EnableCaching;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

@CacheConfig

@Configuration

@EnableCaching

public class RedisConfig extends CachingConfigurerSupport {

@Bean

public CacheManager cacheManager(RedisTemplate redisTemplate) {

redisTemplate.setKeySerializer(new StringRedisSerializer());

ObjectMapper mapper = new ObjectMapper();

mapper.findAndRegisterModules();

mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);

redisTemplate.setValueSerializer(serializer);

MyRedisCacheManager mrc = new MyRedisCacheManager(redisTemplate);

return mrc;

}

}

相关知识

springboot毕设项目网上鲜花销售系统6r845(java+VUE+Mybatis+Maven+Mysql)
老树新花:旧有项目的简易前端资源缓存处理
JNI 提示
编程实践精华总结集锦系列1: SpringBoot/Maven/IDEA/Java/Kotlin/Redis等等
基于JAVA馥郁花艺网站mp4计算机毕业设计源码+数据库+lw文档+系统+部署
鲜花商场管理系统(Java毕业设计)
基于Java+SpringBoot+Vue+Redis+RabbitMq的鲜花商城
如何设计一个支撑数亿用户的系统
基于SSH框架实现的鲜花订购系统
网上花店设计+vue毕业设计(源码+lw+部署文档+讲解等)

网址: springboot增加注解缓存@Cacheable https://m.huajiangbk.com/newsview131066.html

所属分类:花卉
上一篇: 十大奇花==大棵喜菊==3花
下一篇: 可大量批发===洋兰,红美人