thinknode
Version:
A fast, flexible and all-in-one web framework for node.js.
453 lines (337 loc) • 11.4 kB
JavaScript
'use strict';
exports.__esModule = true;
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _Cache = require('./Cache');
var _Cache2 = _interopRequireDefault(_Cache);
var _RedisSocket = require('../Socket/RedisSocket');
var _RedisSocket2 = _interopRequireDefault(_RedisSocket);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
*
* @author richen
* @copyright Copyright (c) 2015 - <richenlin(at)gmail.com>
* @license MIT
* @version 15/12/3
*/
var _class = function (_cache) {
(0, _inherits3.default)(_class, _cache);
function _class() {
(0, _classCallCheck3.default)(this, _class);
return (0, _possibleConstructorReturn3.default)(this, _cache.apply(this, arguments));
}
_class.prototype.init = function init(options) {
_cache.prototype.init.call(this, options);
var key = THINK.hash(this.options.redis_host + '_' + this.options.redis_port + '_' + this.options.redis_db);
if (!(key in THINK_CACHES.INSTANCES.CACHE)) {
THINK_CACHES.INSTANCES.CACHE[key] = new _RedisSocket2.default(this.options);
}
this.handle = THINK_CACHES.INSTANCES.CACHE[key];
//兼容旧版方法名
this.batchRm = this.batchrm;
this.incrBy = this.incrby;
this.hSet = this.hset;
this.hGet = this.hget;
this.hExists = this.hexists;
this.hLen = this.hlen;
this.hIncrBy = this.hincrby;
this.hGetAll = this.hgetall;
this.hKeys = this.hkeys;
this.hVals = this.hvals;
this.hDel = this.hdel;
this.lLen = this.llen;
this.rPush = this.rpush;
this.lPop = this.lpop;
this.lRange = this.lrange;
this.sAdd = this.sadd;
this.sCard = this.scard;
this.sisMember = this.sismember;
this.sMembers = this.smembers;
this.sPop = this.spop;
this.sRem = this.srem;
};
/**
* 字符串获取
* @param name
*/
_class.prototype.get = function get(name) {
return this.handle.wrap('get', [this.options.cache_key_prefix + name]);
};
/**
* 字符串写入
* @param name
* @param value
* @param timeout
* @returns {Promise}
*/
_class.prototype.set = function set(name, value) {
var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.options.cache_timeout;
var setP = [this.handle.wrap('set', [this.options.cache_key_prefix + name, value])];
if (typeof timeout === 'number') {
setP.push(this.handle.wrap('expire', [this.options.cache_key_prefix + name, timeout]));
}
return _promise2.default.all(setP);
};
/**
* 以秒为单位,返回给定 key 的剩余生存时间
* @param name
* @returns {*}
*/
_class.prototype.ttl = function ttl(name) {
return this.handle.wrap('ttl', [this.options.cache_key_prefix + name]);
};
/**
* 设置key超时属性
* @param name
* @param timeout
*/
_class.prototype.expire = function expire(name) {
var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.options.cache_timeout;
return this.handle.wrap('expire', [this.options.cache_key_prefix + name, timeout]);
};
/**
* 删除key
* @param name
*/
_class.prototype.rm = function rm(name) {
return this.handle.wrap('del', [this.options.cache_key_prefix + name]);
};
/**
* 批量删除,可模糊匹配
* @param keyword
* @returns {*}
*/
_class.prototype.batchrm = function batchrm(keyword) {
var _this2 = this;
return this.handle.wrap('keys', keyword + '*').then(function (keys) {
if (THINK.isEmpty(keys)) {
return null;
}
return _this2.handle.wrap('del', [keys]);
});
};
/**
* 判断key是否存在
* @param name
*/
_class.prototype.exists = function exists(name) {
return this.handle.wrap('exists', [this.options.cache_key_prefix + name]);
};
/**
* 查找所有符合给定模式 pattern 的 key
* @param pattern
*/
_class.prototype.keys = function keys(pattern) {
return this.handle.wrap('keys', [pattern]);
};
/**
* 自增
* @param name
*/
_class.prototype.incr = function incr(name) {
return this.handle.wrap('incr', [this.options.cache_key_prefix + name]);
};
/**
* 自减
* @param name
* @returns {*}
*/
_class.prototype.decr = function decr(name) {
return this.handle.wrap('decr', [this.options.cache_key_prefix + name]);
};
/**
* 字符key增加指定长度
* @param name
* @param incr
* @returns {*}
*/
_class.prototype.incrby = function incrby(name, incr) {
incr = incr || 1;
return this.handle.wrap('incrby', [this.options.cache_key_prefix + name, incr]);
};
/**
* 哈希写入
* @param name
* @param key
* @param value
* @param timeout
*/
_class.prototype.hset = function hset(name, key, value) {
var timeout = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : this.options.cache_timeout;
var setP = [this.handle.wrap('hset', [this.options.cache_key_prefix + name, key, value])];
if (typeof timeout === 'number') {
setP.push(this.handle.wrap('expire', [this.options.cache_key_prefix + name, timeout]));
}
return _promise2.default.all(setP);
};
/**
* 哈希获取
* @param name
* @param key
* @returns {*}
*/
_class.prototype.hget = function hget(name, key) {
return this.handle.wrap('hget', [this.options.cache_key_prefix + name, key]);
};
/**
* 查看哈希表 hashKey 中,给定域 key 是否存在
* @param name
* @param key
* @returns {*}
*/
_class.prototype.hexists = function hexists(name, key) {
return this.handle.wrap('hexists', [this.options.cache_key_prefix + name, key]);
};
/**
* 返回哈希表 key 中域的数量
* @param name
* @returns {*}
*/
_class.prototype.hlen = function hlen(name) {
return this.handle.wrap('hlen', [this.options.cache_key_prefix + name]);
};
/**
* 给哈希表指定key,增加increment
* @param name
* @param key
* @param incr
* @returns {*}
*/
_class.prototype.hincrby = function hincrby(name, key) {
var incr = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
return this.handle.wrap('hincrby', [this.options.cache_key_prefix + name, key, incr]);
};
/**
* 返回哈希表所有key-value
* @param name
* @returns {*}
*/
_class.prototype.hgetall = function hgetall(name) {
return this.handle.wrap('hgetall', [this.options.cache_key_prefix + name]);
};
/**
* 返回哈希表所有key
* @param name
* @returns {*}
*/
_class.prototype.hkeys = function hkeys(name) {
return this.handle.wrap('hkeys', [this.options.cache_key_prefix + name]);
};
/**
* 返回哈希表所有value
* @param name
* @returns {*}
*/
_class.prototype.hvals = function hvals(name) {
return this.handle.wrap('hvals', [this.options.cache_key_prefix + name]);
};
/**
* 哈希删除
* @param name
* @param key
* @returns {*}
*/
_class.prototype.hdel = function hdel(name, key) {
return this.handle.wrap('hdel', [this.options.cache_key_prefix + name, key]);
};
/**
* 判断列表长度,若不存在则表示为空
* @param name
* @returns {*}
*/
_class.prototype.llen = function llen(name) {
return this.handle.wrap('llen', [this.options.cache_key_prefix + name]);
};
/**
* 将值插入列表表尾
* @param name
* @param value
* @returns {*}
*/
_class.prototype.rpush = function rpush(name, value) {
return this.handle.wrap('rpush', [this.options.cache_key_prefix + name, value]);
};
/**
* 将列表表头取出,并去除
* @param name
* @returns {*}
*/
_class.prototype.lpop = function lpop(name) {
return this.handle.wrap('lpop', [this.options.cache_key_prefix + name]);
};
/**
* 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定
* @param name
* @param start
* @param stop
* @returns {*}
*/
_class.prototype.lrange = function lrange(name, start, stop) {
return this.handle.wrap('lrange', [this.options.cache_key_prefix + name, start, stop]);
};
/**
* 集合新增
* @param name
* @param value
* @param timeout
* @returns {*}
*/
_class.prototype.sadd = function sadd(name, value) {
var timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.options.cache_timeout;
var setP = [this.handle.wrap('sadd', [this.options.cache_key_prefix + name, value])];
if (typeof timeout === 'number') {
setP.push(this.handle.wrap('expire', [this.options.cache_key_prefix + name, timeout]));
}
return _promise2.default.all(setP);
};
/**
* 返回集合的基数(集合中元素的数量)
* @param name
* @returns {*}
*/
_class.prototype.scard = function scard(name) {
return this.handle.wrap('scard', [this.options.cache_key_prefix + name]);
};
/**
* 判断 member 元素是否集合的成员
* @param name
* @param key
* @returns {*}
*/
_class.prototype.sismember = function sismember(name, key) {
return this.handle.wrap('sismember', [this.options.cache_key_prefix + name, key]);
};
/**
* 返回集合中的所有成员
* @param name
* @returns {*}
*/
_class.prototype.smembers = function smembers(name) {
return this.handle.wrap('smembers', [this.options.cache_key_prefix + name]);
};
/**
* 移除并返回集合中的一个随机元素
* @param name
* @returns {*}
*/
_class.prototype.spop = function spop(name) {
return this.handle.wrap('spop', [this.options.cache_key_prefix + name]);
};
/**
* 移除集合 key 中的一个 member 元素
* @param name
* @param key
* @returns {*}
*/
_class.prototype.srem = function srem(name, key) {
return this.handle.wrap('srem', [this.options.cache_key_prefix + name, key]);
};
return _class;
}(_Cache2.default);
exports.default = _class;