通过 NodeJS 在 Redis v. 5.0 中存储数组或对象列表
Store an array or a list of objects in Redis v. 5.0 via NodeJS
是否不能通过 Redis 5.0 存储对象数组或对象列表?
我只能让它工作 - 如果我首先将 array/list 转换为 JSON-格式,如下所示:
redis.set('persons', JSON.stringify(array_of_persons)
const persons = JSON.parse(await redis.get('persons'));
或
redis.hset('my_list', 'persons', JSON.stringify(array_of_persons));
const persons = JSON.parse(await redis.hget('my_list', 'persons'));
但是当我需要 add/update/remove 集合中的对象时,在 javascript 中连续 stringify/parse 大集合有很多开销 - 那么我如何优化此代码的性能 -如果它不能在 Redis v. 5.0 中以更“原生”的方式完成?
您可以将它们放在 Redis 中的列表中而不是字符串中。尝试:
redis.lpush('persons', ...array_of_persons)
有关列表命令的完整补充,请查看:https://redis.io/commands#list
是否不能通过 Redis 5.0 存储对象数组或对象列表?
我只能让它工作 - 如果我首先将 array/list 转换为 JSON-格式,如下所示:
redis.set('persons', JSON.stringify(array_of_persons)
const persons = JSON.parse(await redis.get('persons'));
或
redis.hset('my_list', 'persons', JSON.stringify(array_of_persons));
const persons = JSON.parse(await redis.hget('my_list', 'persons'));
但是当我需要 add/update/remove 集合中的对象时,在 javascript 中连续 stringify/parse 大集合有很多开销 - 那么我如何优化此代码的性能 -如果它不能在 Redis v. 5.0 中以更“原生”的方式完成?
您可以将它们放在 Redis 中的列表中而不是字符串中。尝试:
redis.lpush('persons', ...array_of_persons)
有关列表命令的完整补充,请查看:https://redis.io/commands#list