Converting a Map<String, String> to a Value type with Jedis

Naman :

So, I have a value type :

class Session {
    long createdAt;
    List<String> postIds;
}

Using the jedis client(3.0.0-m1 is that matters), I am currently performing an hset to create the entries and hgetAll to retrieve all the key-values:

private redis.clients.jedis.Jedis jedis;

void createSession(String idAsKey, Map<String, String> hashFieldValues) {
    jedis.hset(idAsKey, hashFieldValues);
}


Map<String, String> fetchSession(String idAsKey) {
    return jedis.hgetAll(idAsKey);
}

The challenge that I am currently looking at is the ease of converting the Map<String, String> into the Session object. Is there an existing way to do this?

Server response for an equivalent command

1) "createdAt"
2) "1556099708307"
3) "postIds"
4) "[a, b, c]"

PS: Starting to learn Redis, hoping this kind of mapping might have already been solved for. Yes, not looking for a client change as an answer at least.

Sripathi Krishnan :

Jedis doesn't offer a way to map objects to hash structures.

If you are using spring, then you can look at HashMappers. A HashMapper converts a POJO to a hash and vice-versa. In your case, the HashMapper will convert a Session to a hash, and the other way round.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86191&siteId=1