當前位置: 首頁>>代碼示例>>Java>>正文


Java RedisTemplate類代碼示例

本文整理匯總了Java中org.springframework.data.redis.core.RedisTemplate的典型用法代碼示例。如果您正苦於以下問題:Java RedisTemplate類的具體用法?Java RedisTemplate怎麽用?Java RedisTemplate使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


RedisTemplate類屬於org.springframework.data.redis.core包,在下文中一共展示了RedisTemplate類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;
}
 
開發者ID:cwenao,項目名稱:springboot_cwenao,代碼行數:25,代碼來源:RedisConfig.java

示例2: buildRedisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
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;
}
 
開發者ID:Paleozoic,項目名稱:storm_spring_boot_demo,代碼行數:18,代碼來源:RedisConfUtils.java

示例3: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;
}
 
開發者ID:allianzit,項目名稱:ait-platform,代碼行數:27,代碼來源:AitCacheConfigBase.java

示例4: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
/**
 * 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;
}
 
開發者ID:SnailFastGo,項目名稱:springboot_op,代碼行數:22,代碼來源:RedisConfiguration.java

示例5: cacheManager

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
    //設置緩存過期時間
    //rcm.setDefaultExpiration(60);//秒
    return rcm;
}
 
開發者ID:ChinaLHR,項目名稱:JavaQuarkBBS,代碼行數:9,代碼來源:RedisConfig.java

示例6: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;
}
 
開發者ID:duanyaxin,項目名稱:springboot-smart,代碼行數:17,代碼來源:RedisConfiguration.java

示例7: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;
}
 
開發者ID:fanqinghui,項目名稱:action,代碼行數:20,代碼來源:RedisUtil.java

示例8: cacheManager

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@Bean
public CacheManager cacheManager ( RedisTemplate redisTemplate ) {
	RedisCacheManager redisCacheManager = new RedisCacheManager( redisTemplate );
	// 默認30分鍾
	redisCacheManager.setDefaultExpiration( defaultExpiration );
	redisCacheManager.setExpires( expires );
	redisCacheManager.setUsePrefix( usePrefix );
	return redisCacheManager;
}
 
開發者ID:yujunhao8831,項目名稱:spring-boot-start-current,代碼行數:10,代碼來源:SpringCacheConfig.java

示例9: createAttributeCache

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
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);
}
 
開發者ID:eclipse,項目名稱:keti,代碼行數:17,代碼來源:AttributeCacheFactory.java

示例10: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@Bean(name = "redisTemplate")
public RedisTemplate<String,?> redisTemplate(@Qualifier("mafConnectionFactory") RedisConnectionFactory factory) {
    RedisTemplate<String,?> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    return template;
}
 
開發者ID:slking1987,項目名稱:mafia,代碼行數:9,代碼來源:RedisConfig.java

示例11: getStringRedisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@Bean
public RedisTemplate getStringRedisTemplate() {
    StringRedisTemplate clusterTemplate = new StringRedisTemplate();
    clusterTemplate.setConnectionFactory(jedisConnectionFactory());
    clusterTemplate.setKeySerializer(new StringRedisSerializer());
    clusterTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    return clusterTemplate;
}
 
開發者ID:yu199195,項目名稱:happylifeplat-transaction,代碼行數:9,代碼來源:RedisClusterConfig.java

示例12: test

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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());
}
 
開發者ID:tomoncle,項目名稱:JavaStudy,代碼行數:14,代碼來源:Test.java

示例13: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;  
   }
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:15,代碼來源:RedisConfig.java

示例14: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;
}
 
開發者ID:davidgamez,項目名稱:parrot-rest,代碼行數:9,代碼來源:ApplicationTestConfiguration.java

示例15: redisTemplate

import org.springframework.data.redis.core.RedisTemplate; //導入依賴的package包/類
@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;
}
 
開發者ID:ChinaLHR,項目名稱:JavaQuarkBBS,代碼行數:13,代碼來源:RedisConfig.java


注:本文中的org.springframework.data.redis.core.RedisTemplate類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。