引入依赖包

1
npm install redis --save

工具类模式redisUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import redis from 'redis';
const redisUtil = {
config: {
url: 'localhost',
port: '6379',
password: '123456'
},
client: null,
createClient(conf) {
this.client = redis.createClient(conf.port, conf.url, {});
this.client.auth(conf.password, function (res) {
console.log(res);
});
const _this = this;
this.client.on('connect', function () {
_this.client.set('author', 'Wilson', redis.print);
_this.client.get('author', redis.print);
console.log('connect');
});
this.client.on('ready', function () {
console.log('ready');
});
},
getClient(){
if(this.client == null){
this.createClient(this.config);
}
return this.client;
},
setKey(key, value) {
const client = this.getClient();
return new Promise((resolve, reject) => {
client.set(key, value, (err, replay) => {
if (err) {
reject(err);
} else {
resolve(replay);
}
})
})
},
getKey(key) {
const client = this.getClient();
return new Promise((resolve, reject) => {
client.get(key, (err, replay) => {
if (err) {
reject(err);
} else {
resolve(replay);
}
})
})
}
}
export default redisUtil;

对象模式

es6语法

定义一个RedisClient对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import redis from 'redis';
class RedisClient {
/**
* 构造函数
* @param {Object} config
*/
constructor(config) {
this.config = config;
this.client = null;
}

/**
* 创建连接
* @param {Object} conf 配置信息
*/
createClient(conf) {
this.client = redis.createClient(conf.port, conf.url, {});
this.client.auth(conf.password, function (res) {
console.log(res);
});
const _this = this;
this.client.on('connect', function () {
_this.client.set('author', 'Wilson', redis.print);
_this.client.get('author', redis.print);
console.log('connect');
});
this.client.on('ready', function () {
console.log('ready');
});
}

/**
* 获取链接
*/
getClient() {
if (this.client == null) {
this.createClient(this.config);
}
return this.client;
}

/**
* 设置值
* @param {String} key
* @param {Object} value
*/
setKey(key, value) {
const client = this.getClient();
return new Promise((resolve, reject) => {
client.set(key, value, (err, replay) => {
if (err) {
reject(err);
} else {
resolve(replay);
}
})
})
}

/**
* 获取值
* @param {String}} key
*/
getKey(key) {
const client = this.getClient();
return new Promise((resolve, reject) => {
client.get(key, (err, replay) => {
if (err) {
reject(err);
} else {
resolve(replay);
}
})
})
}

/**
* 查询key
* @param {String}} pattern
*/
keys(pattern) {
const client = this.getClient();
return new Promise((resolve, reject) => {
client.keys(pattern, (err, replay) => {
if (err) {
reject(err);
} else {
resolve(replay);
}
});
});
}
}
export default RedisClient;

对象的使用方法

1
2
3
4
5
6
7
8
9
10
import RedisClient from "./js/app/RedisClient";
const config = {
url: "localhost",
port: "6379",
password: "123456"
};
const rc = new RedisClient(config);
rc.getKey("test2").then(function(res) {
console.log(res);
});