Created
March 18, 2017 03:55
-
-
Save banyudu/0d0b24196d87d53b6202aa3db074c69d to your computer and use it in GitHub Desktop.
proxy get method for db record in cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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