1 | var zxy = require('zxy-comm');
|
2 | var _redisClient;
|
3 | var outerror = true;
|
4 | var outconn = true;
|
5 |
|
6 | var 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 |
|
33 |
|
34 | return this;
|
35 | }
|
36 | exports.runRedis = runRedis;
|
37 |
|
38 |
|
39 | var 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 | }
|
55 | exports.set = set;
|
56 |
|
57 | //获取
|
58 | var 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 | }
|
73 | exports.get = get;
|
74 |
|
75 | //删除
|
76 | var del = function (key) {
|
77 | get(key, function (r) {
|
78 | if (r) {
|
79 | _redisClient.del(key);
|
80 | }
|
81 | });
|
82 | }
|
83 | exports.del = del;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | var 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 | }
|
101 | exports.keys = keys;
|
102 |
|
103 |
|
104 | var ttl = function (key, callback) {
|
105 | _redisClient.ttl(key, function (err, time) {
|
106 | if (callback && typeof callback == 'function') {
|
107 | callback(time);
|
108 | }
|
109 | });
|
110 | }
|
111 | exports.ttl = ttl;
|
112 |
|
113 |
|
114 | var 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 | }
|
121 | exports.exists = exists;
|
122 |
|
123 |
|
124 | var rpush = function (key, value) {
|
125 | _redisClient.rpush(key, value);
|
126 | }
|
127 | exports.rpush = rpush;
|
128 |
|
129 |
|
130 | var lpop = function (key, callback) {
|
131 | _redisClient.lpop(key, function (err, value) {
|
132 | if (callback && typeof callback == 'function') {
|
133 | callback(value);
|
134 | }
|
135 | });
|
136 | }
|
137 | exports.lpop = lpop; |
\ | No newline at end of file |