我应该如何在redis中存储数组? [英] How should I store an array in redis?
问题描述
我有一个查询,它在 javaScript (json
) 中返回一个对象数组,我需要将它保存在 redis
中,而不需要 foreach
,
I have a query that returns an array of objects in javaScript (json
) and I need to save it in redis
without the need for a foreach
,
我目前使用 redis
set
命令并将 json
数组转换为 string
,但我不不知道这是多么优化,因为我们正在谈论一个转换为 string
的 json
数组.
I currently use the redis
set
command and converted the json
array to string
, but I don't know how optimal this is since we are talking about a json
array converted to string
.
这是您用来尝试模拟redis
中json
对象的数组存储的代码.
This is the code you use to try to emulate the array storage of json
objects inredis
.
client.set('proyectos',JSON.stringify(proyectos));
res.json(proyectos);
这里我提取链并将其转换回 json
对象的数组.
Here I extract the chain and convert it back into an array of json
objects.
redisCtrl.getProyectos=async (req,res,next)=>{
client.get('proyectos').then(proyectos=>{
if(proyectos){
console.log("ok");
res.json(JSON.parse(proyectos));
}
else
next();
}).catch(err=>{
console.error("error");
next();
});
};
这将返回以下内容:
[{"id":1,"nombre":"cualquier","descripcion":"descripción muy especifica","monto":"100000","fecha":"2019-10-16","estado":true},{"id":2,"nombre":"conjunto autosustentable","descripcion":"es un proyecto creado para favourecer al media environmente y reducir costos de estilo de vida","monto":"15000","fecha":"2019-12-16","estado":true},{"id":3,"nombre":"cultivo autosustentable","descripcion":"el objetivo es reducirel costo de producción de alimento yavourecer el Medioambine","monto":"190000000","fecha":"2019-12-16","estado":true}]
这本身不是错误,但我认为如上所述这是一种不好的做法,而且对于大规模生产环境更是如此,那么我应该如何以最佳方式做到这一点?
This in itself is not a mistake, but I would think it is a bad practice as mentioned above, and more for a mass production environment, so how should I do this in the most optimal way possible?
推荐答案
如果你的对象很浅,你可以对每个项目使用哈希,对数组使用列表
if your object are shallow, you can use hash for each item and a list for the array
使用列表键作为项目键的名称 itemlist
使用散列将实际数据存储在键中,例如 item:1
using a list key for name of the item keys itemlist
using hash for storing actual data in key such as item:1
const data = [
{
id: 1,
nombre: 'cualquier',
descripcion: 'descripción muy especifica',
monto: '100000',
fecha: '2019-10-16',
estado: true
},
{
id: 2,
nombre: 'conjunto autosustentable',
descripcion:
'es un proyecto creado para favorecer al medio ambiente y reducir costos de estilo de vida',
monto: '15000',
fecha: '2019-12-16',
estado: true
},
{
id: 3,
nombre: 'cultivo autosustentable',
descripcion:
'el objetivo es reducir el costo de producción de alimento y favorecer el medio ambiente',
monto: '190000000',
fecha: '2019-12-16',
estado: true
}
]
// using ioredis lib in this example
// saving it to redis
for (let i = 0; i < data.length; i++) {
const item = data[i]
await redis.hmset('item:${item.id}', item)
await redis.lpush('itemlist', `item:${item.id}`)
}
// getting it back from redis: first geet the keys ; then get all the data
const keys = await redis.lrange('itemlist', 0, -1) // 0, -1 => all items
const p = redis.pipeline()
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
p.hgetall(key)
}
const resp = await p.exec()
这篇关于我应该如何在redis中存储数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!