使用 Jedis 将 Map<String, String> 转换为值类型

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

所以,我有一个值类型:

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

使用 jedis 客户端(3.0.0-m1 很重要),我目前正在执行 hset 来创建条目和 hgetAll检索所有键值:

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);
}

我目前面临的挑战是如何轻松地将 Map<String, String> 转换为 Session 对象。有现成的方法吗?

等效命令的服务器响应

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

PS:开始学习Redis,希望这种映射已经解决了。是的,至少不是寻找客户变更作为答案。

Jedis 不提供将对象映射到哈希结构的方法。

如果你正在使用spring,那么你可以看看HashMappers。 HashMapper 将 POJO 转换为散列,反之亦然。在您的情况下,HashMapper 会将 Session 转换为散列,反之亦然。

你可以convert the map to POJO

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

因此您不需要特殊处理,只需使用映射器库作为 Jackson-Databind

您可以像下面这样在 Redis 中保存和获取数据:

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;
    }

现在从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;

    }

您不是单独使用这些字段,而是同时使用。因此,我建议您使用简单明了的 Redis Strings 而不是使用 Redis 哈希。因此,您将使用 set 来保存条目并使用 get 来检索它们。

采用以上建议,您的代码可能会变成这样:

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;
}

注意:为了serialization/deserialization,我使用了Gson。不用说,你可以使用任何库。