Skip to content

Instantly share code, notes, and snippets.

@banyudu
Created March 18, 2017 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyudu/0d0b24196d87d53b6202aa3db074c69d to your computer and use it in GitHub Desktop.
Save banyudu/0d0b24196d87d53b6202aa3db074c69d to your computer and use it in GitHub Desktop.
proxy get method for db record in cache
Cache.proxyGetDBRecord = function *(model, id, field) {
if (!id) {
return null;
}
let key = genCacheKeyWithModel(model, id); // generate id with tableName and id
let result = null;
if (field) {
result = yield client.hgetAsync(key, field);
} else {
result = yield client.hgetallAsync(key);
}
if (result === null) {
let fields = model.getCacheFields();
if (field && fields.indexOf(field) === -1) {
fields.push(field);
}
let data = yield model.findOne({
where: {id: id},
attributes: fields,
raw: true
});
if (data) {
yield client.hmsetAsync(key, data);
yield client.expireAsync(key, constant.CACHE_EXPIRE_TIME);
result = field? data[field]: data;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment