Last active
August 29, 2015 14:12
-
-
Save huaxinjiayou/ca61afd7ad9baf5b33f2 to your computer and use it in GitHub Desktop.
360duzhe.com 封装、使用的 redis 插件库,分别为 windjs 编译前后的代码;其中 _require 是对 require 的个性定制,所有路径都相对于 app 根路径
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 Redis = require('redis'); | |
var _ = require('underscore'); | |
var clazz = _require('util/class'); | |
var util = _require('util/util'); | |
var RedisUtil = clazz.create(); | |
var oRedisConf = nConf.account.redis; | |
_.extend(RedisUtil.prototype, { | |
// redis 可用状态 | |
redisReady : true, | |
initialize : fRedisUtilInitialize, | |
set : eval(util.wind(fRedisUtilSet)), | |
get : eval(util.wind(fRedisUtilGet)), | |
del : eval(util.wind(fRedisUtilDel)), | |
// 序列化 和 反序列化 | |
serialize : fRedisUtilSerialize, | |
deserialize : fRedisUtilDeserialize | |
}); | |
_.extend(RedisUtil, { | |
initSyncMethod: fRedisUtilSyncMethod | |
}); | |
// 返回一个对象 | |
module.exports = new RedisUtil(); | |
function fRedisUtilSyncMethod(oInstance, oClient) { | |
var aMethod = ['get', 'set', 'setnx', 'setex', 'append', 'strlen', 'del', 'exists', 'setbit', 'getbit', 'setrange', 'getrange', 'substr', | |
'incr', 'decr', 'mget', 'rpush', 'lpush', 'rpushx', 'lpushx', 'linsert', 'rpop', 'lpop', 'brpop', 'brpoplpush', 'blpop', 'llen', 'lindex', | |
'lset', 'lrange', 'ltrim', 'lrem', 'rpoplpush', 'sadd', 'srem', 'smove', 'sismember', 'scard', 'spop', 'srandmember', 'sinter', 'sinterstore', | |
'sunion', 'sunionstore', 'sdiff', 'sdiffstore', 'smembers', 'zadd', 'zincrby', 'zrem', 'zremrangebyscore', 'zremrangebyrank', 'zunionstore', | |
'zinterstore', 'zrange', 'zrangebyscore', 'zrevrangebyscore', 'zcount', 'zrevrange', 'zcard', 'zscore', 'zrank', 'zrevrank', 'hset', 'hsetnx', | |
'hget', 'hmset', 'hmget', 'hincrby', 'hdel', 'hlen', 'hkeys', 'hvals', 'hgetall', 'hexists', 'incrby', 'decrby', 'getset', 'mset', 'msetnx', | |
'randomkey', 'select', 'move', 'rename', 'renamenx', 'expire', 'expireat', 'keys', 'dbsize', 'auth', 'ping', 'echo', 'save', 'bgsave', | |
'bgrewriteaof', 'shutdown', 'lastsave', 'type', 'multi', 'exec', 'discard', 'sync', 'flushdb', 'flushall', 'sort', 'info', 'monitor', 'ttl', | |
'persist', 'slaveof', 'debug', 'config', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe', 'publish', 'watch', 'unwatch', 'cluster', | |
'restore', 'migrate', 'dump', 'object', 'client', 'eval', 'evalsha']; | |
// 不需要继承的方法 | |
var oUnInheritMethod = {get: 1, set: 1, del: 1, client: 1}; | |
// 初始化容器 | |
// 全部转换一遍,再进行缓存 | |
if (!oInstance.syncMethod) { | |
oInstance.syncMethod = {}; | |
} | |
var oSyncMethodMap = oInstance.syncMethod; | |
aMethod.forEach(function (sMethod) { | |
oSyncMethodMap[sMethod] = Wind.Async.Binding.fromStandard(function () { | |
oClient[sMethod].apply(oClient, arguments); | |
}); | |
if (!oUnInheritMethod[sMethod]) { | |
oInstance[sMethod] = Wind.Async.Binding.fromStandard(function () { | |
oClient[sMethod].apply(oClient, arguments); | |
}); | |
} | |
}); | |
} | |
function fRedisUtilInitialize() { | |
var that = this; | |
that.client = new Redis.createClient(oRedisConf.port, oRedisConf.host, {}); | |
// 将 redis 的方法 wind 类型 | |
RedisUtil.initSyncMethod(that, that.client); | |
// password | |
if (oRedisConf.password) { | |
that.client.auth(oRedisConf.password, function (oErr) { | |
if (oErr) { | |
throw oErr; | |
} | |
}); | |
} | |
// db | |
that.client.select(oRedisConf.cache); | |
that.client.on('connect', function () { | |
that.client.send_anyways = true; | |
that.client.select(oRedisConf.cache); | |
that.client.send_anyways = false; | |
}); | |
that.client.on('error', function () { | |
that.redisReady = false; | |
}); | |
that.client.on('connect', function () { | |
that.redisReady = true; | |
}); | |
} | |
function fRedisUtilSet(sKey, sVal, nDuration) { | |
var that = this; | |
// 不可用 | |
if (!that.redisReady) { | |
return; | |
} | |
// 先保存到对象中,再降对象 | |
var sSaveStr = that.serialize(sVal); | |
if (!nDuration) { | |
$await(that.syncMethod.set(sKey, sSaveStr)); | |
} else { | |
$await(that.syncMethod.setex(sKey, nDuration, sSaveStr)); | |
} | |
return true; | |
} | |
function fRedisUtilGet(sKey) { | |
var that = this; | |
// 不可用 | |
if (!that.redisReady) { | |
return; | |
} | |
var sSaveStr = $await(that.syncMethod.get(sKey)); | |
if (!sSaveStr) { | |
return null; | |
} | |
return that.deserialize(sSaveStr); | |
} | |
function fRedisUtilDel(sKey, bBlur) { | |
if (util.isArray(sKey)) { | |
sKey = sKey.join(','); | |
} else if (!util.isString(sKey)) { | |
return; | |
} | |
var that = this; | |
var aKeys = util.split(sKey); | |
if (aKeys.length === 0) { | |
return; | |
} | |
// 去重 | |
aKeys = _.uniq(aKeys); | |
if (!bBlur) { | |
// 非模糊匹配 | |
for (var i = 0, l = aKeys.length; i < l; i++) { | |
$await(that.syncMethod.del(aKeys[i])); | |
} | |
} else { | |
var aDelKeys = []; | |
var aTmpKeys; | |
for (var i = 0, l = aKeys.length; i < l; i++) { | |
aTmpKeys = $await(that.syncMethod.keys(aKeys[i])); | |
if (aTmpKeys.length > 0) { | |
aDelKeys = aDelKeys.concat(aTmpKeys); | |
} | |
} | |
if (aDelKeys.length === 0) { | |
return; | |
} | |
aDelKeys = _.uniq(aDelKeys); | |
// 递归调用吧 | |
return $await(that.del(aDelKeys.join(','))); | |
} | |
} | |
function fRedisUtilSerialize(sVal) { | |
return JSON.stringify({data: sVal}); | |
} | |
function fRedisUtilDeserialize(sVal) { | |
var oData = {}; | |
try { | |
oData = JSON.parse(sVal); | |
} catch (e) {} | |
return oData.data; | |
} |
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 Redis = require("redis"); | |
var _ = require("underscore"); | |
var clazz = _require("util/class"); | |
var util = _require("util/util"); | |
var RedisUtil = clazz.create(); | |
var oRedisConf = nConf.account.redis; | |
_.extend(RedisUtil.prototype, { | |
redisReady: true, | |
initialize: fRedisUtilInitialize, | |
set: /* async << function (sKey, sVal, nDuration) { */ (function fRedisUtilSet(sKey, sVal, nDuration) { | |
var _builder_$0 = Wind.builders["async"]; | |
return _builder_$0.Start(this, | |
_builder_$0.Delay(function () { | |
/* var that = this; */ var that = this; | |
/* if (! that.redisReady) { */ if (! that.redisReady) { | |
/* return; */ return _builder_$0.Return(); | |
/* } */ } | |
/* var sSaveStr = that.serialize(sVal); */ var sSaveStr = that.serialize(sVal); | |
return _builder_$0.Combine( | |
_builder_$0.Delay(function () { | |
/* if (! nDuration) { */ if (! nDuration) { | |
/* $await(that.syncMethod.set(sKey, sSaveStr)); */ return _builder_$0.Bind(that.syncMethod.set(sKey, sSaveStr), function () { | |
return _builder_$0.Normal(); | |
}); | |
/* } else { */ } else { | |
/* $await(that.syncMethod.setex(sKey, nDuration, sSaveStr)); */ return _builder_$0.Bind(that.syncMethod.setex(sKey, nDuration, sSaveStr), function () { | |
return _builder_$0.Normal(); | |
}); | |
/* } */ } | |
}), | |
_builder_$0.Delay(function () { | |
/* return true; */ return _builder_$0.Return(true); | |
}) | |
); | |
}) | |
); | |
/* } */ }) | |
//@ sourceURL=wind/403_fRedisUtilSet.js | |
, | |
get: /* async << function (sKey) { */ (function fRedisUtilGet(sKey) { | |
var _builder_$0 = Wind.builders["async"]; | |
return _builder_$0.Start(this, | |
_builder_$0.Delay(function () { | |
/* var that = this; */ var that = this; | |
/* if (! that.redisReady) { */ if (! that.redisReady) { | |
/* return; */ return _builder_$0.Return(); | |
/* } */ } | |
/* var sSaveStr = $await(that.syncMethod.get(sKey)); */ return _builder_$0.Bind(that.syncMethod.get(sKey), function (sSaveStr) { | |
/* if (! sSaveStr) { */ if (! sSaveStr) { | |
/* return null; */ return _builder_$0.Return(null); | |
/* } */ } | |
/* return that.deserialize(sSaveStr); */ return _builder_$0.Return(that.deserialize(sSaveStr)); | |
}); | |
}) | |
); | |
/* } */ }) | |
//@ sourceURL=wind/404_fRedisUtilGet.js | |
, | |
del: /* async << function (sKey, bBlur) { */ (function fRedisUtilDel(sKey, bBlur) { | |
var _builder_$0 = Wind.builders["async"]; | |
return _builder_$0.Start(this, | |
_builder_$0.Delay(function () { | |
/* if (util.isArray(sKey)) { */ if (util.isArray(sKey)) { | |
/* sKey = sKey.join(","); */ sKey = sKey.join(","); | |
/* } else if (! util.isString(sKey)) { */ } else if (! util.isString(sKey)) { | |
/* return; */ return _builder_$0.Return(); | |
/* } */ } | |
/* var that = this; */ var that = this; | |
/* var aKeys = util.split(sKey); */ var aKeys = util.split(sKey); | |
/* if (aKeys.length === 0) { */ if (aKeys.length === 0) { | |
/* return; */ return _builder_$0.Return(); | |
/* } */ } | |
/* aKeys = _.uniq(aKeys); */ aKeys = _.uniq(aKeys); | |
/* if (! bBlur) { */ if (! bBlur) { | |
/* var i = 0, l = aKeys.length; */ var i = 0, l = aKeys.length; | |
/* for ( */ return _builder_$0.For(function () { | |
/* ; i < l */ return i < l; | |
}, function () { | |
/* ; i ++) { */ i ++; | |
}, | |
_builder_$0.Delay(function () { | |
/* $await(that.syncMethod.del(aKeys[i])); */ return _builder_$0.Bind(that.syncMethod.del(aKeys[i]), function () { | |
return _builder_$0.Normal(); | |
}); | |
}) | |
/* } */ ); | |
/* } else { */ } else { | |
/* var aDelKeys = []; */ var aDelKeys = []; | |
/* var aTmpKeys; */ var aTmpKeys; | |
return _builder_$0.Combine( | |
_builder_$0.Delay(function () { | |
/* var i = 0, l = aKeys.length; */ var i = 0, l = aKeys.length; | |
/* for ( */ return _builder_$0.For(function () { | |
/* ; i < l */ return i < l; | |
}, function () { | |
/* ; i ++) { */ i ++; | |
}, | |
_builder_$0.Delay(function () { | |
/* var _result_$ = $await(that.syncMethod.keys(aKeys[i])); */ return _builder_$0.Bind(that.syncMethod.keys(aKeys[i]), function (_result_$) { | |
/* aTmpKeys = _result_$; */ aTmpKeys = _result_$; | |
/* if (aTmpKeys.length > 0) { */ if (aTmpKeys.length > 0) { | |
/* aDelKeys = aDelKeys.concat(aTmpKeys); */ aDelKeys = aDelKeys.concat(aTmpKeys); | |
/* } */ } | |
return _builder_$0.Normal(); | |
}); | |
}) | |
/* } */ ); | |
}), | |
_builder_$0.Delay(function () { | |
/* if (aDelKeys.length === 0) { */ if (aDelKeys.length === 0) { | |
/* return; */ return _builder_$0.Return(); | |
/* } */ } | |
/* aDelKeys = _.uniq(aDelKeys); */ aDelKeys = _.uniq(aDelKeys); | |
/* return $await(that.del(aDelKeys.join(","))); */ return _builder_$0.Bind(that.del(aDelKeys.join(",")), function (_result_$) { | |
return _builder_$0.Return(_result_$); | |
}); | |
}) | |
); | |
/* } */ } | |
}) | |
); | |
/* } */ }) | |
//@ sourceURL=wind/405_fRedisUtilDel.js | |
, | |
serialize: fRedisUtilSerialize, | |
deserialize: fRedisUtilDeserialize | |
}); | |
_.extend(RedisUtil, { | |
initSyncMethod: fRedisUtilSyncMethod | |
}); | |
module.exports = new RedisUtil; | |
function fRedisUtilSyncMethod(oInstance, oClient) { | |
var aMethod = [ "get", "set", "setnx", "setex", "append", "strlen", "del", "exists", "setbit", "getbit", "setrange", "getrange", "substr", "incr", "decr", "mget", "rpush", "lpush", "rpushx", "lpushx", "linsert", "rpop", "lpop", "brpop", "brpoplpush", "blpop", "llen", "lindex", "lset", "lrange", "ltrim", "lrem", "rpoplpush", "sadd", "srem", "smove", "sismember", "scard", "spop", "srandmember", "sinter", "sinterstore", "sunion", "sunionstore", "sdiff", "sdiffstore", "smembers", "zadd", "zincrby", "zrem", "zremrangebyscore", "zremrangebyrank", "zunionstore", "zinterstore", "zrange", "zrangebyscore", "zrevrangebyscore", "zcount", "zrevrange", "zcard", "zscore", "zrank", "zrevrank", "hset", "hsetnx", "hget", "hmset", "hmget", "hincrby", "hdel", "hlen", "hkeys", "hvals", "hgetall", "hexists", "incrby", "decrby", "getset", "mset", "msetnx", "randomkey", "select", "move", "rename", "renamenx", "expire", "expireat", "keys", "dbsize", "auth", "ping", "echo", "save", "bgsave", "bgrewriteaof", "shutdown", "lastsave", "type", "multi", "exec", "discard", "sync", "flushdb", "flushall", "sort", "info", "monitor", "ttl", "persist", "slaveof", "debug", "config", "subscribe", "unsubscribe", "psubscribe", "punsubscribe", "publish", "watch", "unwatch", "cluster", "restore", "migrate", "dump", "object", "client", "eval", "evalsha" ]; | |
var oUnInheritMethod = { | |
get: 1, | |
set: 1, | |
del: 1, | |
client: 1 | |
}; | |
if (!oInstance.syncMethod) { | |
oInstance.syncMethod = {}; | |
} | |
var oSyncMethodMap = oInstance.syncMethod; | |
aMethod.forEach(function(sMethod) { | |
oSyncMethodMap[sMethod] = Wind.Async.Binding.fromStandard(function() { | |
oClient[sMethod].apply(oClient, arguments); | |
}); | |
if (!oUnInheritMethod[sMethod]) { | |
oInstance[sMethod] = Wind.Async.Binding.fromStandard(function() { | |
oClient[sMethod].apply(oClient, arguments); | |
}); | |
} | |
}); | |
} | |
function fRedisUtilInitialize() { | |
var that = this; | |
that.client = new Redis.createClient(oRedisConf.port, oRedisConf.host, {}); | |
RedisUtil.initSyncMethod(that, that.client); | |
if (oRedisConf.password) { | |
that.client.auth(oRedisConf.password, function(oErr) { | |
if (oErr) { | |
throw oErr; | |
} | |
}); | |
} | |
that.client.select(oRedisConf.cache); | |
that.client.on("connect", function() { | |
that.client.send_anyways = true; | |
that.client.select(oRedisConf.cache); | |
that.client.send_anyways = false; | |
}); | |
that.client.on("error", function() { | |
that.redisReady = false; | |
}); | |
that.client.on("connect", function() { | |
that.redisReady = true; | |
}); | |
} | |
function fRedisUtilSet(sKey, sVal, nDuration) { | |
var that = this; | |
if (!that.redisReady) { | |
return; | |
} | |
var sSaveStr = that.serialize(sVal); | |
if (!nDuration) { | |
$await(that.syncMethod.set(sKey, sSaveStr)); | |
} else { | |
$await(that.syncMethod.setex(sKey, nDuration, sSaveStr)); | |
} | |
return true; | |
} | |
function fRedisUtilGet(sKey) { | |
var that = this; | |
if (!that.redisReady) { | |
return; | |
} | |
var sSaveStr = $await(that.syncMethod.get(sKey)); | |
if (!sSaveStr) { | |
return null; | |
} | |
return that.deserialize(sSaveStr); | |
} | |
function fRedisUtilDel(sKey, bBlur) { | |
if (util.isArray(sKey)) { | |
sKey = sKey.join(","); | |
} else if (!util.isString(sKey)) { | |
return; | |
} | |
var that = this; | |
var aKeys = util.split(sKey); | |
if (aKeys.length === 0) { | |
return; | |
} | |
aKeys = _.uniq(aKeys); | |
if (!bBlur) { | |
for (var i = 0, l = aKeys.length; i < l; i++) { | |
$await(that.syncMethod.del(aKeys[i])); | |
} | |
} else { | |
var aDelKeys = []; | |
var aTmpKeys; | |
for (var i = 0, l = aKeys.length; i < l; i++) { | |
aTmpKeys = $await(that.syncMethod.keys(aKeys[i])); | |
if (aTmpKeys.length > 0) { | |
aDelKeys = aDelKeys.concat(aTmpKeys); | |
} | |
} | |
if (aDelKeys.length === 0) { | |
return; | |
} | |
aDelKeys = _.uniq(aDelKeys); | |
return $await(that.del(aDelKeys.join(","))); | |
} | |
} | |
function fRedisUtilSerialize(sVal) { | |
return JSON.stringify({ | |
data: sVal | |
}); | |
} | |
function fRedisUtilDeserialize(sVal) { | |
var oData = {}; | |
try { | |
oData = JSON.parse(sVal); | |
} catch (e) {} | |
return oData.data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment