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 Project: framework   Author: gyoomi   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   Author: sengeiou   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   Author: FleyX   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   Author: wayshall   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   Author: homeyanmi   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   Author: LiHaodong888   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
Source Project: mica   Author: lets-mica   File: RateLimiterAutoConfiguration.java    License: 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 #8
Source Project: Auth-service   Author: keets2012   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
Source Project: ShedLock   Author: lukas-krecan   File: AbstractRedisLockProviderIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
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   Author: stalary   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   Author: stalary   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   Author: hsj-xiaokang   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
Source Project: SpringBoot-Dubbo-Docker-Jenkins   Author: bz51   File: CacheService.java    License: 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 #14
Source Project: beihu-boot   Author: beihu-stack   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   Author: wuyouzhuguli   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   Author: stalary   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   Author: Frodez   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
Source Project: spring-boot-email-tools   Author: ozimov   File: EmailRedisTemplateConfigurationTest.java    License: 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 #19
Source Project: galeb   Author: galeb   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   Author: febsteam   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
Source Project: spring-boot-redis-guava-caffeine-cache   Author: zheng-zy   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 #22
Source Project: spring-boot-quickstart   Author: fireshort   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   Author: stalary   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   Author: xuyisu   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   Author: yx726843014   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   Author: idreamshen   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   Author: SophiaLeo   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   Author: TheBund1st   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   Author: galeb   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   Author: stalary   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;
}