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

The following examples show how to use org.springframework.data.redis.core.StringRedisTemplate. These examples are extracted from open source projects. 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 Project: framework   Source File: CsrfFilter.java    License: 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 2
Source Project: ZTuoExchange_framework   Source File: RedisCacheConfig.java    License: 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 3
Source Project: demo-project   Source File: RedisConfig.java    License: 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 4
Source Project: onetwo   Source File: RedisOperationServiceTest.java    License: 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 5
Source Project: ZTuoExchange_framework   Source File: RedisCacheConfig.java    License: 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 6
Source Project: SpringBootLearn   Source File: RedisConfig.java    License: 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 7
@Bean
@ConditionalOnMissingBean
public RedisRateLimiterClient redisRateLimiter(StringRedisTemplate redisTemplate,
											   Environment environment) {
	RedisScript<List<Long>> redisRateLimiterScript = redisRateLimiterScript();
	return new RedisRateLimiterClient(redisTemplate, redisRateLimiterScript, environment);
}
 
Example 8
Source Project: Auth-service   Source File: OAuth2Config.java    License: 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 9
public AbstractRedisLockProviderIntegrationTest(RedisConnectionFactory connectionFactory) {
    lockProvider = new RedisLockProvider.Builder(connectionFactory)
        .environment(ENV)
        .keyPrefix(KEY_PREFIX)
        .build();

    redisTemplate = new StringRedisTemplate(connectionFactory);
}
 
Example 10
Source Project: microservice-recruit   Source File: CacheConfig.java    License: 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 11
Source Project: microservice-recruit   Source File: CacheConfig.java    License: 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 12
Source Project: springboot-shiro-cas-mybatis   Source File: RedisConfig.java    License: 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 13
/**
 * 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 14
Source Project: beihu-boot   Source File: AbstractRedisUtil.java    License: 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 15
Source Project: SpringAll   Source File: RedisConfig.java    License: 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 16
Source Project: microservice-recruit   Source File: CacheConfig.java    License: 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 17
Source Project: BlogManagePlatform   Source File: RedisConfig.java    License: 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 18
@Test
public void createOrderingTemplate() throws Exception {
    //Act
    StringRedisTemplate template = emailRedisTemplateConfiguration.createOrderingTemplate();

    //Assert
    assertions.assertThat(template).isNotNull();
}
 
Example 19
Source Project: galeb   Source File: HealthService.java    License: 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 20
Source Project: FEBS-Security   Source File: RedisConfig.java    License: 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
@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 22
Source Project: spring-boot-quickstart   Source File: RedisConfig.java    License: 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 23
Source Project: UserCenter   Source File: CacheConfig.java    License: 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 24
Source Project: fw-spring-cloud   Source File: RedisConfig.java    License: 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 25
Source Project: spring_boot   Source File: RedisConfig.java    License: 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 26
Source Project: spring-boot-with-multi-redis   Source File: RedisConfiguration.java    License: 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 27
Source Project: sophia_scaffolding   Source File: RedisConfig.java    License: 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 28
Source Project: daming   Source File: RedisConfiguration.java    License: 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 29
Source Project: galeb   Source File: RoutersService.java    License: 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 30
Source Project: microservice-recruit   Source File: CacheConfig.java    License: 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;
}