本文整理汇总了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;
}