org.springframework.data.redis.serializer.RedisSerializer Java Examples

The following examples show how to use org.springframework.data.redis.serializer.RedisSerializer. 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 File: RedisConfig.java    From demo-project with MIT License 6 votes vote down vote up
@Bean(name = "redisTemplate")
@SuppressWarnings("all")
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    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);

    // 设置RedisTemplate
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    RedisSerializer stringSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(serializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(serializer);
    template.afterPropertiesSet();
    return template;
}
 
Example #2
Source File: RedisConfig.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean(name="redisTemplateHash")  
@Scope("prototype")
   public RedisTemplate redisTemplateHash(){  
	RedisSerializer valueSerializer = new GsonRedisSerializer(Object.class);
	RedisSerializer keySerializer = new StringRedisSerializer();

       RedisTemplate redisTemplate = new RedisTemplate();    
        
       redisTemplate.setConnectionFactory(jedisConnectionFactory());
       redisTemplate.setKeySerializer(new StringRedisSerializer());  
       redisTemplate.setValueSerializer(new StringRedisSerializer());  
       redisTemplate.setHashKeySerializer(keySerializer);  
       redisTemplate.setHashValueSerializer(valueSerializer);  
       redisTemplate.afterPropertiesSet();  
       redisTemplate.setEnableTransactionSupport(true);  
         
       return redisTemplate;    
   }
 
Example #3
Source File: RedisCacheConfig.java    From syhthems-platform with MIT License 6 votes vote down vote up
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Bean
public CacheManager JDKCacheManager() {
    final RedisCacheWriter redisCacheWriter =
            RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
    final RedisCacheConfiguration defaultCacheConfig =
            RedisCacheConfiguration.defaultCacheConfig()

                    // 不缓存 null 值
                    .disableCachingNullValues()
                    // 使用注解时的序列化、反序列化对
                    .serializeKeysWith(stringPair)
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.java()))
                    .prefixKeysWith("syhthems:cache:");
    return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
 
Example #4
Source File: UserDao.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 批量新增 使用pipeline方式
 */
@Override
public boolean add(final List<UserDTO> list) {
	Assert.notEmpty(list);
	boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
		@Override
		public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
			RedisSerializer<String> serializer = getRedisSerializer();
			for (UserDTO userDTO : list) {
				byte[] key = serializer.serialize(userDTO.getId());
				byte[] name = serializer.serialize(userDTO.getName());
				connection.setNX(key, name);
			}
			return true;
		}
	}, false, true);
	return result;
}
 
Example #5
Source File: RedisClient.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 更新缓存中的对象,也可以在redis缓存中存入新的对象
 *
 * @param key
 * @param t
 * @param <T>
 */
public <T> void set(String key, T t) {
    byte[] keyBytes = getKey(key);
    RedisSerializer serializer = redisTemplate.getValueSerializer();
    byte[] val = serializer.serialize(t);
    RedisConnection redisConnection = getConnection();

    if(null!=redisConnection){
        try {
            redisConnection.set(keyBytes, val);
        }finally {
            releaseConnection(redisConnection);
        }
    }else{
        logger.error("1. can not get valid connection");
    }
}
 
Example #6
Source File: RedisClient.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 更新缓存中的对象,也可以在redis缓存中存入新的对象
 *
 * @param key
 * @param t
 * @param <T>
 */
public <T> void set(String key, T t) {
    byte[] keyBytes = getKey(key);
    RedisSerializer serializer = redisTemplate.getValueSerializer();
    byte[] val = serializer.serialize(t);
    RedisConnection redisConnection = getConnection();

    if(null!=redisConnection){
        try {
            redisConnection.set(keyBytes, val);
        }finally {
            releaseConnection(redisConnection);
        }
    }else{
        logger.error("1. can not get valid connection");
    }
}
 
Example #7
Source File: JsonValueRedisTemplate.java    From onetwo with Apache License 2.0 6 votes vote down vote up
/***
	 * 构造后需要调用setConnectionFactory, afterPropertiesSet
	 * @param valueType
	 */
	public JsonValueRedisTemplate(Class<T> valueType) {
//		this.valueType = (Class<T>)ReflectUtils.getGenricType(getClass(), 0);
		this.valueType = valueType;
		RedisSerializer<String> keySerializer = new StringRedisSerializer();
		RedisSerializer<?> valueSerializer = null;
		if (valueType==null || valueType==Object.class) {
			valueSerializer = RedisUtils.typingJackson2RedisSerializer();
		} else {
			valueSerializer = new Jackson2JsonRedisSerializer<T>(valueType);
		}
		setKeySerializer(keySerializer);
		setValueSerializer(valueSerializer);
		setHashKeySerializer(keySerializer);
		setHashValueSerializer(valueSerializer);
	}
 
Example #8
Source File: TarocoRedisRepository.java    From Taroco with Apache License 2.0 6 votes vote down vote up
/**
 * 根据key获取对象
 *
 * @param keyPatten the key patten
 * @return the keys values
 */
public Map<String, String> getKeysValues(final String keyPatten) {
    log.debug("[redisTemplate redis]  getValues()  patten={} ", keyPatten);
    return redisTemplate.execute((RedisCallback<Map<String, String>>) connection -> {
        RedisSerializer<String> serializer = getRedisSerializer();
        Map<String, String> maps = new HashMap<>(16);
        Set<String> keys = redisTemplate.keys(keyPatten + "*");
        if (CollUtil.isNotEmpty(keys)) {
            for (String key : keys) {
                byte[] bKeys = serializer.serialize(key);
                byte[] bValues = connection.get(bKeys);
                String value = serializer.deserialize(bValues);
                maps.put(key, value);
            }
        }
        return maps;
    });
}
 
Example #9
Source File: EmailRedisTemplateConfiguration.java    From spring-boot-email-tools with Apache License 2.0 6 votes vote down vote up
@Bean
@Qualifier("valueTemplate")
public RedisTemplate<String, EmailSchedulingData> createValueTemplate() throws IOException {
    RedisTemplate<String, EmailSchedulingData> template = new RedisTemplate<>();
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(jdkSerializationRedisSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(stringSerializer);

    template.setConnectionFactory(redisConnectionFactory);
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
}
 
Example #10
Source File: RedisConfig.java    From springboot_cwenao with MIT License 6 votes vote down vote up
@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;
}
 
Example #11
Source File: RateLimiterPluginDataHandler.java    From soul with Apache License 2.0 6 votes vote down vote up
@Override
public void handlerPlugin(final PluginData pluginData) {
    if (Objects.nonNull(pluginData) && pluginData.getEnabled()) {
        //init redis
        RateLimiterConfig rateLimiterConfig = GsonUtils.getInstance().fromJson(pluginData.getConfig(), RateLimiterConfig.class);
        //spring data redisTemplate
        if (Objects.isNull(Singleton.INST.get(ReactiveRedisTemplate.class))
                || Objects.isNull(Singleton.INST.get(RateLimiterConfig.class))
                || !rateLimiterConfig.equals(Singleton.INST.get(RateLimiterConfig.class))) {
            LettuceConnectionFactory lettuceConnectionFactory = createLettuceConnectionFactory(rateLimiterConfig);
            lettuceConnectionFactory.afterPropertiesSet();
            RedisSerializer<String> serializer = new StringRedisSerializer();
            RedisSerializationContext<String, String> serializationContext =
                    RedisSerializationContext.<String, String>newSerializationContext().key(serializer).value(serializer).hashKey(serializer).hashValue(serializer).build();
            ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(lettuceConnectionFactory, serializationContext);
            Singleton.INST.single(ReactiveRedisTemplate.class, reactiveRedisTemplate);
            Singleton.INST.single(RateLimiterConfig.class, rateLimiterConfig);
        }
    }
}
 
Example #12
Source File: RedisConfig.java    From springboot_cwenao with MIT License 6 votes vote down vote up
@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;
}
 
Example #13
Source File: UserDao.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 通过key获取
 */
@Override
public UserDTO get(final String keyId) {
	UserDTO result = redisTemplate.execute(new RedisCallback<UserDTO>() {
		@Override
		public UserDTO doInRedis(RedisConnection connection) throws DataAccessException {
			RedisSerializer<String> serializer = getRedisSerializer();
			byte[] key = serializer.serialize(keyId);
			byte[] value = connection.get(key);
			if (value == null) {
				return null;
			}
			String name = serializer.deserialize(value);
			return new UserDTO(keyId, name, null);
		}
	});
	return result;
}
 
Example #14
Source File: RedisConfig.java    From spring-music with Apache License 2.0 6 votes vote down vote up
@Bean
public RedisTemplate<String, Album> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Album> template = new RedisTemplate<>();

    template.setConnectionFactory(redisConnectionFactory);

    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    RedisSerializer<Album> albumSerializer = new Jackson2JsonRedisSerializer<>(Album.class);

    template.setKeySerializer(stringSerializer);
    template.setValueSerializer(albumSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setHashValueSerializer(albumSerializer);

    return template;
}
 
Example #15
Source File: RedisRepository.java    From SpringBoot2.0 with Apache License 2.0 6 votes vote down vote up
/**
 * 根据key获取对象
 *
 * @param keyPatten the key patten
 * @return the keys values
 */
public Map<String, Object> getKeysValues(final String keyPatten) {
    log.debug("[redisTemplate redis]  getValues()  patten={} ", keyPatten);
    return redisTemplate.execute((RedisCallback<Map<String, Object>>) connection -> {
        RedisSerializer<String> serializer = getRedisSerializer();
        Map<String, Object> maps = new HashMap<>(16);
        Set<String> keys = redisTemplate.keys(keyPatten + "*");
        if (!CollectionUtils.isEmpty(keys)) {
            for (String key : keys) {
                byte[] bKeys = serializer.serialize(key);
                byte[] bValues = connection.get(bKeys);
                Object value = OBJECT_SERIALIZER.deserialize(bValues);
                maps.put(key, value);
            }
        }
        return maps;
    });
}
 
Example #16
Source File: RedisCacheAutoConfiguration.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 重新配置一个RedisTemplate
 *
 * @param factory
 * @return
 */
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(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<String> stringSerializer = new StringRedisSerializer();
    // key采用String的序列化方式
    template.setKeySerializer(stringSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);
    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.setDefaultSerializer(jackson2JsonRedisSerializer);
    return template;
}
 
Example #17
Source File: DistributedLock.java    From common-project with Apache License 2.0 6 votes vote down vote up
public boolean lock(String key, long time, TimeUnit timeUnit){
    List<Boolean> results = redisTemplate.executePipelined(new RedisCallback<Boolean>() {

        RedisSerializer keySerializer = redisTemplate.getKeySerializer();

        RedisSerializer valueSerializer = redisTemplate.getValueSerializer();

        @Override
        public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
            redisConnection.openPipeline();
            byte[] keySerialize = keySerializer.serialize(key);
            byte[] valueSerialize = valueSerializer.serialize(System.currentTimeMillis());
            redisConnection.setNX(keySerialize, valueSerialize);
            long l = timeUnit.toSeconds(time);
            redisConnection.expire(keySerialize, l);
            return null;
        }
    });
    for (Boolean result :results) {
        if (!result){
            return false;
        }
    }
    return true;
}
 
Example #18
Source File: RedisTemplateConfiguration.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * value 值 序列化
 *
 * @return RedisSerializer
 */
@Bean
@ConditionalOnMissingBean(RedisSerializer.class)
public RedisSerializer<Object> redisSerializer(MicaRedisProperties properties,
											   ObjectProvider<ObjectMapper> objectProvider) {
	MicaRedisProperties.SerializerType serializerType = properties.getSerializerType();
	if (MicaRedisProperties.SerializerType.JDK == serializerType) {
		return new JdkSerializationRedisSerializer();
	}
	// jackson findAndRegisterModules,use copy
	ObjectMapper objectMapper = objectProvider.getIfAvailable(ObjectMapper::new).copy();
	// findAndRegisterModules
	objectMapper.findAndRegisterModules();
	// class type info to json
	GenericJackson2JsonRedisSerializer.registerNullValueSerializer(objectMapper, null);
	objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), DefaultTyping.NON_FINAL, As.PROPERTY);
	return new GenericJackson2JsonRedisSerializer(objectMapper);
}
 
Example #19
Source File: MicaRedisCacheAutoConfiguration.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
MicaRedisCacheAutoConfiguration(RedisSerializer<Object> redisSerializer,
								CacheProperties cacheProperties,
								CacheManagerCustomizers customizerInvoker,
								ObjectProvider<RedisCacheConfiguration> redisCacheConfiguration) {
	this.redisSerializer = redisSerializer;
	this.cacheProperties = cacheProperties;
	this.customizerInvoker = customizerInvoker;
	this.redisCacheConfiguration = redisCacheConfiguration.getIfAvailable();
}
 
Example #20
Source File: TarocoRedisRepository.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 根据key获取对象
 *
 * @param key the key
 * @return the string
 */
public String get(final String key) {
    String resultStr = redisTemplate.execute((RedisCallback<String>) connection -> {
        RedisSerializer<String> serializer = getRedisSerializer();
        byte[] keys = serializer.serialize(key);
        byte[] values = connection.get(keys);
        return serializer.deserialize(values);
    });
    log.debug("[redisTemplate redis]取出 缓存  url:{} ", key);
    return resultStr;
}
 
Example #21
Source File: RedisServiceImpl.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
@Override
public long lpush(final String key, Object obj) {
    final String value = JSON.toJSONString(obj);
    long result = redisTemplate.execute(new RedisCallback<Long>() {
        @Override
        public Long doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));
            return count;
        }
    });
    return result;
}
 
Example #22
Source File: RedisServiceImpl.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
@Override
public long del(final String key){
    long result = redisTemplate.execute(new RedisCallback<Long>() {
        @Override
        public Long doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            long value =  connection.del(serializer.serialize(key));
            return value;
        }
    });
    return result;
}
 
Example #23
Source File: RedisServiceImpl.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean set(final String key, final String value) {
    boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            connection.set(serializer.serialize(key), serializer.serialize(value));
            return true;
        }
    });
    return result;
}
 
Example #24
Source File: RedisServiceImpl.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
@Override
public String lpop(final String key) {
    String result = redisTemplate.execute(new RedisCallback<String>() {
        @Override
        public String doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            byte[] res =  connection.lPop(serializer.serialize(key));
            return serializer.deserialize(res);
        }
    });
    return result;
}
 
Example #25
Source File: RedisRepository.java    From SpringBoot2.0 with Apache License 2.0 5 votes vote down vote up
/**
 * 根据key获取对象
 *
 * @param key the key
 * @return the string
 */
public Object get(final String key) {
    Object resultStr = redisTemplate.execute((RedisCallback<Object>) connection -> {
        RedisSerializer<String> serializer = getRedisSerializer();
        byte[] keys = serializer.serialize(key);
        byte[] values = connection.get(keys);
        return OBJECT_SERIALIZER.deserialize(values);
    });
    log.debug("[redisTemplate redis]取出 缓存  url:{} ", key);
    return resultStr;
}
 
Example #26
Source File: TarocoRedisRepository.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 一次性添加数组到   过期时间的  缓存,不用多次连接,节省开销
 *
 * @param keys   redis主键数组
 * @param values 值数组
 * @param time   过期时间
 */
public void setExpire(final String[] keys, final String[] values, final long time) {
    redisTemplate.execute((RedisCallback<Long>) connection -> {
        RedisSerializer<String> serializer = getRedisSerializer();
        for (int i = 0; i < keys.length; i++) {
            byte[] bKeys = serializer.serialize(keys[i]);
            byte[] bValues = serializer.serialize(values[i]);
            connection.set(bKeys, bValues);
            connection.expire(bKeys, time);
            log.debug("[redisTemplate redis]放入 缓存  url:{} ========缓存时间为:{}秒", keys[i], time);
        }
        return 1L;
    });
}
 
Example #27
Source File: TarocoRedisRepository.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 添加到带有 过期时间的  缓存
 *
 * @param key   redis主键
 * @param value 值
 * @param time  过期时间
 */
public void setExpire(final String key, final String value, final long time) {
    redisTemplate.execute((RedisCallback<Long>) connection -> {
        RedisSerializer<String> serializer = getRedisSerializer();
        byte[] keys = serializer.serialize(key);
        byte[] values = serializer.serialize(value);
        connection.set(keys, values);
        connection.expire(keys, time);
        log.debug("[redisTemplate redis]放入 缓存  url:{} ========缓存时间为{}秒", key, time);
        return 1L;
    });
}
 
Example #28
Source File: JsonRedisTemplate.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public JsonRedisTemplate() {
		JsonMapper jsonMapper = JsonMapper.ignoreNull();
		ObjectMapper mapper = jsonMapper.getObjectMapper();
		mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);//用这个配置,如果写入的对象是list,并且元素是复合对象时,会抛错:Current context not Object but Array
//		mapper.enableDefaultTyping(DefaultTyping.JAVA_LANG_OBJECT);
		
//		RedisSerializer<String> keySerializer = new StringRedisSerializer();
		RedisSerializer<Object> keySerializer = new GenericJackson2JsonRedisSerializer(mapper);
		RedisSerializer<Object> valueSerializer = new GenericJackson2JsonRedisSerializer(mapper);
		setKeySerializer(keySerializer);
		setValueSerializer(valueSerializer);
		setHashKeySerializer(keySerializer);
		setHashValueSerializer(valueSerializer);
	}
 
Example #29
Source File: RedisRepository.java    From SpringBoot2.0 with Apache License 2.0 5 votes vote down vote up
/**
 * 对某个主键对应的值加一,value值必须是全数字的字符串
 *
 * @param key the key
 * @return the long
 */
public long incr(final String key) {
    return redisTemplate.execute((RedisCallback<Long>) connection -> {
        RedisSerializer<String> redisSerializer = getRedisSerializer();
        return connection.incr(redisSerializer.serialize(key));
    });
}
 
Example #30
Source File: J2CacheSpringRedisAutoConfiguration.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") RedisConnectionFactory j2CahceRedisConnectionFactory,
		@Qualifier("j2CacheValueSerializer") RedisSerializer<Object> j2CacheSerializer) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(j2CacheSerializer);
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}