redis介绍
Redis是目前业界使用最广泛的内存数据存储。相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化。除此之外,Redis还提供一些类数据库的特性,比如事务,HA,主从库。可以说Redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。本文介绍Redis在Spring Boot中两个典型的应用场景。
如何使用
1、引入 spring-boot-starter-redis
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.0.58
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
springboot2.x 的 RedisCacheManager变化
由于最近在学着使用redis做缓存,使用的是springboot2.x来搭建的项目。
看了看网上的一些教程,但是大多数教程都是基于1.x的版本来讲解的,但是springboot2.x之后发生了一些变动,网上想找一些资料不太容易。
springboot配置缓存过期时间,网上大部分资料是使用ReidsCacheManager来进行自定义的配置
以下是大部分网上的代码(这也是基于springboot1.x的版本可以使用的)
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(60);
Map expiresMap=new HashMap<>();
expiresMap.put("Product",5L);
cacheManager.setExpires(expiresMap);
return cacheManager;
}
然而在springboot2.x中,RedisCacheManager已经没有了单参数的构造方法
@Configuration
@EnableCaching
public class RedisConfiguratioon extends CachingConfigurerSupport {
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration=RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(null);
return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
@Bean
@SuppressWarnings({"rawtypes", "unchecked"})
public RedisTemplate
Example 1
Project: springboot_cwenao File: RedisConfig.java
@Bean(name="redisTemplate")
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
RedisSerializer 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);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
Example 2
Project: springboot_cwenao File: RedisConfig.java
@Bean(name=“redisTemplate”)
public RedisTemplate
RedisTemplate template = new RedisTemplate<>();
RedisSerializer 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);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
Example 3
Project: springboot_op File: RedisConfiguration.java
/**
* RedisTemplate配置
* @param factory
* @return
*/
@Bean
@SuppressWarnings({"rawtypes", "unchecked"})
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
//定义value的序列化方式
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);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
Example 4
Project: JavaQuarkBBS File: RedisConfig.java
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
//rcm.setDefaultExpiration(60);//秒
return rcm;
}
Example 5
Project: springboot-smart File: RedisConfiguration.java
@Bean("redisTemplate") //新家的这个注解 10-26 12:06
@SuppressWarnings({ "rawtypes", "unchecked" })
public RedisTemplate redisTemplate(RedisConnectionFactory redisFactory){
StringRedisTemplate template = new StringRedisTemplate(redisFactory);
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);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
Example 6
Project: action File: RedisUtil.java
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
Example 7
Project: keti File: AttributeCacheFactory.java
private AttributeCache createAttributeCache(final String cacheType, final long maxCachedIntervalMinutes,
final String zoneName, final RedisTemplate cacheRedisTemplate,
final BiFunction getKey, final boolean enableAttributeCaching) {
String[] profiles = environment.getActiveProfiles();
if (!enableAttributeCaching) {
LOGGER.info("Caching disabled for {} attributes.", cacheType);
return new NonCachingAttributeCache();
}
if (ArrayUtils.contains(profiles, "redis") || ArrayUtils.contains(profiles, "cloud-redis")) {
LOGGER.info("Redis caching enabled for {} attributes.", cacheType);
return new RedisAttributeCache(maxCachedIntervalMinutes, zoneName, getKey, cacheRedisTemplate);
}
LOGGER.info("In-memory caching enabled for {} attributes.", cacheType);
return new InMemoryAttributeCache(maxCachedIntervalMinutes, zoneName, getKey);
}
Example 8
Project: storm_spring_boot_demo File: RedisConfUtils.java
public static RedisTemplate buildRedisTemplate(byte[] redisProperties){
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(
RedisConfUtils.getClusterConfiguration(
(RedisProperties) Serializer.INSTANCE.deserialize(redisProperties)));
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
jedisConnectionFactory.afterPropertiesSet();
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
Example 9
Project: ait-platform File: AitCacheConfigBase.java
@Bean
@Primary
RedisTemplate redisTemplate(RedisConnectionFactory rcf) {
AitLogger.debug(logger, "Serializer overriding ");
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(rcf);
GenericJackson2JsonRedisSerializer valSerializer = new GenericJackson2JsonRedisSerializer();
template.setValueSerializer(valSerializer);
template.setKeySerializer(new RedisSerializer() {
@Override
public byte[] serialize(Object t) throws SerializationException {
return (t == null ? null : (":" + t.toString()).getBytes());
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
return (bytes == null ? null : new String(bytes));
}
});
template.setHashValueSerializer(valSerializer);
return template;
}
Example 10
Project: data-migration File: RedisConfiguration.java
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
Example 11
Project: happylifeplat-transaction File: RedisClusterConfig.java
@Bean
public RedisTemplate getStringRedisTemplate() {
StringRedisTemplate clusterTemplate = new StringRedisTemplate();
clusterTemplate.setConnectionFactory(jedisConnectionFactory());
clusterTemplate.setKeySerializer(new StringRedisSerializer());
clusterTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return clusterTemplate;
}
Example 12
Project: JavaStudy File: Test.java View source code
@org.junit.Test
public void test(){
String user_prefix="user_";
User user=new User(1,"aric");
ApplicationContext atc=new AnnotationConfigApplicationContext(RedisStartConfig.class);
RedisTemplate obj = (RedisTemplate) atc.getBean("redisTemplate");
PrinterUtils.printILog(obj.getKeySerializer());
obj.opsForValue().set(user_prefix+user.getId(),user);
RedisSentinelConfiguration RedisSentinelConfiguration = (RedisSentinelConfiguration) atc.getBean("sentinelConfig");
PrinterUtils.printILog(RedisSentinelConfiguration);
User value=obj.opsForValue().get(user_prefix+1);
PrinterUtils.printELog(value.getUsername());
}
Example 13
Project: springboot-shiro-cas-mybatis File: RedisConfig.java
@SuppressWarnings({"unchecked","rawtypes"})
@Bean
public RedisTemplate redisTemplate(
RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
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);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
Example 14
Project: springboot-shiro-cas-mybatis File: RedisConfig.java
@SuppressWarnings({"unchecked","rawtypes"})
@Bean
public RedisTemplate redisTemplate(
RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
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);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
Example 15
Project: parrot-rest File: ApplicationTestConfiguration.java
@Bean
public RedisTemplate redisTemplate() {
@SuppressWarnings("unchecked")
RedisTemplate template = mock(RedisTemplate.class);
opsHash = new HashOperationsMock();
when(template.opsForHash()).thenReturn(opsHash);
return template;
}
Example 16
Project: JavaQuarkBBS File: RedisConfig.java
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
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);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
Example 17
Project: JavaQuarkBBS File: RedisConfig.java
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
//rcm.setDefaultExpiration(60);//秒
return rcm;
}
Example 18
Project: lemcloud File: RedisConfig.java
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setSerializer(RedisTemplate template) {
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);
template.setKeySerializer(new StringRedisSerializer());
//template.setValueSerializer(jackson2JsonRedisSerializer); //-- 值序列化为json
template.setValueSerializer(new RedisObjectSerializer()); //-- 默认序列化为byte[]
}
Example 19
Project: iBase4J-Common File: RedisHelper.java
@SuppressWarnings("unchecked")
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
this.keySerializer = (RedisSerializer)redisTemplate.getKeySerializer();
this.valueSerializer = (RedisSerializer)redisTemplate.getValueSerializer();
CacheUtil.setCacheManager(this);
}
Example 20
Project: iBase4J-Common File: JedisConfig.java
@Bean
@Qualifier("redisTemplate")
public CacheManager redisCacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setTransactionAware(true);
cacheManager.setDefaultExpiration(PropertiesUtil.getInt("redis.expiration", 10));
return cacheManager;
}
Example 21
Project: neto File: AppConfig.java View source code
private RedisTemplate jsonRedisTemplate(JedisConnectionFactory jedisConnectionFactory, Class> clazz) {
RedisTemplate template = new RedisTemplate();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new Jackson2JsonRedisSerializer(clazz));
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new Jackson2JsonRedisSerializer(clazz));
template.setConnectionFactory(jedisConnectionFactory);
return template;
}
Example 22
Project: tx-lcn File: JedisClusterConfig.java
@Bean
public RedisTemplate redisTemplateFactory(){
RedisTemplate redisTemplate =new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
//指定具体序列化方式 不过这种方式不是很好,一个系统中可能对应值的类型不一样,如果全部使用StringRedisSerializer 序列化
//会照成其他类型报错,所以还是推荐使用第一种,直接指定泛型的类型,spring 会根据指定类型序列化。
//redisTemplate.setKeySerializer( new StringRedisSerializer());
//redisTemplate.setValueSerializer(new StringRedisSerializer());
//redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//redisTemplate.setHashValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
Example 23
Project: xproject File: RedisCache.java
public RedisCache(String cacheName, RedisTemplate redisTemplate, boolean cacheExpire,
int cacheExpireSeconds, String cacheKeyPrefix) {
super();
Assert.hasText(cacheName, "Parameter 'cacheName' can not be empty!");
Assert.notNull(redisTemplate, "Parameter 'redisTemplate' can not be null!");
Assert.isTrue(cacheExpireSeconds > 0, "Parameter 'cacheExpireSeconds' must be > 0!");
this.cacheName = cacheName;
this.redisTemplate = redisTemplate;
this.cacheExpire = cacheExpire;
this.cacheExpireSeconds = cacheExpireSeconds;
this.cacheKeyPrefix = cacheKeyPrefix;
}
Example 24
Project: automat File: RedisHelper.java
@SuppressWarnings("unchecked")
private RedisTemplate getRedis() {
if (redisTemplate == null) {
synchronized (RedisHelper.class) {
if (redisTemplate == null) {
redisTemplate = (RedisTemplate)applicationContext
.getBean("redisTemplate");
}
}
}
return redisTemplate;
}
Example 25
Project: spring-data-examples File: ApplicationConfiguration.java
@Bean
RedisTemplate, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(connectionFactory);
return template;
}
Example 26
Project: filter File: JedisClusterConfig.java
@Bean
public RedisTemplate redisTemplateFactory(){
RedisTemplate redisTemplate =new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
//指定具体序列化方式 不过这种方式不是很好,一个系统中可能对应值的类型不一样,如果全部使用StringRedisSerializer 序列化
//会照成其他类型报错,所以还是推荐使用第一种,直接指定泛型的类型,spring 会根据指定类型序列化。
// redisTemplate.setKeySerializer( new StringRedisSerializer());
// redisTemplate.setValueSerializer(new StringRedisSerializer());
// redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// redisTemplate.setHashValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
Example 27
Project: OutsourcedProject File: RedisConfig.java
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
Example 28
Project: MI-S File: RedisConfig.java
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
//rcm.setDefaultExpiration(60);//秒
return rcm;
}
Example 29
Project: boot-mon File: BootmonServerRedisConfig.java
@Bean
public RedisTemplate redisTemplate() {
final RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(jedisConnectionFactory());
template.setValueSerializer(new GenericToStringSerializer(Object.class));
return template;
}
Example 30
Project: bird-java File: RedisCacher.java V
@SuppressWarnings("unchecked")
private RedisTemplate getRedis() {
if (redisTemplate == null) {
synchronized (RedisCacher.class) {
if (redisTemplate == null) {
redisTemplate = (RedisTemplate)applicationContext
.getBean("redisTemplate");
}
}
}
return redisTemplate;
}`在这里插入代码片`
Example 31
Project: shoucang File: RedisConfiguration.java
@Bean(name = "redisListTemplate")
RedisTemplate redisListTemplate() {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class));
return redisTemplate;
}
`
Example 32
Project: shoucang File: RedisConfiguration.java View source code 5 votes vote down vote up
@Bean(name = "redisCountingTemplate")
RedisTemplate redisCountingTemplate() {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Integer.class));
return redisTemplate;
}
Example 33
Project: raven File: CacheService.java View source code 5 votes vote down vote up
/** 查询huawei accessToken */
public AccessToken getHuaweiAK()
{
Cache redisCache1101 = commonCacheManager.getCache("redisCache1101");
@SuppressWarnings("unchecked")
RedisTemplate template = (RedisTemplate)redisCache1101.getNativeCache();
Set keys = template.keys(CacheKeyPrefixConst.HW_ACCESS_TOKEN);
if (null == keys || keys.isEmpty())
{
return null;
}
return redisCache1101.get(keys.iterator().next(), AccessToken.class);
}
Example 34
Project: DanmuChat File: RedisConfig.java View source code 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
RedisCacheManager redisCacheManager =new RedisCacheManager(redisTemplate);
//设置缓存过期时间
redisCacheManager.setDefaultExpiration(60);//秒
return redisCacheManager;
}
Example 35
Project: DanmuChat File: RedisConfig.java View source code 5 votes vote down vote up
/**
* 序列化设置
*/
private void initRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
}
Example 36
Project: spring-boot-start-current File: SpringCacheConfig.java View source code 5 votes vote down vote up
@Bean
public CacheManager cacheManager ( RedisTemplate redisTemplate ) {
RedisCacheManager redisCacheManager = new RedisCacheManager( redisTemplate );
// 默认30分钟
redisCacheManager.setDefaultExpiration( defaultExpiration );
redisCacheManager.setExpires( expires );
redisCacheManager.setUsePrefix( usePrefix );
return redisCacheManager;
}
Example 37
Project: keti File: RedisAttributeCache.java View source code 5 votes vote down vote up
RedisAttributeCache(final long maxCachedIntervalMinutes, final String zoneName,
final BiFunction getKey,
final RedisTemplate resourceCacheRedisTemplate) {
this.maxCachedIntervalMinutes = maxCachedIntervalMinutes;
this.zoneName = zoneName;
this.getKey = getKey;
this.resourceCacheRedisTemplate = resourceCacheRedisTemplate;
}
Example 38
Project: keti File: CommonRedisConfig.java View source code 5 votes vote down vote up
private RedisTemplate createCacheRedisTemplate(final RedisConnectionFactory redisConnectionFactory,
final String redisTemplateType) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
LOGGER.info("Successfully created {} Redis template.", redisTemplateType);
return redisTemplate;
}
Example 39
Project: storm_spring_boot_demo File: RedisConfig.java View source code 5 votes vote down vote up
@Bean("redisTemplate")
public RedisTemplate, ?> getRedisTemplate(){
RedisTemplate,?> template = new StringRedisTemplate(jedisConnectionFactory);
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(genericJackson2JsonRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
return template;
}
Example 40
Project: xq_seckill_microservice File: CachingConfig.java View source code 5 votes vote down vote up
@Bean
public RedisTemplate byteRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setEnableDefaultSerializer(true);
//redisTemplate.setDefaultSerializer(new StringRedisSerializer());
redisTemplate.setKeySerializer(new StringRedisSerializer());
//redisTemplate.setValueSerializer(new StringRedisSerializer());
//redisTemplate.setEnableTransactionSupport(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}