简体   繁体   中英

How can I get customized value from Redis cache?

I am working on Redis with spring boot. And I am new in Redis. I am stuck in a problem where I need customized data from Redis cache.

For example, the key is 1 and the object stored in Redis is

{
    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. 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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM