26

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

Venkat Teki
  • 2,233
  • 4
  • 21
  • 28

5 Answers5

16

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 maintains internal connection pool 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

Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71
  • For java objects to be serialized by redisson, should the class be defined as Serializable? Or should we do something similar? Also, I don't completely understand the codec support. Does Radisson give me an option to configure the serialization mode ( Avro or Kyro etc) ? – Anirudh Jayakumar Sep 20 '17 at 20:41
  • 1
    @AnirudhJayakumar For some codecs object should implement Serializable and shouldn't for others. Here is a [full list](https://github.com/redisson/redisson/wiki/4.-data-serialization) of supported codecs. You able to define codec through `config.setCodec` method – Nikita Koksharov Sep 20 '17 at 20:50
  • 3
    You should make a disclamer that you are the founder and lead developer of redisson – Arthur Kalimullin Sep 24 '19 at 10:26
  • Should we use a store database connection java object in Redis? – Shubham Khandare Jun 19 '20 at 14:20
9

There is no direct means - it can be done only via serialization and storing resultant byte-array. Please 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.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Muthukumaran
  • 106
  • 1
  • 2
    What if the object is third party and non serializable – User3 Sep 13 '17 at 11:42
  • @User3 in that case, you'd have to extract data from that 3rd party object and copy it back to your type – asgs Jan 16 '18 at 10:49
  • You can use ByteBuffer as well to/from conversion in Java and than converting to/from ByteBuffer to byte[] array to be store/get from redis. – neel4soft Jan 21 '19 at 08:11
7

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

Community
  • 1
  • 1
deepakborania
  • 166
  • 2
  • 8
0

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));
    }
}
Qy Zuo
  • 2,622
  • 24
  • 21
0

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

neel4soft
  • 507
  • 1
  • 4
  • 12