在 Redis 中存储 Java 对象列表的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28935229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to store a list of java objects in Redis
提问by user_ab
It would be great if someone could suggest me on what would be the best way to store a list of java objects in Redis.
如果有人可以向我建议在 Redis 中存储 Java 对象列表的最佳方法,那就太好了。
Currently, I'm converting the java objects into json strings and storing those strings in Redis and I have a set in Redis to keep track of all these.
目前,我正在将 java 对象转换为 json 字符串并将这些字符串存储在 Redis 中,并且我在 Redis 中有一个集合来跟踪所有这些。
For eg :-
例如:-
SET student:1 '{"name":"testOne","stream":computer science}'
SET student:2 '{"name":"testTwo","stream":electronics}'
SADD students 1
SADD students 2
So when ever I want to fetch the list of students, I first get the set students
and then iterate over it and get the json strings at those keys.
因此,当我想获取学生列表时,我首先获取集合students
,然后对其进行迭代并获取这些键处的 json 字符串。
Just wondering if there is any other better way to handle the scenario of storing a list of java objects to Redis.
只是想知道是否有其他更好的方法来处理将 Java 对象列表存储到 Redis 的场景。
(I'm using redis as a cache)
(我使用 redis 作为缓存)
采纳答案by zenbeni
If you don't need querying your java objects stored in redis (indexation, scores...), then you can just serialize them to a bytearray with a library (Kryo for instance) and store them in a String in redis. Note that you can serialize a whole collection of java objects in one redis string, just remember which class it is (collection type + type of object) when you want to deserialize, you can define a clever key name for this purpose for instance.
如果您不需要查询存储在 redis 中的 java 对象(索引、分数...),那么您可以将它们序列化为带有库(例如 Kryo)的字节数组,并将它们存储在 redis 中的字符串中。请注意,您可以在一个 redis 字符串中序列化整个 java 对象集合,只要记住它是哪个类(集合类型 + 对象类型),当您想要反序列化时,您可以为此目的定义一个巧妙的键名。
JSON will just use more space and will be more network intensive than other binary marshalling formats, if you don't need queries or human readable data in redis, then it is fine (and more performant) to store binary data in redis.
与其他二进制编组格式相比,JSON 只会使用更多空间并且网络密集度更高,如果您不需要在 redis 中进行查询或人类可读数据,那么将二进制数据存储在 redis 中是可以的(并且性能更高)。
Note that if you are using jedis library (as you tagged it) you have methods on Jedis that take bytearrays as parameters instead of Java strings in order to send data as C-String to Redis (no data loss in the process).
请注意,如果您正在使用 jedis 库(如您所标记的那样),您在 Jedis 上有一些方法将字节数组作为参数而不是 Java 字符串,以便将数据作为 C 字符串发送到 Redis(在此过程中不会丢失数据)。
回答by Lugg
Redis is more than a simple key value store. You can use hashes to store structured data in redis
Redis 不仅仅是一个简单的键值存储。您可以使用哈希在 redis 中存储结构化数据
HMSET student:1 name "testOne" stream "computer science" http://redis.io/commands/hmset
HMSET 学生:1 名“testOne”流“计算机科学” http://redis.io/commands/hmset
to get all contents of the hash call HGETALL student:1
you can also get single values of Hashes with HGET
EXAMPLE HGET student:1 name
要获取哈希调用 HGETALL student:1 的所有内容,
您还可以使用 HGET
示例 HGET student:1 name获取哈希的单个值
回答by Nikita Koksharov
You can easily do it with Redisson. It's a Redis based framework for Java which supports many popular codecs (Hymanson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization) and Redis connection modes like Cluster, Sentinel, AWS Elasticache.
您可以使用Redisson轻松完成。它是一个基于 Redis 的 Java 框架,支持许多流行的编解码器(Hymanson JSON、Avro、Smile、CBOR、MsgPack、Kryo、FST、LZ4、Snappy 和 JDK 序列化)和 Redis 连接模式,如 Cluster、Sentinel、AWS Elasticache。
Here is an example how to store Java object to list:
以下是如何将 Java 对象存储到列表的示例:
RList<SomeObject> list = redisson.getList("anyList");
list.add(new SomeObject(1));
list.add(new SomeObject(2));
RList
interface also implements java.util.List
interface. Pretty easy, right?
RList
接口也实现了java.util.List
接口。很容易,对吧?