merged private server and console to private-cloud
This commit is contained in:
22
console/mini_poem/cache/base_cache.js
vendored
Normal file
22
console/mini_poem/cache/base_cache.js
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Created by strawmanbobi
|
||||
* 2014-12-01.
|
||||
*/
|
||||
|
||||
var BaseCache = function(_cacheType, _host, _port, _user, _password) {
|
||||
throw new Error("Abstract class");
|
||||
};
|
||||
|
||||
BaseCache.prototype.set = function(key, value, ttl, callback) {
|
||||
throw new Error("Could not implemented");
|
||||
};
|
||||
|
||||
BaseCache.prototype.get = function(key, callback) {
|
||||
throw new Error("Could not implemented");
|
||||
};
|
||||
|
||||
BaseCache.prototype.delete = function(key, callback) {
|
||||
throw new Error("Could not implemented");
|
||||
};
|
||||
|
||||
module.exports = BaseCache;
|
||||
71
console/mini_poem/cache/redis.js
vendored
Normal file
71
console/mini_poem/cache/redis.js
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Created by strawmanbobi
|
||||
* 2016-11-24
|
||||
*/
|
||||
|
||||
require('../configuration/constants');
|
||||
var ErrorCode = require('../configuration/error_code');
|
||||
var Enums = require('../configuration/enums');
|
||||
var BaseCache = require('./base_cache.js');
|
||||
|
||||
var redis = require("redis");
|
||||
|
||||
var logger = require('../logging/logger4js').helper;
|
||||
|
||||
var errorCode = new ErrorCode();
|
||||
var enums = new Enums();
|
||||
|
||||
var Cache = function(_host, _port, _user, _password) {
|
||||
this.redisClient = redis.createClient(_port, _host, {detect_buffers: true});
|
||||
// initialize client according to run-time ENV
|
||||
// in _user indicates the redis instance:token pair value
|
||||
if(null != _password) {
|
||||
logger.info("Redis needs authorization");
|
||||
this.redisClient.auth(_password, redis.print);
|
||||
}
|
||||
logger.info("Redis client connected");
|
||||
};
|
||||
|
||||
Cache.prototype = Object.create(BaseCache.prototype);
|
||||
|
||||
Cache.prototype.set = function(key, value, ttl, callback) {
|
||||
this.redisClient.set(key, value, function(err) {
|
||||
if(err) {
|
||||
logger.error("Redis set value failed with key " + key);
|
||||
callback(errorCode.FAILED);
|
||||
} else {
|
||||
callback(errorCode.SUCCESS);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Cache.prototype.get = function(key, isBuffer, callback) {
|
||||
if(true == isBuffer) {
|
||||
this.redisClient.get(new Buffer(key), function (err, reply) {
|
||||
if(err) {
|
||||
logger.error("Redis get buffer failed with key " + key);
|
||||
this.redisClient.end();
|
||||
callback(errorCode.FAILED, null);
|
||||
} else {
|
||||
this.redisClient.end();
|
||||
callback(errorCode.SUCCESS, reply);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.redisClient.get(key, function(err, reply) {
|
||||
if(err) {
|
||||
logger.error("Redis get value failed with key " + key);
|
||||
this.redisClient.end();
|
||||
callback(errorCode.FAILED, null);
|
||||
} else {
|
||||
callback(errorCode.SUCCESS, reply);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Cache.prototype.delete = function(key, callback) {
|
||||
callback(errorCode.SUCCESS);
|
||||
};
|
||||
|
||||
module.exports = Cache;
|
||||
Reference in New Issue
Block a user