Question

I am pretty new to Redis.

I downloaded Jedis and added that to my classpath. But, it doesnt provide a way to store java object as "value"

Am i missing something or Jedis doesn't provide the way to store java object as value?

Thanks, -Venkat

Était-ce utile?

La solution

There is no direct means - it can be done only via serialization and storing resultant byte-array. Pls refer http://static.springsource.org/spring-data/redis/docs/1.0.x/api/org/springframework/data/redis/serializer/package-summary.html if you want to use spring.

Cheers Muthu

Autres conseils

You can easily do it with Redis based framework for Java - Redisson:

RBucket<AnyObject> bucket = redisson.getBucket("anyObject");
// set an object
bucket.set(new AnyObject());
// get an object
AnyObject myObject = bucket.get();

// supports some useful functions like:
bucket.trySet(object);
bucket.compareAndSet(oldObject, newObject);
AnyObject prevObject = bucket.getAndSet(new AnyObject());

It handles serialization and work with connection so you don't need to deal with it each time when you need to send an object to Redis. Redisson does it for you. Work with Redis as you used to work with Java objects.

It supports many popular codecs (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization).

DISCLAMER: I'm a lead developer of Redisson

Storing java object as value isn't the redis way of doing stuff although you may accomplish what you want using serialization.

See this answer by Jedis developer : https://stackoverflow.com/a/12355876/2237351

As it says above, there is no direct way to do so, but you can implement it your self (example below use fastjson to do serialization, you could choose one yourself):

public static <T extends Serializable> T putObject(String key, T value, int expireTimeSecs) {
    if (expireTimeSecs < 0) {
        throw new IllegalArgumentException(String.format("Illegal expireTimeSecs = %s", expireTimeSecs));
    }
    try (Jedis jedis = POOL.getResource()) {
        String code;
        if (expireTimeSecs == 0) {
            code = jedis.set(key, JSON.toJSONString(value));
        } else {
            code = jedis.setex(key, expireTimeSecs, JSON.toJSONString(value));
        }
        if (!"OK".equalsIgnoreCase(code)) {
            throw new CacheException("Put object to redis failed!");
        }
    }
    return value;
}

public static <T extends Serializable> T putObject(String key, T value) {
    return putObject(key, value, 0);
}


public static <T extends Serializable> T getObject(String key, Class<T> clazz) {
    try (Jedis jedis = POOL.getResource()) {
        return JSON.parseObject(jedis.get(key), clazz);
    }
}

public static Object getObject(String key) {
    try (Jedis jedis = POOL.getResource()) {
        return JSON.parse(jedis.get(key));
    }
}

There is no direct way to store a Java object as value in redis, however one can store and get an java object as byte[] and the Object can be to/from converted to byte[] array using ByteBuffer.

This can be used to even reduce memory usage on redis if the object has numerical values.

// Allocating 9 bytes  
ByteBuffer buffer = ByteBuffer.allocate(9);  

// Storing first row: Hour > Minute > Count  
buffer.put((byte) 12);  
buffer.put((byte) 01);  
buffer.put((byte) 10);  




String key = "k";  

Jedis jedis = new Jedis("127.0.0.1");  
jedis.set(key.getBytes(), buffer.array());  

to get value of stored ByteBuffer in application and construct actual value thet was stored:

byte [] value= jedis.get(key.getBytes());  
        ByteBuffer valueBuffer = ByteBuffer.wrap(value);  

        System.out.println(valueBuffer.get()+","+valueBuffer.get()+","+valueBuffer.get());    

Read more about it here : ByteBuffer to get and set data on Apache Redis

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top