springboot with multiple cachemanager


springboot integration redis
need to introduce

Official propulsion lettuce connection pool, Lettuce connection is based Netty, the connection instance (StatefulRedisConnection) between multiple threads can concurrently access, should be thread-safe StatefulRedisConnection


@Configuration
public class EhcacheConfig {


public interface CacheManagerNames {
String REDIS_CACHE_MANAGER = "redisCacheManager";
String EHCACHE_CACHE_MANAGER = "ehCacheManager";
}

@Bean(name = CacheManagerNames.REDIS_CACHE_MANAGER)
public RedisCacheManager redisCacheManager(RedisConnectionFactory factory) {
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(30));
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(factory))
.cacheDefaults(configuration).build();

}

@Bean
public EhCacheManagerFactoryBean cacheManagerFactoryBean(){
EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
bean.setShared(true);
return bean;
}
/**
* @Description @Primary 设置默认加载manager
* @param bean
* @return org.springframework.cache.ehcache.EhCacheCacheManager
* @date 2019/5/31 4:38 PM
* @auther lixin
*/
@Bean(name = CacheManagerNames.EHCACHE_CACHE_MANAGER)
@Primary
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
return new EhCacheCacheManager(bean.getObject());
}

/**
* @Description 自定义缓存 key规则
* @return org.springframework.cache.interceptor.KeyGenerator
* @date 2019/5/31 4:38 PM
* @auther lixin
*/
@Bean
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append (method.getName ());
for (Object obj: params) {
// Since the parameters may be different, hashCode certainly not the same, the cache key need is not the same
sb.append (JSON.toJSONString (obj) .hashCode ());
}
return sb.toString ();
};
}


/ **
* set the default expiration time redis data, the default one day
* provided @cacheable serialization
* @return
* /
@Bean
public redisCacheConfiguration redisCacheConfiguration () {
FastJsonRedisSerializer <Object> = new new fastJsonRedisSerializer fastJsonRedisSerializer <> (Object.class);
RedisCacheConfiguration RedisCacheConfiguration.defaultCacheConfig Configuration = ();
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(1));
return configuration;
}
}
序列化 需要自定义
 FastJsonRedisSerializer class

public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

private Class<T> clazz;

public FastJsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}

@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}

@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}

}





Guess you like

Origin www.cnblogs.com/pengthrree/p/10956078.html