本文整理匯總了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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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());
}
示例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;
}
示例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;
}
示例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;
}