自定义声明式redis缓存序列化fang
来源:互联网 发布:exe文件加密网络授权 编辑:程序博客网 时间:2024/05/17 13:08
package person.txm.springredis.config;
import java.util.HashMap;
import java.util.Map;
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.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@Configuration
@EnableCaching
public class CacheConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(redisSerializer());
return redisTemplate;
}
@Bean
public CacheManager cacheManager(){
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setUsePrefix(true);
cacheManager.setDefaultExpiration(86400L);
Map<String, Long> expires = new HashMap<String, Long>();
expires.put("person", 1000L);
cacheManager.setExpires(expires);
return cacheManager;
}
@Bean
public GenericJackson2JsonRedisSerializer redisSerializer() {
ObjectMapper om=new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL,Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return new GenericJackson2JsonRedisSerializer(om);
}
}
import java.util.HashMap;
import java.util.Map;
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.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@Configuration
@EnableCaching
public class CacheConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(redisSerializer());
return redisTemplate;
}
@Bean
public CacheManager cacheManager(){
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setUsePrefix(true);
cacheManager.setDefaultExpiration(86400L);
Map<String, Long> expires = new HashMap<String, Long>();
expires.put("person", 1000L);
cacheManager.setExpires(expires);
return cacheManager;
}
@Bean
public GenericJackson2JsonRedisSerializer redisSerializer() {
ObjectMapper om=new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL,Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return new GenericJackson2JsonRedisSerializer(om);
}
}
0 0
- 自定义声明式redis缓存序列化fang
- spring-data-redis使用自定义序列化数据 使用 protobuf
- Redis序列化存储Java集合List等自定义类型
- Redis序列化存储Java集合List等自定义类型
- 将对象序列化存到redis缓存中
- Fang Fang
- Fang Fang
- Mybatis自定义缓存——Redis实现
- spring+ehCache+redis多级缓存自定义实现
- Spring Cache+Redis实现自定义注解缓存
- Redis+MyBatis自定义注解实现缓存
- Redis缓存Object,List对象 - Java 对象和List的序列化和反序列化
- 使用Spring Cache + Redis + Jackson Serializer缓存数据库查询结果中序列化问题的解决
- Redis序列化
- Redis的序列化
- 序列化---自定义序列化
- [Dubbo实战]Dubbo声明式缓存
- Spring缓存与声明式事务管理
- Android进程优先级oom_adj
- TCP/IP协议 简析
- 金融时间序列分析:6. AR模型实例(R语言)
- Photoshop批量替换PSD文件中的文字并转成JPEG图片
- poj --1125----Stockbroker Grapevine(多源最短路径)
- 自定义声明式redis缓存序列化fang
- OpenStack网络指南(18)负载均衡服务(LBaaS)
- InputStream输入流,读取数据实例
- 【总结】PHP常见面试题汇总(三)
- Hibernate的核心组件,三种状态,常用方法和缓存机制
- MAC将根目录文件夹的权限赋给用户
- C++中一个头文件可以对应多个源文件
- 5.1通信数据转发程序:代理,网关,隧道
- C语言判断两个字符串是否相等