【问题标题】:How should I store an array in redis?我应该如何在redis中存储一个数组?
【发布时间】:2020-02-16 13:40:03
【问题描述】:

我有一个查询,它在 javaScript (json) 中返回一个对象数组,我需要将它保存在 redis 中,而不需要 foreach

我目前使用redis set 命令并将json 数组转换为string,但我不知道这是多么优化,因为我们正在谈论将json 数组转换为@987654329 @。

这是您用来模拟json 对象在redis 中的数组存储的代码。

client.set('proyectos',JSON.stringify(proyectos));
 res.json(proyectos);

在这里我提取链并将其转换回json 对象的数组。

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 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}]

这本身并不是一个错误,但我认为如上所述这是一种不好的做法,而且对于大规模生产环境而言更是如此,那么我应该如何以最优化的方式做到这一点?

【问题讨论】:

  • Bienvenido 堆栈溢出。 Este 网站 esta usando solamente Ingles,没有 Español。请使用英语。
  • 你考虑过使用 RedisJSON 吗?
  • 我是redis新手,不知道有redisJSON 非常感谢
  • 我应该为 json 数组使用什么依赖项或 npm 模块?

标签: node.js npm redis


【解决方案1】:

如果你的对象很浅,你可以为每个项目使用哈希,为数组使用列表

使用列表键作为项目键的名称itemlist 使用哈希将实际数据存储在密钥中,例如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()

【讨论】:

  • 这对于国际 Web 开发来说真的是最佳选择吗?,如果登录的用户通过 redis 查询他的购物车或他的已付或未付或混合发票列表,为什么要使用这个想法, 让我担心的是,如果同时执行此操作的用户有数英里的蜂蜜,这会使缓存超载
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-11-17
  • 2021-08-26
  • 2015-11-27
  • 2017-12-07
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多