fix: remove redis object cache

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 740388492f
commit 4df925e76a

@ -3,13 +3,6 @@
module.exports = function (redisClient, module) { module.exports = function (redisClient, module) {
var helpers = module.helpers.redis; var helpers = module.helpers.redis;
const async = require('async');
const _ = require('lodash');
const cache = require('../cache').create('redis');
module.objectCache = cache;
module.setObject = function (key, data, callback) { module.setObject = function (key, data, callback) {
callback = callback || function () {}; callback = callback || function () {};
if (!key || !data) { if (!key || !data) {
@ -30,11 +23,7 @@ module.exports = function (redisClient, module) {
return callback(); return callback();
} }
redisClient.hmset(key, data, function (err) { redisClient.hmset(key, data, function (err) {
if (err) { callback(err);
return callback(err);
}
cache.delObjectCache(key);
callback();
}); });
}; };
@ -44,51 +33,23 @@ module.exports = function (redisClient, module) {
return callback(); return callback();
} }
redisClient.hset(key, field, value, function (err) { redisClient.hset(key, field, value, function (err) {
if (err) { callback(err);
return callback(err);
}
cache.delObjectCache(key);
callback();
}); });
}; };
module.getObject = function (key, callback) { module.getObject = function (key, callback) {
module.getObjects([key], function (err, data) { redisClient.hgetall(key, callback);
if (err) {
return callback(err);
}
callback(null, data && data.length ? data[0] : null);
});
}; };
module.getObjects = function (keys, callback) { module.getObjects = function (keys, callback) {
var cachedData = {}; if (!Array.isArray(keys) || !keys.length) {
function getFromCache(next) { return setImmediate(callback, null, []);
process.nextTick(next, null, keys.map(key => _.clone(cachedData[key])));
}
const unCachedKeys = cache.getUnCachedKeys(keys, cachedData);
if (!unCachedKeys.length) {
return getFromCache(callback);
} }
if (keys.length > 1) {
async.waterfall([ helpers.execKeys(redisClient, 'batch', 'hgetall', keys, callback);
function (next) {
if (unCachedKeys.length > 1) {
helpers.execKeys(redisClient, 'batch', 'hgetall', unCachedKeys, next);
} else { } else {
redisClient.hgetall(unCachedKeys[0], (err, data) => next(err, [data])); redisClient.hgetall(keys[0], (err, data) => callback(err, [data]));
} }
},
function (data, next) {
unCachedKeys.forEach(function (key, i) {
cachedData[key] = data[i] || null;
cache.set(key, cachedData[key]);
});
getFromCache(next);
},
], callback);
}; };
module.getObjectField = function (key, field, callback) { module.getObjectField = function (key, field, callback) {
@ -107,20 +68,25 @@ module.exports = function (redisClient, module) {
if (!Array.isArray(fields) || !fields.length) { if (!Array.isArray(fields) || !fields.length) {
return callback(null, keys.map(function () { return {}; })); return callback(null, keys.map(function () { return {}; }));
} }
module.getObjects(keys, function (err, items) { var batch = redisClient.batch();
for (var x = 0; x < keys.length; x += 1) {
batch.hmget.apply(batch, [keys[x]].concat(fields));
}
batch.exec(function (err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
const returnData = items.map((item) => {
item = item || {};
const result = {};
fields.forEach((field) => {
result[field] = item[field] !== undefined ? item[field] : null;
});
return result;
});
callback(null, returnData); results = results.map(function makeObject(array) {
var obj = {};
for (var i = 0, ii = fields.length; i < ii; i += 1) {
obj[fields[i]] = array[i];
}
return obj;
});
callback(null, results);
}); });
}; };
@ -150,14 +116,12 @@ module.exports = function (redisClient, module) {
return setImmediate(callback); return setImmediate(callback);
} }
redisClient.hdel(key, field, function (err) { redisClient.hdel(key, field, function (err) {
cache.delObjectCache(key);
callback(err); callback(err);
}); });
}; };
module.deleteObjectFields = function (key, fields, callback) { module.deleteObjectFields = function (key, fields, callback) {
helpers.execKeyValues(redisClient, 'batch', 'hdel', key, fields, function (err) { helpers.execKeyValues(redisClient, 'batch', 'hdel', key, fields, function (err) {
cache.delObjectCache(key);
callback(err); callback(err);
}); });
}; };
@ -176,7 +140,6 @@ module.exports = function (redisClient, module) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
cache.delObjectCache(key);
callback(null, Array.isArray(result) ? result.map(value => parseInt(value, 10)) : parseInt(result, 10)); callback(null, Array.isArray(result) ? result.map(value => parseInt(value, 10)) : parseInt(result, 10));
} }
value = parseInt(value, 10); value = parseInt(value, 10);

@ -16,7 +16,6 @@ module.exports = function (redisClient, module) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
module.objectCache.resetObjectCache();
callback(); callback();
}); });
}; };
@ -36,7 +35,6 @@ module.exports = function (redisClient, module) {
module.delete = function (key, callback) { module.delete = function (key, callback) {
callback = callback || function () {}; callback = callback || function () {};
redisClient.del(key, function (err) { redisClient.del(key, function (err) {
module.objectCache.delObjectCache(key);
callback(err); callback(err);
}); });
}; };
@ -48,7 +46,6 @@ module.exports = function (redisClient, module) {
batch.del(keys[i]); batch.del(keys[i]);
} }
batch.exec(function (err) { batch.exec(function (err) {
module.objectCache.delObjectCache(keys);
callback(err); callback(err);
}); });
}; };
@ -75,8 +72,6 @@ module.exports = function (redisClient, module) {
if (err && err.message !== 'ERR no such key') { if (err && err.message !== 'ERR no such key') {
return callback(err); return callback(err);
} }
module.objectCache.delObjectCache(oldKey);
module.objectCache.delObjectCache(newKey);
callback(); callback();
}); });
}; };

Loading…
Cancel
Save