Remove redis

This commit is contained in:
Zaid Abdulla committed 2019-02-04 23:47:54 -08:00
1 parent e6700fdcaa
commit 824639976f
3 files changed
+11 -99

No files matched your search

+11 -93
View File
@@ -22,28 +22,11 @@ const config = require("./config");
const MAX_CACHE_TTL = config.get("MAX_CACHE_TTL");
let redisClient;
let mongoClient;
let mongoCollection;
let mongoTimeOffset = 0;
function connect(callback) {
const REDIS_HOST = config.get("REDIS_HOST");
if (REDIS_HOST) {
const redis = require("redis");
const REDIS_PORT = config.get("REDIS_PORT");
const REDIS_DB = config.get("REDIS_DB");
redisClient = redis.createClient(REDIS_PORT, REDIS_HOST);
redisClient.select(REDIS_DB, err => {
exports.get = redisGet;
exports.set = redisSet;
exports.del = redisDel;
exports.pop = redisPop;
exports.lock = redisLock;
callback(err);
});
} else {
const mongodb = require("mongodb");
const MONGODB_CONNECTION_URL = config.get("MONGODB_CONNECTION_URL");
@@ -62,87 +45,17 @@ function connect(callback) {
db.command({ hostInfo: 1 }, (err, res) => {
if (err) return void callback(err);
mongoTimeOffset = res.system.currentTime.getTime() - now;
exports.get = mongoGet;
exports.set = mongoSet;
exports.del = mongoDel;
exports.pop = mongoPop;
exports.lock = mongoLock;
callback();
});
}
);
}
}
function disconnect() {
if (redisClient) redisClient.quit();
if (mongoClient) mongoClient.close();
}
function redisGet(key, callback) {
if (Array.isArray(key)) redisClient.mget(key, callback);
else redisClient.get(key, callback);
}
function redisDel(key, callback) {
redisClient.del(key, callback);
}
function redisSet(key, value, ttl, callback) {
if (!callback && typeof v === "function") {
callback = ttl;
ttl = null;
}
if (!ttl) ttl = MAX_CACHE_TTL;
redisClient.setex(key, ttl, value, callback);
}
function redisPop(key, callback) {
const POP_SCRIPT =
'local v=redis.call("get",KEYS[1]);redis.call("del",KEYS[1]);return v;';
redisClient.eval(POP_SCRIPT, 1, key, callback);
}
function redisLock(lockName, ttl, callback) {
const UNLOCK_SCRIPT =
'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end';
const EXTEND_SCRIPT =
'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("pexpire", KEYS[1], ARGV[2]) else return 0 end';
const token = Math.random()
.toString(36)
.slice(2);
function unlockOrExtend(extendTtl) {
if (!extendTtl) {
redisClient.eval(UNLOCK_SCRIPT, 1, lockName, token, (err, res) => {
if (err || !res) throw err || new Error("Lock expired");
});
} else {
redisClient.eval(
EXTEND_SCRIPT,
1,
lockName,
token,
extendTtl,
(err, res) => {
if (err || !res) throw err || new Error("Lock expired");
}
);
}
}
redisClient.set(lockName, token, "NX", "EX", ttl, (err, res) => {
if (err || res) return void callback(err, unlockOrExtend);
setTimeout(() => {
redisLock(lockName, ttl, callback);
}, 200);
});
}
function mongoGet(key, callback) {
function get(key, callback) {
const expire = new Date(Date.now() - mongoTimeOffset);
if (Array.isArray(key)) {
mongoCollection.find({ _id: { $in: key } }).toArray((err, res) => {
@@ -170,13 +83,13 @@ function mongoGet(key, callback) {
}
}
function mongoDel(key, callback) {
function del(key, callback) {
if (Array.isArray(key))
mongoCollection.deleteMany({ _id: { $in: key } }, callback);
else mongoCollection.deleteOne({ _id: key }, callback);
}
function mongoSet(key, value, ttl, callback) {
function set(key, value, ttl, callback) {
if (!callback && typeof v === "function") {
callback = ttl;
ttl = null;
@@ -193,7 +106,7 @@ function mongoSet(key, value, ttl, callback) {
);
}
function mongoPop(key, callback) {
function pop(key, callback) {
mongoCollection.findOneAndDelete({ _id: key }, (err, res) => {
if (err || !res["value"]) return void callback(err);
@@ -204,7 +117,7 @@ function mongoPop(key, callback) {
});
}
function mongoLock(lockName, ttl, callback) {
function lock(lockName, ttl, callback) {
const token = Math.random()
.toString(36)
.slice(2);
@@ -238,7 +151,7 @@ function mongoLock(lockName, ttl, callback) {
err => {
if (err && err.code === 11000) {
return setTimeout(() => {
mongoLock(lockName, ttl, callback);
lock(lockName, ttl, callback);
}, 200);
}
@@ -249,3 +162,8 @@ function mongoLock(lockName, ttl, callback) {
exports.connect = connect;
exports.disconnect = disconnect;
exports.get = get;
exports.set = set;
exports.del = del;
exports.pop = pop;
exports.lock = lock;
-3
View File
@@ -27,9 +27,6 @@ const options = {
type: "string",
default: "mongodb://127.0.0.1/genieacs"
},
REDIS_PORT: { type: "int", default: 6379 },
REDIS_HOST: { type: "string", default: "" },
REDIS_DB: { type: "int", default: 0 },
CWMP_WORKER_PROCESSES: { type: "int", default: 0 },
CWMP_PORT: { type: "int", default: 7547 },
-3
View File
@@ -32,9 +32,6 @@
"parsimmon": "^1.12.0",
"seedrandom": "^2.4.4"
},
"optionalDependencies": {
"redis": "^2.6.5"
},
"devDependencies": {
"ava": "^1.1.0",
"babel-eslint": "^8.2.6",