Java Code Examples for org.springframework.data.redis.serializer.RedisSerializer#serialize()

The following examples show how to use org.springframework.data.redis.serializer.RedisSerializer#serialize() . 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: 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 2
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 3
Source File: SerializerUtils.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public static int getValueSize(RedisSerializer serializer, Object[] args) {
    try {
        if (args == null || args.length == 0) {
            return 0;
        }
        return serializer.serialize(args).length;
    } catch (Exception e) {
        return 0;
    }
}
 
Example 4
Source File: JsonRedisTemplateTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedisSerializer(){
	RedisSerializer<Object> valueSerializer = new GenericJackson2JsonRedisSerializer();
	JsonData data = new JsonData();
	data.setAge(111);
	data.setName("testName");
	byte[] bytes = valueSerializer.serialize(data);
	Object res = valueSerializer.deserialize(bytes);
	assertThat(res.getClass()).isEqualTo(data.getClass());
}
 
Example 5
Source File: RedisClient.java    From blog_demos with Apache License 2.0 2 votes vote down vote up
/**
 * 获取缓存的key
 * @param id
 * @return
 */
private <T> byte[] getKey(T id) {
    RedisSerializer serializer = redisTemplate.getKeySerializer();
    return serializer.serialize(id);
}
 
Example 6
Source File: RedisClient.java    From blog_demos with Apache License 2.0 2 votes vote down vote up
/**
 * 获取缓存的key
 * @param id
 * @return
 */
private <T> byte[] getKey(T id) {
    RedisSerializer serializer = redisTemplate.getKeySerializer();
    return serializer.serialize(id);
}