org.springframework.data.redis.serializer.StringRedisSerializer Java Examples

The following examples show how to use org.springframework.data.redis.serializer.StringRedisSerializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisConfig.java    From FEBS-Security with Apache License 2.0 6 votes vote down vote up
@Bean(name = "redisTemplate")
@SuppressWarnings({"unchecked", "rawtypes"})
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    //使用 fastjson 序列化
    FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
    // value 值的序列化采用 fastJsonRedisSerializer
    template.setValueSerializer(fastJsonRedisSerializer);
    template.setHashValueSerializer(fastJsonRedisSerializer);
    // key 的序列化采用 StringRedisSerializer
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());

    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #2
Source File: RedisCacheConfig.java    From ywh-frame with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 解决注解方式存放到redis中的值是乱码的情况
 * @param factory 连接工厂
 * @return CacheManager
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

    // 配置注解方式的序列化
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
    RedisCacheConfiguration redisCacheConfiguration =
            config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
                    //配置注解默认的过期时间
                    .entryTtl(Duration.ofDays(1));
    // 加入白名单   https://github.com/alibaba/fastjson/wiki/enable_autotype
    ParserConfig.getGlobalInstance().addAccept("com.ywh");
    ParserConfig.getGlobalInstance().addAccept("com.baomidou");
    return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
 
Example #3
Source File: SmartRedisConfig.java    From smart-admin with MIT License 6 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate() {
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(om);
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.setDefaultSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #4
Source File: RedisAutoConfig.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * <p>支持FastJson进行Redis存储对象序列/反序列化
 * <p>https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
 */
@Bean
public RedisTemplate<String, Object> yueRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
	RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
	redisTemplate.setConnectionFactory(redisConnectionFactory);
	
	// 支持FastJson进行Redis存储对象序列/反序列化
	if (redisProperties.getRedisSerializerEnum() != RedisSerializerEnum.JDK) {
		redisTemplate.setDefaultSerializer(redisProperties.getRedisSerializerEnum().getRedisSerializer());
	}
	StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
	redisTemplate.setKeySerializer(stringRedisSerializer);
	redisTemplate.setHashKeySerializer(stringRedisSerializer);
	
	return redisTemplate;
}
 
Example #5
Source File: RateLimiterPluginDataHandler.java    From soul with Apache License 2.0 6 votes vote down vote up
@Override
public void handlerPlugin(final PluginData pluginData) {
    if (Objects.nonNull(pluginData) && pluginData.getEnabled()) {
        //init redis
        RateLimiterConfig rateLimiterConfig = GsonUtils.getInstance().fromJson(pluginData.getConfig(), RateLimiterConfig.class);
        //spring data redisTemplate
        if (Objects.isNull(Singleton.INST.get(ReactiveRedisTemplate.class))
                || Objects.isNull(Singleton.INST.get(RateLimiterConfig.class))
                || !rateLimiterConfig.equals(Singleton.INST.get(RateLimiterConfig.class))) {
            LettuceConnectionFactory lettuceConnectionFactory = createLettuceConnectionFactory(rateLimiterConfig);
            lettuceConnectionFactory.afterPropertiesSet();
            RedisSerializer<String> serializer = new StringRedisSerializer();
            RedisSerializationContext<String, String> serializationContext =
                    RedisSerializationContext.<String, String>newSerializationContext().key(serializer).value(serializer).hashKey(serializer).hashValue(serializer).build();
            ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(lettuceConnectionFactory, serializationContext);
            Singleton.INST.single(ReactiveRedisTemplate.class, reactiveRedisTemplate);
            Singleton.INST.single(RateLimiterConfig.class, rateLimiterConfig);
        }
    }
}
 
Example #6
Source File: RedisConfig.java    From demo-project with MIT License 6 votes vote down vote up
@SuppressWarnings("all")
@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);
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example #7
Source File: CartTest.java    From Project with Apache License 2.0 6 votes vote down vote up
@Test
public void settingKeyAndValueSerializers() {
	// need a local version so we can tweak the serializer
	RedisTemplate<String, Product> redis = new RedisTemplate<String, Product>();
	redis.setConnectionFactory(cf);
	redis.setKeySerializer(new StringRedisSerializer());
	redis.setValueSerializer(new Jackson2JsonRedisSerializer<Product>(Product.class));
	redis.afterPropertiesSet(); // if this were declared as a bean, you wouldn't have to do this
	
	Product product = new Product();
	product.setSku("9781617291203");
	product.setName("Spring in Action");
	product.setPrice(39.99f);
	
	redis.opsForValue().set(product.getSku(), product);
	
	Product found = redis.opsForValue().get(product.getSku());
	assertEquals(product.getSku(), found.getSku());
	assertEquals(product.getName(), found.getName());
	assertEquals(product.getPrice(), found.getPrice(), 0.005);
	
	StringRedisTemplate stringRedis = new StringRedisTemplate(cf);
	String json = stringRedis.opsForValue().get(product.getSku());
	assertEquals("{\"sku\":\"9781617291203\",\"name\":\"Spring in Action\",\"price\":39.99}", json);
}
 
Example #8
Source File: RedisConfig.java    From demo-project with MIT License 6 votes vote down vote up
@Bean(name = "redisTemplate")
@SuppressWarnings("all")
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    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);

    // 设置RedisTemplate
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(serializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}
 
Example #9
Source File: RedisConfig.java    From spring-boot-study with MIT License 6 votes vote down vote up
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
    RedisTemplate<Object,Object> template = new RedisTemplate<>();
    //设置连接池
    template.setConnectionFactory(redisConnectionFactory);
    StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
    //FastJsonRedisSerializer<Object> serializer = new FastJsonRedisSerializer<Object>(Object.class);
    //也可以使用 Jackson2JsonRedisSerializer
    //Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    //value 的序列号 使用 fastjson
    template.setValueSerializer(stringRedisSerializer);
    //hash value的序列化问题
    template.setHashValueSerializer(stringRedisSerializer);
    //key 的序列化
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());

    return  template;
}
 
Example #10
Source File: RedisConfig.java    From open-capacity-platform with Apache License 2.0 6 votes vote down vote up
@Primary
@Bean("redisTemplate")
// 没有此属性就不会装配bean 如果是单个redis 将此注解注释掉
@ConditionalOnProperty(name = "spring.redis.cluster.nodes", matchIfMissing = false)
public RedisTemplate<String, Object> getRedisTemplate() {
	RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
	redisTemplate.setConnectionFactory(lettuceConnectionFactory);

	RedisSerializer stringSerializer = new StringRedisSerializer();
	// RedisSerializer redisObjectSerializer = new RedisObjectSerializer();
	RedisSerializer redisObjectSerializer = new RedisObjectSerializer();
	redisTemplate.setKeySerializer(stringSerializer); // key的序列化类型
	redisTemplate.setHashKeySerializer(stringSerializer);
	redisTemplate.setValueSerializer(redisObjectSerializer); // value的序列化类型
	redisTemplate.setHashValueSerializer(redisObjectSerializer); // value的序列化类型
	redisTemplate.afterPropertiesSet();

	redisTemplate.opsForValue().set("hello", "wolrd");
	return redisTemplate;
}
 
Example #11
Source File: RedisConfig.java    From spring-boot-seckill with GNU General Public License v2.0 6 votes vote down vote up
/**
    * 序列化Java对象
    * @Author  科帮网
    * @param factory
    * @return  RedisTemplate<Object, Object>
    * @Date	2017年8月13日
    * 更新日志
    * 2017年8月13日 科帮网  首次创建
    *
    */
   @SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
   public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
       RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
       template.setConnectionFactory(connectionFactory);
       //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
       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);
       template.setKeySerializer(new StringRedisSerializer());
       template.afterPropertiesSet();
       return template;
   }
 
Example #12
Source File: FebsLettuceRedisAutoConfigure.java    From FEBS-Cloud with Apache License 2.0 6 votes vote down vote up
@Bean(name = "redisTemplate")
@ConditionalOnClass(RedisOperations.class)
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(mapper);

    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringRedisSerializer);
    template.setHashKeySerializer(stringRedisSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();

    return template;
}
 
Example #13
Source File: RedisCacheConfig.java    From shield-ratelimter with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    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();
    LOGGER.info("Springboot RedisTemplate 加载完成");
    return template;
}
 
Example #14
Source File: RedisLock.java    From java-tutorial with MIT License 6 votes vote down vote up
/**
 * get 取值
 *
 * @param key
 * @return
 */
public String get(final String key) {
    Object object = null;
    try {
        object = redisTemplate.execute((RedisCallback<Object>) redisConnection -> {
            StringRedisSerializer redisSerializer = new StringRedisSerializer();
            byte[] data = redisConnection.get(Objects.requireNonNull(redisSerializer.serialize(key)));


            redisConnection.close();
            if (data == null) {
                return null;
            }
            return redisSerializer.deserialize(data);
        });
    } catch (Exception ex) {
        logger.error("get redis error, key : {}", key);
    }

    return object != null ? object.toString() : null;
}
 
Example #15
Source File: RedisCacheConfig.java    From seckill-rocketmq with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);

    //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    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 #16
Source File: RedisConfig.java    From xmfcn-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * springboot2.x 使用LettuceConnectionFactory 代替 RedisConnectionFactory
 * 在application.yml配置基本信息后,springboot2.x  RedisAutoConfiguration能够自动装配 LettuceConnectionFactory 和 RedisConnectionFactory 及其 RedisTemplate
 *
 * @param redisConnectionFactory
 * @return
 */
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);

    //使用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.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}
 
Example #17
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
/**
 * redis序列化配置,使用lettuce客户端
 */
@Bean
public <T> RedisTemplate<String, T> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, T> template = new RedisTemplate<>();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new GenericFastJsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #18
Source File: RedisCacheConfig.java    From watchdog-framework with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #19
Source File: RedisConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
 */
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #20
Source File: J2CacheRedisAutoConfiguration.java    From Aooms with Apache License 2.0 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
   @ConditionalOnBean(RedisConnectionFactory.class)
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") RedisConnectionFactory j2CahceRedisConnectionFactory,
		@Qualifier("j2CacheValueSerializer") RedisSerializer<Object> j2CacheSerializer) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(j2CacheSerializer);
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}
 
Example #21
Source File: RedisAutoConfiguration.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Bean
@Primary
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

    // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

    // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
    serializer.setObjectMapper(objectMapper);

    RedisTemplate<String, Object> template = new RedisTemplate<>();
    // 配置连接工厂
    template.setConnectionFactory(factory);
    // 值采用json序列化
    template.setValueSerializer(serializer);
    // 使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    // 设置hash key 和value序列化模式
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();

    return template;
}
 
Example #22
Source File: RedisLuaRateLimiter.java    From api-boot with Apache License 2.0 5 votes vote down vote up
/**
 * redis lua away
 * Processing traffic restrictions using LUA scripts
 * Processing with Spring Cloud Gateway official script
 *
 * @param annotationQPS {@link RateLimiter#QPS()}
 * @param requestKey    request key
 * @return true : allow access to
 */
@Override
public boolean tryAcquire(Double annotationQPS, String requestKey) {
    try {
        Long QPS = getPriorityQPS(requestKey, annotationQPS);
        if (QPS <= 0) {
            return true;
        }
        // get keys
        List<String> keys = getKeys(requestKey);

        // Parameters and serialization of return values
        RedisSerializer<String> serializer = new StringRedisSerializer();

        // execute lua script
        List<Long> tokenResult = (List<Long>) this.redisTemplate.execute(this.redisScript, serializer, serializer, keys, String.valueOf(QPS), String.valueOf(QPS), Instant.now().getEpochSecond() + "", "1");

        // Index 1 value is the number of remaining requestable tokens
        Long newTokens = tokenResult.get(1);
        if (logger.isDebugEnabled()) {
            logger.debug("Number of remaining tokens for this request is {}", newTokens);
        }
        return newTokens > 0;
    } catch (Exception e) {
        /*
         * We don't want a hard dependency on Redis to allow traffic. Make sure to set
         * an alert so you know if this is happening too much. Stripe's observed
         * failure rate is 0.01%.
         */
        logger.error("Error determining if user allowed from redis", e);
    }
    return true;
}
 
Example #23
Source File: TcpChannelMsgManage.java    From jt809-tcp-server with MIT License 5 votes vote down vote up
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    RedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
    redisTemplate.setKeySerializer(stringSerializer);
    redisTemplate.setValueSerializer(stringSerializer);
    redisTemplate.setHashKeySerializer(stringSerializer);
    redisTemplate.setHashValueSerializer(jdkSerializer);
    redisTemplate.afterPropertiesSet();
    this.redisTemplate = redisTemplate;
}
 
Example #24
Source File: ShopRedisTemplate.java    From spring-cloud-shop with MIT License 5 votes vote down vote up
/**
 * Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
 * and {@link #afterPropertiesSet()} still need to be called.
 */
public ShopRedisTemplate() {
    RedisSerializer stringSerializer = new StringRedisSerializer();
    RedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
    setKeySerializer(stringSerializer);
    setValueSerializer(genericFastJsonRedisSerializer);
    setHashKeySerializer(genericFastJsonRedisSerializer);
    setHashValueSerializer(genericFastJsonRedisSerializer);
}
 
Example #25
Source File: RedisConfig.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
/**
 * 设置数据存入 redis 的序列化方式
 * @param redisTemplate
 * @param factory
 */
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
    //使用fastjson序列化
    FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
    // value值的序列化采用fastJsonRedisSerializer
    redisTemplate.setValueSerializer(fastJsonRedisSerializer);
    redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
    // key的序列化采用StringRedisSerializer
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
    log.debug("自定义RedisTemplate加载完成");
}
 
Example #26
Source File: RedisConfig.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
/**
 * 设置数据存入 redis 的序列化方式
 * @param redisTemplate
 * @param factory
 */
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
    //使用fastjson序列化
    FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
    // value值的序列化采用fastJsonRedisSerializer
    redisTemplate.setValueSerializer(fastJsonRedisSerializer);
    redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
    // key的序列化采用StringRedisSerializer
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
    log.debug("自定义RedisTemplate加载完成");
}
 
Example #27
Source File: RedisCacheConfig.java    From agile-service-old with Apache License 2.0 5 votes vote down vote up
/**
 * 设置数据存入 redis 的序列化方式
 *
 * @param redisTemplate redisTemplate
 * @param factory       factory
 */
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    redisTemplate.setConnectionFactory(factory);
}
 
Example #28
Source File: RedisCacheFactory.java    From alcor with Apache License 2.0 5 votes vote down vote up
private <K, V> RedisTemplate<K, V> getRedisTemplate(Class<V> v){

        RedisTemplate<K, V> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());

        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(v));
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(v));
        template.afterPropertiesSet();

        return template;
    }
 
Example #29
Source File: CacheFactory.java    From alcor with Apache License 2.0 5 votes vote down vote up
public <K, V> ICache getRedisCache(Class<V> v, String cacheName) {

        RedisTemplate<K, V> template = new RedisTemplate<K, V>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());

        template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(v));
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(v));
        template.afterPropertiesSet();

        return new RedisCache<>(template, cacheName);
    }
 
Example #30
Source File: RedisConfiguration.java    From alcor with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, SubnetState> redisSubnetTemplate() {
    final RedisTemplate<String, SubnetState> template = new RedisTemplate<String, SubnetState>();
    template.setConnectionFactory(lettuceConnectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new Jackson2JsonRedisSerializer<SubnetState>(SubnetState.class));
    template.setValueSerializer(new Jackson2JsonRedisSerializer<SubnetState>(SubnetState.class));
    return template;
}