[英]How can I get customized value from Redis cache?
I am working on Redis with spring boot. 我正在使用Spring Boot开发Redis。 And I am new in Redis.
我是Redis的新手。 I am stuck in a problem where I need customized data from Redis cache.
我陷入需要从Redis缓存中获取自定义数据的问题。
For example, the key is 1 and the object stored in Redis is 例如,键为1,Redis中存储的对象为
{
name,
age,
DOB,
...
}
and I want to retrieve only 2 two of them from Redis eg name and age But for a given key Redis returns the complete object. 而且我只想从Redis中检索其中的两个,例如名称和年龄,但是对于给定的键,Redis返回完整的对象。 But I need only custom fields from that object.
但是我只需要该对象的自定义字段。
I have no idea how to get over this problem and I cannot do like just to cache only 2 required fields. 我不知道如何解决这个问题,我不能只缓存2个必填字段。 Caching of the complete object is required for other purposes.
出于其他目的,需要缓存整个对象。
Please help. 请帮忙。 Thanks in advance.
提前致谢。
You can use hmget operation and specify fields what you need. 您可以使用hmget操作并指定所需的字段。 Next example from official documentation :
官方文档中的下一个示例:
redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HMGET myhash field1 field2 nofield
1) "Hello"
2) "World"
3) (nil)
Edited: If you prefer use list it may look like this: 编辑:如果您更喜欢使用列表,它可能看起来像这样:
RPUSH mylist "name"
(integer) 1
redis> RPUSH mylist "age"
(integer) 2
redis> RPUSH mylist "DOB"
(integer) 3
Then you can use 那你可以用
LRANGE mylist 0 1
1) "name"
2) "age"
But if you need associate key with value for one object more elegant will be Hashes data structure 但是,如果您需要将一个键的值与一个对象的值相关联,则哈希数据结构会更优雅
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.