Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换地图<;字符串,字符串>;到绝地武士的值类型_Java_Redis_Objectmapper_Jedis_Value Type - Fatal编程技术网

Java 转换地图<;字符串,字符串>;到绝地武士的值类型

Java 转换地图<;字符串,字符串>;到绝地武士的值类型,java,redis,objectmapper,jedis,value-type,Java,Redis,Objectmapper,Jedis,Value Type,因此,我有一个值类型: class Session { long createdAt; List<String> postIds; } 我目前面临的挑战是如何将映射转换为会话对象。有没有现成的方法可以做到这一点 等效命令的服务器响应 PS:开始学习Redis,希望这种映射可能已经解决了。是的,至少不要将客户端更改作为答案。绝地武士没有提供将对象映射到散列结构的方法 如果您使用的是spring,那么您可以查看。哈希映射器将POJO转换为哈希,反之亦然。在您的情况下,H

因此,我有一个值类型:

class Session {
    long createdAt;
    List<String> postIds;
}
我目前面临的挑战是如何将
映射
转换为
会话
对象。有没有现成的方法可以做到这一点

等效命令的服务器响应


PS:开始学习Redis,希望这种映射可能已经解决了。是的,至少不要将客户端更改作为答案。

绝地武士没有提供将对象映射到散列结构的方法

如果您使用的是spring,那么您可以查看。哈希映射器将POJO转换为哈希,反之亦然。在您的情况下,HashMapper将会话转换为哈希,反之亦然。

您可以


因此,除了使用mapper库之外,您不需要特殊的处理,因为您可以在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;
    }
publicmap saveDataInRedis(字符串id,对象obj){
映射结果=新的HashMap();
字符串jsonObj=“”;
试一试{
jsonObj=objectMapper.writeValueAsString(obj);
System.out.println(jsonObj);
}捕获(JsonProcessingException jpe){
logger.warn(“在saveDataInRedis异常中::”+jpe);
}
试一试{
valOps.set(id,jsonObj);
result.put(DataConstants.IS_SUCCESS,true);
result.put(DataConstants.MESSAGE,“数据成功保存在redis中”);
}捕获(重新连接失败异常e){
结果=空;
logger.warn(“在saveDataInRedis异常e::”+e中);
}
System.out.println(valOps.getOperations().getClass());
System.out.println(jedisConnectionFactory.getPoolConfig().getMaxTotal());
返回结果;
}
现在从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;

    }
公共映射getDataFromRedis(字符串id){ 映射结果=新的HashMap(); 字符串jsonObj=valOps.get(id); System.out.println(“jsonObj::”+jsonObj); 会话obj=null; 试一试{ obj=(Session)objectMapper.readValue(jsonObj,Session.class); }捕获(例外e){ 结果。put(“数据”,null); logger.warn(“来自redis的数据被删除”); logger.warn(“在getDataFromRedis异常e::”+e中); } 如果(obj!=null){ result.put(DataConstants.IS_SUCCESS,true); 结果。输入(“数据”,obj); } System.out.println(“结果::”+result); 返回结果; }
您不是单独使用字段,而是同时使用字段。因此,我建议您使用plain和simple,而不是使用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;
}
注意:我使用的用于序列化/反序列化。不用说,你可以使用任何图书馆

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

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