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;
|