Java Code Examples for org.springframework.data.redis.core.RedisTemplate

The following are top voted examples for showing how to use org.springframework.data.redis.core.RedisTemplate. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: springboot_cwenao   File: RedisConfig.java   View source code 11 votes vote down vote up
@Bean(name="redisTemplate")
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {

    RedisTemplate<String, String> template = new RedisTemplate<>();


    RedisSerializer<String> 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   View source code 7 votes vote down vote up
@Bean(name="redisTemplate")
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {

    RedisTemplate<String, String> template = new RedisTemplate<>();


    RedisSerializer<String> 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: storm_spring_boot_demo   File: RedisConfUtils.java   View source code 7 votes vote down vote up
public static RedisTemplate buildRedisTemplate(byte[] redisProperties){
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(
            RedisConfUtils.getClusterConfiguration(
                    (RedisProperties) Serializer.INSTANCE.deserialize(redisProperties)));
    RedisTemplate<String, Long> 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 4
Project: ait-platform   File: AitCacheConfigBase.java   View source code 7 votes vote down vote up
@Bean
@Primary
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rcf) {
	AitLogger.debug(logger, "Serializer overriding ");

	RedisTemplate<String, Object> template = new RedisTemplate<>();
	template.setConnectionFactory(rcf);

	GenericJackson2JsonRedisSerializer valSerializer = new GenericJackson2JsonRedisSerializer();

	template.setValueSerializer(valSerializer);
	template.setKeySerializer(new RedisSerializer<Object>() {

		@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 5
Project: springboot_op   File: RedisConfiguration.java   View source code 6 votes vote down vote up
/**
 * RedisTemplate配置
 * @param factory
 * @return
 */
@Bean
@SuppressWarnings({"rawtypes", "unchecked"})
public RedisTemplate<String, String> 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 6
Project: JavaQuarkBBS   File: RedisConfig.java   View source code 6 votes vote down vote up
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //设置缓存过期时间
    //rcm.setDefaultExpiration(60);//秒
    return rcm;
}
 
Example 7
Project: springboot-smart   File: RedisConfiguration.java   View source code 6 votes vote down vote up
@Bean("redisTemplate")  //新家的这个注解 10-26 12:06
@SuppressWarnings({ "rawtypes", "unchecked" })
public RedisTemplate<String, String> 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 8
Project: action   File: RedisUtil.java   View source code 6 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> 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 9
Project: spring-boot-start-current   File: SpringCacheConfig.java   View source code 6 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 10
Project: keti   File: AttributeCacheFactory.java   View source code 6 votes vote down vote up
private AttributeCache createAttributeCache(final String cacheType, final long maxCachedIntervalMinutes,
        final String zoneName, final RedisTemplate<String, String> cacheRedisTemplate,
        final BiFunction<String, String, String> 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 11
Project: data-migration   File: RedisConfiguration.java   View source code 6 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> 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 12
Project: happylifeplat-transaction   File: RedisClusterConfig.java   View source code 5 votes vote down vote up
@Bean
public RedisTemplate getStringRedisTemplate() {
    StringRedisTemplate clusterTemplate = new StringRedisTemplate();
    clusterTemplate.setConnectionFactory(jedisConnectionFactory());
    clusterTemplate.setKeySerializer(new StringRedisSerializer());
    clusterTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    return clusterTemplate;
}
 
Example 13
Project: JavaStudy   File: Test.java   View source code 5 votes vote down vote up
@org.junit.Test
public void test(){
    String  user_prefix="user_";
    User user=new User(1,"aric");
    ApplicationContext atc=new AnnotationConfigApplicationContext(RedisStartConfig.class);
    RedisTemplate<String,User> 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 14
Project: springboot-shiro-cas-mybatis   File: RedisConfig.java   View source code 5 votes vote down vote up
@SuppressWarnings({"unchecked","rawtypes"})
@Bean  
   public RedisTemplate<String, String> 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: springboot-shiro-cas-mybatis   File: RedisConfig.java   View source code 5 votes vote down vote up
@SuppressWarnings({"unchecked","rawtypes"})
@Bean  
   public RedisTemplate<String, String> 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 16
Project: parrot-rest   File: ApplicationTestConfiguration.java   View source code 5 votes vote down vote up
@Bean
public RedisTemplate<String, Phrase> redisTemplate() {
    @SuppressWarnings("unchecked")
	RedisTemplate<String, Phrase> template = mock(RedisTemplate.class);
    opsHash = new HashOperationsMock();
	when(template.opsForHash()).thenReturn(opsHash);
    return template;
}
 
Example 17
Project: JavaQuarkBBS   File: RedisConfig.java   View source code 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> 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 18
Project: JavaQuarkBBS   File: RedisConfig.java   View source code 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //设置缓存过期时间
    //rcm.setDefaultExpiration(60);//秒
    return rcm;
}
 
Example 19
Project: lemcloud   File: RedisConfig.java   View source code 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
  private void setSerializer(RedisTemplate<String, Object> 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 20
Project: iBase4J-Common   File: RedisHelper.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void setRedisTemplate(RedisTemplate<Serializable, Serializable> redisTemplate) {
    this.redisTemplate = redisTemplate;
    this.keySerializer = (RedisSerializer<String>)redisTemplate.getKeySerializer();
    this.valueSerializer = (RedisSerializer<Object>)redisTemplate.getValueSerializer();
    CacheUtil.setCacheManager(this);
}
 
Example 21
Project: iBase4J-Common   File: JedisConfig.java   View source code 5 votes vote down vote up
@Bean
@Qualifier("redisTemplate")
public CacheManager redisCacheManager(RedisTemplate<Serializable, Serializable> redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    cacheManager.setTransactionAware(true);
    cacheManager.setDefaultExpiration(PropertiesUtil.getInt("redis.expiration", 10));
    return cacheManager;
}
 
Example 22
Project: neto   File: AppConfig.java   View source code 5 votes vote down vote up
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 23
Project: tx-lcn   File: JedisClusterConfig.java   View source code 5 votes vote down vote up
@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 24
Project: xproject   File: RedisCache.java   View source code 5 votes vote down vote up
public RedisCache(String cacheName, RedisTemplate<String, Object> 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 25
Project: automat   File: RedisHelper.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
private RedisTemplate<Serializable, Serializable> getRedis() {
    if (redisTemplate == null) {
        synchronized (RedisHelper.class) {
            if (redisTemplate == null) {
                redisTemplate = (RedisTemplate<Serializable, Serializable>)applicationContext
                        .getBean("redisTemplate");
            }
        }
    }
    return redisTemplate;
}
 
Example 26
Project: spring-data-examples   File: ApplicationConfiguration.java   View source code 5 votes vote down vote up
@Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {

	RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
	template.setConnectionFactory(connectionFactory);

	return template;
}
 
Example 27
Project: filter   File: JedisClusterConfig.java   View source code 5 votes vote down vote up
@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 28
Project: OutsourcedProject   File: RedisConfig.java   View source code 5 votes vote down vote up
@Bean
public <K, V> RedisTemplate<K, V> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<K, V> 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 29
Project: MI-S   File: RedisConfig.java   View source code 5 votes vote down vote up
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //设置缓存过期时间
    //rcm.setDefaultExpiration(60);//秒
    return rcm;
}
 
Example 30
Project: boot-mon   File: BootmonServerRedisConfig.java   View source code 5 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate() {
    final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
    return template;
}
 
Example 31
Project: bird-java   File: RedisCacher.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
private RedisTemplate<Serializable, Serializable> getRedis() {
    if (redisTemplate == null) {
        synchronized (RedisCacher.class) {
            if (redisTemplate == null) {
                redisTemplate = (RedisTemplate<Serializable, Serializable>)applicationContext
                        .getBean("redisTemplate");
            }
        }
    }
    return redisTemplate;
}
 
Example 32
Project: shoucang   File: RedisConfiguration.java   View source code 5 votes vote down vote up
@Bean(name = "redisListTemplate")
RedisTemplate<String, Long> redisListTemplate() {
    RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class));
    return redisTemplate;
}
 
Example 33
Project: shoucang   File: RedisConfiguration.java   View source code 5 votes vote down vote up
@Bean(name = "redisCountingTemplate")
RedisTemplate<String, Integer> redisCountingTemplate() {
    RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Integer.class));
    return redisTemplate;
}
 
Example 34
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<String, AccessToken> template = (RedisTemplate<String, AccessToken>)redisCache1101.getNativeCache();
    Set<String> keys = template.keys(CacheKeyPrefixConst.HW_ACCESS_TOKEN);

    if (null == keys || keys.isEmpty())
    {
        return null;
    }

    return redisCache1101.get(keys.iterator().next(), AccessToken.class);
}
 
Example 35
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 36
Project: DanmuChat   File: RedisConfig.java   View source code 5 votes vote down vote up
/**
 * 序列化设置
 */
private void initRedisTemplate(RedisTemplate<String, String> redisTemplate, RedisConnectionFactory factory) {
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
}
 
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<String, String, String> getKey,
        final RedisTemplate<String, String> 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<String, String> createCacheRedisTemplate(final RedisConnectionFactory redisConnectionFactory,
        final String redisTemplateType) {
    RedisTemplate<String, String> 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;
}