org.springframework.data.redis.core.StringRedisTemplate Java Examples

The following examples show how to use org.springframework.data.redis.core.StringRedisTemplate. 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: RedisCacheConfig.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * RedisTemplate配置
 * @param factory
 * @return
 */
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    //定义key序列化方式
    //RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
    //定义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.setKeySerializer(redisSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example #2
Source File: CsrfFilter.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * 检查CSRF Token
 *
 * @param req   ServletRequest
 */
private boolean checkCsrfToken(ServletRequest req) {
    AdamProperties adamProperties = CHERRY.SPRING_CONTEXT.getBean(AdamProperties.class);
    boolean isValid = false;

    String csrf = ((HttpServletRequest) req).getHeader(adamProperties.getCsrf().getHeaderName());
    if (StringUtils.isBlank(csrf)) {
        csrf = req.getParameter(adamProperties.getCsrf().getRequestName());
    }

    if (StringUtils.isNotBlank(csrf)) {
        Boolean hasKey = CHERRY.SPRING_CONTEXT.getBean(StringRedisTemplate.class).hasKey(CHERRY.REDIS_KEY_CSRF + csrf);
        isValid = hasKey == null ? false : hasKey;
    }

    return isValid;
}
 
Example #3
Source File: RedisCacheConfig.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * RedisTemplate配置
 * @param factory
 * @return
 */
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    StringRedisTemplate template = new StringRedisTemplate(factory);
    //定义key序列化方式
    //RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
    //定义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.setKeySerializer(redisSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example #4
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 #5
Source File: RedisOperationServiceTest.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAndDel() throws Exception{
	String key = "test12";
	SimpleRedisOperationService ops = redisOperationService;
	
	StringRedisTemplate stringRedisTemplate = ops.getStringRedisTemplate();
	
	String value = ops.getAndDel(key);
	assertThat(value).isNull();
	
	String testValue = "testValue";
	stringRedisTemplate.opsForValue().set(key, testValue);
	assertThat(stringRedisTemplate.opsForValue().get(key)).isEqualTo(testValue);
	
	String cacheKeyPrefix = redisOperationService.getCacheKeyPrefix();
	redisOperationService.setCacheKeyPrefix("");
	value = ops.getAndDelString(key);
	assertThat(value).isEqualTo(testValue);
	assertThat(stringRedisTemplate.opsForValue().get(key)).isNull();
	
	redisOperationService.setCacheKeyPrefix(cacheKeyPrefix);
}
 
Example #6
Source File: CacheService.java    From SpringBoot-Dubbo-Docker-Jenkins with Apache License 2.0 5 votes vote down vote up
/**
 * redisTemplate配置
 *
 * @param factory
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@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 #7
Source File: AbstractRedisUtil.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
public AbstractRedisUtil(StringRedisTemplate redisTemplate,
                         String prefix,
                         RedisSerializers redisSerializers) {
    this.redisTemplate = redisTemplate;
    this.prefix = prefix;
    this.redisSerializers = redisSerializers;
}
 
Example #8
Source File: RedisConfig.java    From SpringAll with MIT License 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
	StringRedisTemplate template = new StringRedisTemplate(factory);
	setSerializer(template);// 设置序列化工具
	template.afterPropertiesSet();
	return template;
}
 
Example #9
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #10
Source File: RedisConfig.java    From springboot-shiro-cas-mybatis with MIT License 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 #11
Source File: OAuth2Config.java    From Auth-service with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
        RedisConnectionFactory redisConnectionFactory)
        throws UnknownHostException {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #12
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #13
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #14
Source File: RedisConfig.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate StringRedisTemplate(RedisConnectionFactory connectionFactory) {
	StringRedisTemplate template = new StringRedisTemplate(connectionFactory);
	// 使用StringRedisSerializer来序列化和反序列化redis的key值
	template.setStringSerializer(StringRedisSerializer.UTF_8);
	template.setEnableTransactionSupport(true);
	template.afterPropertiesSet();
	return template;
}
 
Example #15
Source File: EmailRedisTemplateConfigurationTest.java    From spring-boot-email-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void createOrderingTemplate() throws Exception {
    //Act
    StringRedisTemplate template = emailRedisTemplateConfiguration.createOrderingTemplate();

    //Assert
    assertions.assertThat(template).isNotNull();
}
 
Example #16
Source File: RateLimiterAutoConfiguration.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public RedisRateLimiterClient redisRateLimiter(StringRedisTemplate redisTemplate,
											   Environment environment) {
	RedisScript<List<Long>> redisRateLimiterScript = redisRateLimiterScript();
	return new RedisRateLimiterClient(redisTemplate, redisRateLimiterScript, environment);
}
 
Example #17
Source File: RedisConfig.java    From spring-boot-redis-guava-caffeine-cache with Apache License 2.0 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
Source File: HealthService.java    From galeb with Apache License 2.0 5 votes vote down vote up
public void put(String message) {
    try {
        Assert.notNull(redisTemplate, StringRedisTemplate.class.getSimpleName() + " IS NULL");
        redisTemplate.opsForValue()
            .set(message, String.valueOf(System.currentTimeMillis()), REGISTER_TTL, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        JsonEventToLogger errorEvent = new JsonEventToLogger(this.getClass());
        errorEvent.put("short_message", "Error register healths");
        errorEvent.sendError(e);
    }
}
 
Example #19
Source File: RedisConfig.java    From FEBS-Security with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
        RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #20
Source File: RedisConfig.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
        RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}
 
Example #21
Source File: RedisConfig.java    From spring-boot-quickstart with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(
        RedisConnectionFactory redisConnectionFactory) {
    logger.info("init redis template...");
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
    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 #22
Source File: CacheConfig.java    From UserCenter with MIT License 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #23
Source File: RedisConfig.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
	StringRedisTemplate template = new StringRedisTemplate(factory);
	Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
			Object.class);
	ObjectMapper om = new ObjectMapper();
	om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
	jackson2JsonRedisSerializer.setObjectMapper(om);
	template.setKeySerializer(myStringSerializer);
	template.setHashKeySerializer(myStringSerializer);
	template.setValueSerializer(jackson2JsonRedisSerializer);
	template.afterPropertiesSet();
	return template;
}
 
Example #24
Source File: RedisConfig.java    From spring_boot with Apache License 2.0 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 #25
Source File: RedisConfiguration.java    From spring-boot-with-multi-redis with MIT License 5 votes vote down vote up
@Bean(name = "roleRedisTemplate")
public RedisTemplate roleRedisTemplate(@Qualifier("roleRedisConnectionFactory") RedisConnectionFactory cf) {
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
    stringRedisTemplate.setConnectionFactory(cf);
    setSerializer(stringRedisTemplate);
    return stringRedisTemplate;
}
 
Example #26
Source File: RedisConfig.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    log.info("构建 StringRedisTemplate 成功");
    return template;
}
 
Example #27
Source File: RedisConfiguration.java    From daming with Apache License 2.0 5 votes vote down vote up
@ConditionalOnMissingBean(BlockSendingRateLimitingHandler.class)
@Bean(name = "oneSendSmsVerificationCodeCommandInEveryXSeconds")
public BlockSendingRateLimitingHandler redisSendSmsVerificationCodeRateLimitingHandler(
        StringRedisTemplate redisTemplate, Clock clock, SmsVerificationCodeProperties properties) {
    BlockSendingRateLimitingHandler handler =
            new BlockSendingRateLimitingHandler(redisTemplate, clock);
    handler.setExpires(properties.getBlock());
    return handler;
}
 
Example #28
Source File: RoutersService.java    From galeb with Apache License 2.0 5 votes vote down vote up
@Autowired
public RoutersService(StringRedisTemplate redisTemplate, ChangesService changesService,
                      VersionService versionService, ConverterV1 converterV1, ConverterV2 converterV2,
                      EntityManagerFactory entityManagerFactory, LockerService lockerService) {
    this.redisTemplate = redisTemplate;
    this.changesService = changesService;
    this.versionService = versionService;
    this.converterV1 = converterV1;
    this.converterV2 = converterV2;
    this.entityManagerFactory = entityManagerFactory;
    this.lockerService = lockerService;
}
 
Example #29
Source File: CacheConfig.java    From microservice-recruit with Apache License 2.0 5 votes vote down vote up
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setKeySerializer(new PrefixRedisSerializer());
    template.setValueSerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
Example #30
Source File: RedisConfig.java    From storm_spring_boot_demo with MIT License 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;
}