3

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.

Naman
  • 27,789
  • 26
  • 218
  • 353

4 Answers4

3

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.

Sripathi Krishnan
  • 30,948
  • 4
  • 76
  • 83
3

You are not using the fields separately, but simultaneously. Because of that, I'd suggest you to use plain and simple Redis Strings instead of using Redis Hashes. So you'd be using set to save entries and get to retrieve them.

Using above suggestions, your code may become as follows:

private redis.clients.jedis.Jedis jedis;
private com.google.gson.Gson gson; // see Note

void createSession(String idAsKey, Session object) {
    String serializedValue = gson.toJson(object);
    jedis.set(idAsKey, serializedValue);
}

Session fetchSession(String idAsKey) {
    String serializedValue = jedis.get(idAsKey);
    Session deserializedObject = gson.fromJson(serializedValue, Session.class);
    return deserializedObject;
}

Note: I have used Gson for the purpose of serialization/deserialization. Needless to say, you can use any library.

sazzad
  • 5,740
  • 6
  • 25
  • 42
2

You can convert the map to POJO

Session session = new ObjectMapper().convertValue(map, Session.class);

So you don't need a special handling expect using a mapper library as Jackson-Databind

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

You can save and fetch the data to and from Redis like below:

public Map<String, Object> saveDataInRedis(String id, Object obj) {
        Map<String, Object> result = new HashMap<>();
        String jsonObj = "";
        try {
            jsonObj = objectMapper.writeValueAsString(obj);
            System.out.println(jsonObj);
        } catch (JsonProcessingException jpe) {
            logger.warn("In saveDataInRedis Exception :: "+jpe);
        }
        try {
            valOps.set(id, jsonObj);
            result.put(DataConstants.IS_SUCCESS, true);
            result.put(DataConstants.MESSAGE, "Data saved succesfully in redis");
        }catch(RedisConnectionFailureException e){
            result =null;
            logger.warn("In saveDataInRedis Exception e :: "+e);
        }
        System.out.println(valOps.getOperations().getClass());
        System.out.println(jedisConnectionFactory.getPoolConfig().getMaxTotal());
        return result;
    }

Now get data from redis:

public Map<String, Object> getDataFromRedis(String id) {
        Map<String, Object> result = new HashMap<>();
        String jsonObj = valOps.get(id);
        System.out.println("jsonObj :: " + jsonObj);
        Session obj = null;
        try {
            obj = (Session) objectMapper.readValue(jsonObj, Session.class);
        } catch (Exception e) {
            result.put("data", null);
            logger.warn("Data from redis is deleted");
            logger.warn("In getDataFromRedis Exception e :: "+e);
        }

        if (obj != null) {
            result.put(DataConstants.IS_SUCCESS, true);
            result.put("data", obj);
        }
        System.out.println("result :: " + result);
        return result;

    }
Pawan Tiwari
  • 518
  • 6
  • 26