mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-15 17:19:10 +00:00
change KV emulation to leveldb, remove redis
This commit is contained in:
parent
3bca07c7d7
commit
8fe3d1dcf2
7 changed files with 195 additions and 76 deletions
|
@ -1,5 +1,6 @@
|
|||
const redis = require('redis');
|
||||
const { promisify } = require('util');
|
||||
const level = require('level');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
let hasWarned = false;
|
||||
|
||||
|
@ -10,28 +11,53 @@ const getConversion = {
|
|||
stream: (x) => Buffer.from(x),
|
||||
};
|
||||
|
||||
class RedisKVShim {
|
||||
constructor(redisClient, namespace) {
|
||||
class KVShim {
|
||||
constructor(namespace) {
|
||||
this.namespace = namespace;
|
||||
this._redis = redisClient;
|
||||
this._redisGet = promisify(this._redis.get).bind(redisClient);
|
||||
this._redisSetex = promisify(this._redis.setex).bind(redisClient);
|
||||
this._redisSet = promisify(this._redis.set).bind(redisClient);
|
||||
this._redisDel = promisify(this._redis.del).bind(redisClient);
|
||||
|
||||
fs.mkdirSync(path.resolve(__dirname, '../../.devdbs'), {
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
(async () => {
|
||||
this.level = await level(path.resolve(__dirname, '../../.devdbs', namespace));
|
||||
})();
|
||||
}
|
||||
|
||||
key(key) {
|
||||
return `${this.namespace}__${key}`;
|
||||
makeValue(value, expirationTtl) {
|
||||
if (!expirationTtl) {
|
||||
return {
|
||||
value,
|
||||
expires: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
value: JSON.stringify(value),
|
||||
expires: Date.now() + 1000 * expirationTtl,
|
||||
};
|
||||
}
|
||||
|
||||
validate(value) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value.expires < Date.now()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async get(key, type = 'text') {
|
||||
const result = await this._redisGet(this.key(key));
|
||||
const result = await this.level.get(key);
|
||||
|
||||
if (!result) {
|
||||
if (!this.validate(result)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getConversion[type](result);
|
||||
return getConversion[type](result.value);
|
||||
}
|
||||
|
||||
async getWithMetadata(key, type) {
|
||||
|
@ -49,15 +75,11 @@ class RedisKVShim {
|
|||
hasWarned = true;
|
||||
}
|
||||
|
||||
if (expirationTtl) {
|
||||
return this._redisSetex(this.key(key), value, expirationTtl);
|
||||
}
|
||||
|
||||
return this._redisSet(this.key(key), value);
|
||||
return this.level.put(key, this.makeValue(value, expirationTtl));
|
||||
}
|
||||
|
||||
async delete(key) {
|
||||
return this._redisDel(this.key(key));
|
||||
return this.level.del(key);
|
||||
}
|
||||
|
||||
list() {
|
||||
|
@ -71,5 +93,5 @@ class RedisKVShim {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
RedisKVShim,
|
||||
KVShim,
|
||||
};
|
||||
|
|
|
@ -7,22 +7,15 @@ const chokidar = require('chokidar');
|
|||
const webpack = require('webpack');
|
||||
const { Crypto } = require('@peculiar/webcrypto');
|
||||
const roleypolyConfig = require('../backend-worker/roleypoly.config');
|
||||
const redis = require('redis');
|
||||
const { RedisKVShim } = require('./kv');
|
||||
const { KVShim } = require('./kv');
|
||||
const crypto = new Crypto();
|
||||
|
||||
const redisClient = redis.createClient({ host: 'redis' });
|
||||
|
||||
const getKVs = (redisClient, namespaces = []) => {
|
||||
namespaces.reduce(
|
||||
(acc, ns) => ({ ...acc, [ns]: new RedisKVShim(redisClient, ns) }),
|
||||
{}
|
||||
);
|
||||
};
|
||||
const getKVs = (namespaces = []) =>
|
||||
namespaces.reduce((acc, ns) => ({ ...acc, [ns]: new KVShim(ns) }), {});
|
||||
|
||||
const workerShims = {
|
||||
...roleypolyConfig.environment,
|
||||
...getKVs(redisClient, roleypolyConfig.kv),
|
||||
...getKVs(roleypolyConfig.kv),
|
||||
};
|
||||
|
||||
let listeners = [];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue