Created
November 4, 2016 08:47
-
-
Save tzechienchu/9b5805746c732166bfcd3c5e79cab68d to your computer and use it in GitHub Desktop.
User 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
var redisServer = require('redis'); | |
var RedisService = (function () { | |
var instance; | |
function init() { | |
//6381 for Develop and Test | |
//6379 for Production | |
var port = 6379; | |
var host = "localhost"; | |
var redis = redisServer.createClient(port,host); | |
var expireKey = function(key,cb) { | |
redis.expire(key,0,function(err,status) { | |
cb(err,status); | |
}) | |
} | |
var getKeyTTL = function(key,cb) { | |
redis.ttl(key, function (err, ttl) { | |
cb(err,ttl); | |
}); | |
} | |
var fetchByKey = function(key,cb) { | |
redis.get(key,function(err,data){ | |
cb(err,data) | |
}) | |
} | |
var fetchByKeys = function(keys,cb) { | |
if (keys.length > 0) { | |
redis.mget(keys,function(err,data){ | |
cb(err,data) | |
}) | |
} else { | |
cb(null,'Bad Keys') | |
} | |
} | |
var storeAtKey = function(key,value,ttl,cb) { | |
redis.set(key,value,function(err){ | |
if (err) { | |
cb(err) | |
} else { | |
redis.expire(key,ttl,function(err) { | |
cb(null,'ok'); | |
}); | |
} | |
}) | |
} | |
var clearCache = function(cb) { | |
redis.flushdb(); | |
} | |
var listAllKeys = function(cb) { | |
redis.keys("*",cb); | |
} | |
return { | |
fetchByKey :fetchByKey, | |
fetchByKeys:fetchByKeys, | |
storeAtKey :storeAtKey, | |
getKeyTTL:getKeyTTL, | |
expireKey:expireKey, | |
listAllKeys:listAllKeys, | |
clearCache:clearCache, | |
TTL_ONE_DAY:24*60*60, | |
TTL_30_MIN:30*60, | |
TTL_01_HOUR:1*60*60, | |
TTL_08_HOUR:8*60*60, | |
TTL_ZERO:0 | |
} | |
} | |
return { | |
getInstance: function () { | |
if ( !instance ) { | |
instance = init(); | |
} | |
return instance; | |
} | |
}; | |
})(); | |
module.exports = RedisService; |
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
var co = require('co'); | |
var _ = require('lodash'); | |
var moment = require('moment'); | |
const promisify = require("es6-promisify"); | |
var Util = require('./Utilities'); | |
var APIConst = require('./constant.js'); | |
var myRedisService = require('./redisService.js').getInstance(); | |
//User Relation inside user table | |
module.exports = function(user) { | |
var TTL = 60*60; | |
//["581965742d145f15b993ea15","581965742d145f15b993ea1c","581965742d145f15b993ea12","581965742d145f15b993ea14","581965742d145f15b993ea16","581965742d145f15b993ea19" ] | |
user.getUsersData = function(userIds,format,cb){ | |
co(function*() { | |
var cachedUser = yield promisify(myRedisService.fetchByKeys)(userIds); | |
cachedUser = cachedUser.map(function(ud) { | |
return JSON.parse(ud) | |
}) | |
//console.log(cachedUser) | |
cachedUser = cachedUser.filter(function(ud) { | |
return ud | |
}) | |
var cachedIds = cachedUser.map(function(ud) { | |
return ud.id | |
}) | |
var leftIds = userIds.filter(function(uid){ | |
return (cachedIds.indexOf(uid.toString()) === -1 ) | |
}) | |
console.log(leftIds) | |
var dbUsers = [] | |
if (leftIds.length > 0) { | |
format = format || 'array'; | |
var orQuery = []; | |
var uniqIds = _.uniq(leftIds); | |
uniqIds.map(function(uid){ | |
orQuery.push({id:uid}) | |
}); | |
var query = { | |
where:{or:orQuery}, | |
fields:APIConst.TABLE_FIELDS.UserFieldsDefault | |
} | |
dbUsers = yield user.find(query); | |
var cacheSave = yield dbUsers.map(function(ud) { | |
return promisify(myRedisService.storeAtKey)(ud.id.toString(),JSON.stringify(ud),TTL) | |
}) | |
} | |
var allUsers = cachedUser.concat(dbUsers); | |
if (format === 'dictionary') { | |
//In Dictionary Form | |
var userDict = {}; | |
allUsers.map(function(ud) { | |
userDict[ud.id] = ud | |
}) | |
cb(null,userDict) | |
} else { | |
cb(null,allUsers) | |
} | |
}) | |
.catch(function(err){ | |
cb(null,{err:err}); | |
}) | |
} | |
user.remoteMethod( | |
'getUsersData', | |
{ | |
http: {verb: 'get'}, | |
accepts: [ | |
{arg: 'userIds', type: 'array', required: true, description:'userIds'}, | |
{arg: 'format', type: 'string', description:'array | dictionary'} | |
], | |
returns: {arg: 'response', type: 'object'}, | |
description:'Get userData from userId in an Array' | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment