node-shared-shm-cache
v0.0.1
Published
Node.js shared memory cache
Downloads
2
Readme
Nodejs Shared Memory Map
a shared memory cache for nodejs, support grow/shrink, read/write lock
- with this, every process QPS 4W+ and pipe 400MB/s data
- i try use ipc(process.send message) to share cache ,but QPS 4k and pipe 50MB/s
- i try use mmap or other lib, but no read/write lock so is hard to sync even crash or stuck
- comfort for nodejs server with fork() process, local cache
Installation
xxx todo
Usage
const addon = require('../build/Release/hello.node');
const cluster = require('node:cluster');
class ShmObject {
cache = null;
constructor() {
if (cluster.isMaster || cluster.isPrimary) {
// careful only master set true to reset memory
this.cache = new addon.NodeShareCache("Highscorev3", 1024 * 100, true);
} else {
// child process set false
this.cache = new addon.NodeShareCache("Highscorev3", 1024 * 100, false);
}
// cache expire time
this.cache.setMaxAge(40000);
// enable lock
this.cache.setLock(true);
}
get(key) {
return this.cache.get(key);
}
set(key, value) {
return this.cache.set(key, value);
}
}
module.exports = ShmObject;
API
- get/set with key , base data operation
- setMaxAge , set max age for data expire,default 2000ms
- setMaxSize , set max memory allocation size, default 800MB
- setLock(bool), enable/disable lock
can be disable when read only to improve performance
!!!!can nooooot disable when write, cause crash or stuck - setLogLevel(1,2,3) , show detail logs
memory allocation
grow
- <4MB auto grow x2
- 4MB-400MB auto grow 400KB
- 400MB-4GB auto grow 4MB
- and custome grow
shrink
when clean up, and free memory more than 40MB shrink to fit the size of data
read/write lock
- read with shared lock
- write with exclusive lock
- no timeout lock support (toto later add)
- memory grow/shrink with exclusive lock
benchmark
5 child process , read only, macbook pro
5 child process , read+write, macbook pro
5 child process , read first, macbook pro
5 child process , write first, win64 i5-12490