UNPKG

3.71 kBJavaScriptView Raw
1var zxy = require('zxy-comm');
2var _redisClient;
3var outerror = true;
4var outconn = true;
5
6var runRedis = function (_redisConf) {
7 _redisClient = require('redis').createClient(_redisConf);
8 if (_redisConf.password) {
9 _redisClient.auth(_redisConf.password);
10 }
11 if (_redisConf.database) {
12 _redisClient.select(_redisConf.database);
13 }
14 if (!_redisClient.connected) {
15 _redisClient.on("connect", function () {
16 outerror = true;
17 if (outconn) {
18 outconn = false;
19 if (zxy.app.v_singleCoreProcess) {
20 console.info("a single core process[" + process.pid + "] redis connected to", _redisConf.host, ":", _redisConf.port);
21 }
22 }
23 });
24 }
25 _redisClient.on("error", function (err) {
26 outconn = true;
27 if (outerror) {
28 outerror = false;
29 console.error("redis connect error:", err.toString());
30 }
31 });
32 //exports.client = _redisClient;
33 // zxy.app.m_redis = this;
34 return this;
35}
36exports.runRedis = runRedis;
37
38//设置 time单位秒
39var set = function (key, value, time) {
40 if (time) {
41 _redisClient.setex(key, time, value, function (err) {
42 if (err) {
43 console.error(err);
44 }
45 });
46 }
47 else {
48 _redisClient.set(key, value, function (err) {
49 if (err) {
50 console.error(err);
51 }
52 });
53 }
54}
55exports.set = set;
56
57//获取
58var get = function (key, callback) {
59 _redisClient.get(key, function (err, reply) {
60 if (err) {
61 console.error(err);
62 if (callback && typeof callback == 'function') {
63 callback(undefined);
64 }
65 }
66 else {
67 if (callback && typeof callback == 'function') {
68 callback(reply);
69 }
70 }
71 });
72}
73exports.get = get;
74
75//删除
76var del = function (key) {
77 get(key, function (r) {
78 if (r) {
79 _redisClient.del(key);
80 }
81 });
82}
83exports.del = del;
84
85//keys
86//pattern :
87// * 匹配数据库中所有 key 。
88// h?llo 匹配 hello , hallo 和 hxllo 等。
89// h*llo 匹配 hllo 和 heeeeello 等。
90// h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
91var keys = function (pattern, callback) {
92 if (!pattern) {
93 pattern = "*";
94 }
95 _redisClient.keys(pattern, function (err, keys) {
96 if (callback && typeof callback == 'function') {
97 callback(keys);
98 }
99 });
100}
101exports.keys = keys;
102
103//以秒为单位返回 key 的剩余生存时间 如果不存在key或者长期有效 返回-1
104var ttl = function (key, callback) {
105 _redisClient.ttl(key, function (err, time) {
106 if (callback && typeof callback == 'function') {
107 callback(time);
108 }
109 });
110}
111exports.ttl = ttl;
112
113//是否存在key
114var exists = function (key, callback) {
115 _redisClient.exists(key, function (err, exists) {
116 if (callback && typeof callback == 'function') {
117 callback(exists ? true : false);
118 }
119 });
120}
121exports.exists = exists;
122
123//将一个或多个值 value 插入到列表 key 的表尾(最右边)。
124var rpush = function (key, value) {
125 _redisClient.rpush(key, value);
126}
127exports.rpush = rpush;
128
129//移除并返回列表 key 的头元素。
130var lpop = function (key, callback) {
131 _redisClient.lpop(key, function (err, value) {
132 if (callback && typeof callback == 'function') {
133 callback(value);
134 }
135 });
136}
137exports.lpop = lpop;
\No newline at end of file