我正在尝试将大型数据库查询结果存储在Memcached服务器中,以便更快地进行检索,并在压缩数据之前使用brotli将其输入Memcache。
我一直在尝试用这样的示例对象进行测试:
//cacheserver already defined and connected
let brotli_config = {
mode: 1, //0=regular, 1=text
quality: 8 //1=fast .. 11=small
}
let test_key = 'paths_100'
let test_value = [
{
text: 'this is text',
outlines: 'this is an outline',
inlines: 'this is an inline'
}
]
let compressed_in = require('brotli/compress')(JSON.stringify(test_value), brotli_config)
console.log(compressed_in) //LOG 1
cacheserver.set(test_key, compressed_in, {
expires: 3600
},
function(err) {
cacheserver.get(test_key, function(err, compressed_out) {
console.log(compressed_out) //LOG 2
let string_out = require('brotli/decompress')(compressed_out)
console.log(string_out) //LOG 3
})
}
)
这是输出:
//LOG 1
Uint8Array(10) [
27, 86, 0, 0, 36,
0, 2, 177, 64, 32
]
//LOG 2
<Buffer 1b 56 00 00 24 00 02 b1 40 20>
//LOG 3
Uint8Array(87) [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0
]
我正在将对象转换为字符串(可能不是提高性能的好主意,但是使用brotli模式0且不进行字符串化是行不通的;返回null ...),将其压缩,将10项uint8array存储在缓存服务器,检索10字节的缓冲区并解压缩。但是,解压缩后的结果只是一个零数组。这里发生了什么事?我第一次使用此压缩库和memcached客户端(memjs),因此,有关如何正确使用它们的任何信息都将受到赞赏。
我想出了一个解决方案(请告诉我是否可以加快速度)。非常感谢this blog post阐明了nodejs对象,字符串和缓冲区之间的关系。
在将对象数据传递给brotli之前,我缺少从字符串到缓冲区的转换。现在,整个过程如下所示:
let data = {/* stuff */}
let test_key = 'paths_100'
let compressed_in = require('brotli/compress')(Buffer.from(JSON.stringify(test_value),'utf-8'), brotli_config)
cacheserver.set(test_key, compressed_in, {
expires: 3600
},
function(err) {
cacheserver.get(test_key, function(err, compressed_out) {
let data_out = JSON.parse(Buffer.from(require('brotli/decompress')(compressed_out)).toString('utf-8'))
})
}
)