'use strict' /** * Copyright (c) 2017~2023, OBCon Inc. * All rights reserved. */ /** * @file * @copyright 2017~2023, OBCon Inc. * @author gye hyun james kim [pnuskgh@gmail.com] */ //--- yum list installed | grep redis //--- redis.x86_64 3.2.12-2.el7 //--- //--- Redis package in node.js //--- Redis 3.1.2 for OBCon Desktop, service.obcon.biz, 피지에스코리아, 정보산업진흥원 //--- Redis 4.3.1 for CAProtec //--- 향후 배포시 3.1.2 버전으로 재설치할 것 const redis = require('redis'); //--- https://redis.io/docs/clients/nodejs/ //--- Redis 3.2.12-2.el7 //--- Library : Redis 3.1.2 //--- To-Do : Express의 세션을 redis에 저장하여 관리할 수 있음 //--- redis-cli -h 127.0.0.1 -p 6379 -a '비밀번호' //--- set local_scada_~ ~ //--- get local_scada_devicesInfo //--- get scada_devicesInfo //--- del local_scada_~ //--- keys * #--- 모든 key 조회 //--- flushall #--- 모두 삭제 //--- quit class Redis { constructor(options = null) { try { this._ready = false; this._client = redis.createClient(options || config.redis); //--- connect > ready this._client.on('connect', () => { // console.log('Redis connected.'); }); this._client.on('ready', function() { // const socket = this._client.stream; console.log('Redis ready.'); this._ready = true; }.bind(this)); this._client.on('error', (err) => { console.error('Redis error :', err); }); this._client.on('end', () => { // console.log('Redis end.'); }); this._client.on('reconnecting', () => { // console.log('Redis reconnecting.'); }); // //--- String // this._client.set('hello', 'world'); // this._client.get('hello', (err, reply) => { // if (err) { // console.error(err); // } else { // console.log('--- get'); // console.log(reply); // } // }); // if (this._client.exists('hello')) { // this._client.del('hello'); // } // //--- Object // this._client.hmset('friends', 'name', 'zero', 'age', 24); // this._client.hgetall('friends', (err, obj) => { // if (err) { // console.error(err); // } else { // console.log('--- hgetall'); // console.log(obj); // } // }); // //--- List // this._client.rpush('fruits', 'apple', 'orange', 'apple'); // this._client.lpush('fruits', 'banana', 'pear'); // this._client.lrange('fruits', 0, -1, (err, arr) => { // if (err) { // console.error(err); // } else { // console.log('--- lrange'); // console.log(arr); // } // }); // //--- Set // this._client.sadd('animals', 'dog', 'cat', 'bear', 'cat', 'lion'); // this._client.smembers('animals', (err, set) => { // if (err) { // console.error(err); // } else { // console.log('--- smembers'); // console.log(set); // } // }); // //--- Sorted set // this._client.zadd('height', 180, 'zero', 168, 'aero', 176, 'nero', 172, 'hero'); // this._client.zrange('height', 0, -1, (err, sset) => { // if (err) { // console.error(err); // } else { // console.log('--- zrange'); // console.log(sset); // } // }); // //--- 위도와 경도 // this._client.geoadd('cities', 126.97, 37.56, 'seoul', 129.07, 35.17, 'busan', 126.70, 37.45, 'incheon'); // this._client.geodist('cities', 'seoul', 'busan', (err, dist) => { // if (err) { // console.error(err); // } else { // console.log('--- geodist'); // console.log(dist); // } // }); // this._client.georadius('cities', 126.8, 37.5, 50, 'km', (err, cities) => { // if (err) { // console.error(err); // } else { // console.log('--- georadius'); // console.log(cities); // } // }); } catch(ex) { console.error(ex); this._client = null; } } get client() { return this._client; } get ready() { return this._ready; } exists(name, prefix = config.redis.clientIdPrefix) { return this._client.exists(prefix + name); } async set(name, obj, prefix = config.redis.clientIdPrefix) { try { this._client.set(prefix + name, JSON.stringify(obj)); return true; // //--- 1초 동안 저장 되기를 기다린다. // for (let idx = 0; idx < 50; idx++) { // await utils.sleep(20); // if (this.exists(name, prefix)) { // return true; // } // } // return false; } catch (err) { utils.obj.loggerError(err); return false; } } async get(name, prefix = config.redis.clientIdPrefix) { try { return await this._get(name, prefix); } catch (err) { utils.obj.loggerError(err); return null; } } _get(name, prefix = config.redis.clientIdPrefix) { return new Promise(function(resolve, reject) { if (this.exists(name, prefix)) { this._client.get(prefix + name, (err, reply) => { if (err == null) { resolve((reply == null) ? null:JSON.parse(reply)); } else { console.error(err); resolve(null) } }); } else { resolve(null); } }.bind(this)); } async keys(pattern, prefix = config.redis.clientIdPrefix) { try { return await this._keys(pattern, prefix); } catch (err) { utils.obj.loggerError(err); return null; } } _keys(pattern, prefix = config.redis.clientIdPrefix) { return new Promise(function(resolve, reject) { this._client.keys(`${prefix}${pattern}`, function(err, replies) { if (err == null) { resolve(replies); } else { console.error(err); resolve([]); } }); }.bind(this)); } async del(name, prefix = config.redis.clientIdPrefix) { try { if (this.exists(name, prefix)) { this._client.del(prefix + name); } return true; // //--- 1초 동안 삭제 되기를 기다린다. // for (let idx = 0; idx < 50; idx++) { // await utils.sleep(20); // if (this.exists(name, prefix) == false) { // return true; // } // } // return false; } catch (err) { utils.obj.loggerError(err); return false; } } } module.exports = Redis;