我们希望从Redis获得响应作为流,类似于Postgres Streams。
我们有一个限制,那就是要使用尽可能少的服务器内存。我们的某些列表可能约为10MB,并且需要多个用户同时 请求。
以传统方式 这样做会最终在我们的服务器上引起内存问题。
另一种解决方案是迭代列表并发送lrange
命令,每个批处理都返回一部分列表项,但是由于网络往返而效率低下。
这是我现在拥有的,这是标准的处理方式:
'use strict'
const redis = require('redis'),
client = redis.createClient()
client.on('error', function (err) {
console.log('Error ' + err)
})
client.rpush('foo', 1)
client.rpush('foo', 2)
client.rpush('foo', 3)
client.rpush('foo', 4)
client.rpush('foo', 5)
client.lrange('foo', 0, -1, (err, replies) => {
console.log(replies)
})
答案 0 :(得分:1)
Redis中尚未实现流传输,因此分页是一种很好的方法。也就是说,List数据结构不能很好地支持该模式,因为LRANGE
是昂贵的(O(N))操作。
我建议您考虑使用其他数据结构,例如排序集或v5流来存储数据。