@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; }
/** * 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; }
@SuppressWarnings("rawtypes") @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); //设置缓存过期时间 //rcm.setDefaultExpiration(60);//秒 return rcm; }
@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; }
@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; }
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); }
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; }
@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; }
@Bean public RedisTemplate getStringRedisTemplate() { StringRedisTemplate clusterTemplate = new StringRedisTemplate(); clusterTemplate.setConnectionFactory(jedisConnectionFactory()); clusterTemplate.setKeySerializer(new StringRedisSerializer()); clusterTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return clusterTemplate; }
@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()); }
@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; }
@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; }
@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; }
@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[] }
@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); }
@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; }
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; }
@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; }
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; }
@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; }
@Bean RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>(); template.setConnectionFactory(connectionFactory); return template; }
@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; }
@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; }
@Bean public CacheManager cacheManager(RedisTemplate redisTemplate){ RedisCacheManager rcm = new RedisCacheManager(redisTemplate); //设置缓存过期时间 //rcm.setDefaultExpiration(60);//秒 return rcm; }
@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; }
@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; }
@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; }
@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; }
/** 查询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); }
@SuppressWarnings("rawtypes") @Bean public CacheManager cacheManager(RedisTemplate redisTemplate){ RedisCacheManager redisCacheManager =new RedisCacheManager(redisTemplate); //设置缓存过期时间 redisCacheManager.setDefaultExpiration(60);//秒 return redisCacheManager; }
/** * 序列化设置 */ 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); }
@Bean public CacheManager cacheManager ( RedisTemplate redisTemplate ) { RedisCacheManager redisCacheManager = new RedisCacheManager( redisTemplate ); // 默认30分钟 redisCacheManager.setDefaultExpiration( defaultExpiration ); redisCacheManager.setExpires( expires ); redisCacheManager.setUsePrefix( usePrefix ); return redisCacheManager; }
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; }
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; }
@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; }
@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; }