所以,我有一个值类型:
Glide.with(getActivity())
.load("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/AJ_Digital_Camera.svg")
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(img_resturant);
使用 jedis 客户端(很重要class Session {
long createdAt;
List<String> postIds;
}
),我目前正在执行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
PS :开始学习Redis,希望可以解决这种映射问题。是的,至少不希望将客户变更作为答案。
答案 0 :(得分:3)
您不是分别使用字段,而是同时使用。因此,建议您使用简单的Redis Strings而不是Redis Hashes。因此,您将使用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;
}
注意:我已使用Gson进行序列化/反序列化。不用说,您可以使用任何库。
答案 1 :(得分:2)
Jedis没有提供一种将对象映射到哈希结构的方法。
如果您使用的是Spring,则可以查看HashMappers。 HashMapper将POJO转换为哈希,反之亦然。对于您而言,HashMapper会将Session转换为哈希,反之亦然。
答案 2 :(得分:2)
Session session = new ObjectMapper().convertValue(map, Session.class);
因此,您不需要将映射器库用作Jackson-Databind
答案 3 :(得分:0)
您可以像下面那样在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;
}