问题描述
是否无法通过Redis 5.0存储对象数组或对象列表?
我只能使其工作-如果我首先将数组/列表转换为JSON格式,如下所示:
redis.set('persons',JSON.stringify(array_of_persons)
const persons = JSON.parse(await redis.get('persons'));
OR
redis.hset('my_list','persons',JSON.stringify(array_of_persons));
const persons = JSON.parse(await redis.hget('my_list','persons'));
但是当我需要从集合中添加/更新/删除对象时,要在javascript中连续地字符串化/解析大集合有很多开销-因此,如果无法在其中完成代码,我该如何性能优化该代码Redis v。5.0中更“原生”的方式?
解决方法
您可以将它们放在 Redis 中的列表中,而不是字符串中。试试:
redis.lpush('persons',...array_of_persons)
有关 List 命令的完整补充,请查看:https://redis.io/commands#list