简体   繁体   English

如何从Redis缓存中获得定制的价值?

[英]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.

相关问题 如何从redis缓存中取回Long(数据类型)值 - How to get Long (data type) value back from redis cache 如何从番石榴缓存加载器获取缓存的值并更新该值而不更改缓存中的值? - How can I get the value of a cache from guava cacheloader and update the value without changing the value in cache? Spring Cache中的@Cacheable将值存储在cache外部的redis中。 如何将它放入Redis的缓存中? - @Cacheable in Spring Cache Stores value in redis outside cache . How do i put it inside cache in redis? 如何通过 Spring 启动 Spring 数据从 redis 缓存中获取所有密钥? - How do I get all the keys from a redis cache via Spring Boot with Spring Data Redis 2.x? 如何通过Spring Boot从Redis缓存中获取所有密钥? - How do I get all the keys from a redis cache via Spring Boot? 如何将自定义值传递给spring aop建议? - How can I pass a customized value to a spring aop advice? 如何从Coherence缓存中获取统计信息? - How can I get statistics back from a Coherence cache? 使用 Jedis 从 redis 缓存中获取所有键值对的有效方法 - Efficient way to get all the key value pair from redis cache using Jedis 无法通过 Spring 从 Redis 获取正确的值 - Can't get correct value from Redis through Spring 如何从具有不同密钥的redis数据库中一次获取100条记录? - How can i get 100 records at a time from redis database having different keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM