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
+30 -118

No files matched your search

+30 -112
View File
@@ -22,127 +22,40 @@ const config = require("./config");
const MAX_CACHE_TTL = config.get("MAX_CACHE_TTL"); const MAX_CACHE_TTL = config.get("MAX_CACHE_TTL");
let redisClient;
let mongoClient; let mongoClient;
let mongoCollection; let mongoCollection;
let mongoTimeOffset = 0; let mongoTimeOffset = 0;
function connect(callback) { function connect(callback) {
const REDIS_HOST = config.get("REDIS_HOST"); const mongodb = require("mongodb");
const MONGODB_CONNECTION_URL = config.get("MONGODB_CONNECTION_URL");
if (REDIS_HOST) { mongodb.MongoClient.connect(
const redis = require("redis"); MONGODB_CONNECTION_URL,
const REDIS_PORT = config.get("REDIS_PORT"); { useNewUrlParser: true },
const REDIS_DB = config.get("REDIS_DB"); (err, client) => {
redisClient = redis.createClient(REDIS_PORT, REDIS_HOST); if (err) return void callback(err);
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");
mongodb.MongoClient.connect( const db = client.db();
MONGODB_CONNECTION_URL, mongoClient = client;
{ useNewUrlParser: true }, mongoCollection = db.collection("cache");
(err, client) => { mongoCollection.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
const now = Date.now();
db.command({ hostInfo: 1 }, (err, res) => {
if (err) return void callback(err); if (err) return void callback(err);
mongoTimeOffset = res.system.currentTime.getTime() - now;
const db = client.db(); callback();
mongoClient = client; });
mongoCollection = db.collection("cache"); }
mongoCollection.createIndex({ expire: 1 }, { expireAfterSeconds: 0 }); );
const now = Date.now();
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() { function disconnect() {
if (redisClient) redisClient.quit();
if (mongoClient) mongoClient.close(); if (mongoClient) mongoClient.close();
} }
function redisGet(key, callback) { function get(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) {
const expire = new Date(Date.now() - mongoTimeOffset); const expire = new Date(Date.now() - mongoTimeOffset);
if (Array.isArray(key)) { if (Array.isArray(key)) {
mongoCollection.find({ _id: { $in: key } }).toArray((err, res) => { 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)) if (Array.isArray(key))
mongoCollection.deleteMany({ _id: { $in: key } }, callback); mongoCollection.deleteMany({ _id: { $in: key } }, callback);
else mongoCollection.deleteOne({ _id: 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") { if (!callback && typeof v === "function") {
callback = ttl; callback = ttl;
ttl = null; 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) => { mongoCollection.findOneAndDelete({ _id: key }, (err, res) => {
if (err || !res["value"]) return void callback(err); 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() const token = Math.random()
.toString(36) .toString(36)
.slice(2); .slice(2);
@@ -238,7 +151,7 @@ function mongoLock(lockName, ttl, callback) {
err => { err => {
if (err && err.code === 11000) { if (err && err.code === 11000) {
return setTimeout(() => { return setTimeout(() => {
mongoLock(lockName, ttl, callback); lock(lockName, ttl, callback);
}, 200); }, 200);
} }
@@ -249,3 +162,8 @@ function mongoLock(lockName, ttl, callback) {
exports.connect = connect; exports.connect = connect;
exports.disconnect = disconnect; 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", type: "string",
default: "mongodb://127.0.0.1/genieacs" 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_WORKER_PROCESSES: { type: "int", default: 0 },
CWMP_PORT: { type: "int", default: 7547 }, CWMP_PORT: { type: "int", default: 7547 },
-3
View File
@@ -32,9 +32,6 @@
"parsimmon": "^1.12.0", "parsimmon": "^1.12.0",
"seedrandom": "^2.4.4" "seedrandom": "^2.4.4"
}, },
"optionalDependencies": {
"redis": "^2.6.5"
},
"devDependencies": { "devDependencies": {
"ava": "^1.1.0", "ava": "^1.1.0",
"babel-eslint": "^8.2.6", "babel-eslint": "^8.2.6",