UNPKG

ts-api-core

Version:

Nodejs api framework core

154 lines 5.34 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheAccess = void 0; const ioc_decorator_1 = require("../../mvc/ioc.decorator"); const package_loader_1 = require("../package.loader"); /** * Redis 缓存数据通道 */ let CacheAccess = class CacheAccess extends ioc_decorator_1.BaseProvider { onFree() { this.quitClient(); } get redis() { if (!this._redis) { this._redis = package_loader_1.PackageLoader.load('redis'); } return this._redis; } /** * 获取一个redis客户端 */ getRedis() { if (this._client) return this._client; const client = this.redis.createClient({ host: this.config.host, port: this.config.port, username: this.config.username, password: this.config.password, }); this._client = client; return client; } /** 初始化客户端, 允许不初始化, 每次临时生成 */ initClient() { this.getRedis(); } /** 关闭客户端 */ quitClient() { if (this._client) { this._client.quit(); this._client = undefined; } } execute(doTask) { return new Promise((resolve, reject) => { const isTemp = !this._client; const client = this.getRedis(); const task = doTask(client); task.then((data) => { if (isTemp) { this.quitClient(); } resolve(data); }).catch(e => { if (isTemp) { this.quitClient(); } reject(e); }); }); } /** * redis.get * @param key 缓存的key */ get(key) { return __awaiter(this, void 0, void 0, function* () { const value = yield this.execute((client) => client.get(key)); return value !== null && value !== void 0 ? value : ''; }); } /** * redis.set * @param key 缓存的key * @param value 缓存的值 * @param expire (可选) 缓存有效期 单位秒 */ set(key, value, expire) { return this.execute((client) => expire !== undefined && expire > 0 ? client.setEx(key, expire, value) : client.set(key, value)); } /** * redis.del 删除 * @param key 需要删除的key */ remove(key) { return this.execute((client) => client.del(key)); } /** * 检查key是否存在 * @param key */ exists(key) { return this.execute((client) => client.exists(key)); } /** * 设置key的有效期 单位秒 * @param key * @param ttl 有效期 单位秒 */ expire(key, ttl) { return this.execute((client) => client.expire(key, ttl)); } /** * Set the string value of a key and return its old value. * if old value not exists,return nil * @param key * @param value new value */ getSet(key, value) { return __awaiter(this, void 0, void 0, function* () { const data = yield this.execute((client) => client.getSet(key, value)); return data !== null && data !== void 0 ? data : ''; }); } /** * 查找key * @param partykey */ search(partykey) { return this.execute((client) => client.keys(partykey)); } /** * Set the value of a key, only if the key does not exist. * 设置成功,返回 1 。 设置失败,返回 0 。 * @param key * @param value */ setNX(key, value) { return this.execute((client) => client.setNX(key, value)); } }; CacheAccess = __decorate([ (0, ioc_decorator_1.Provide)() ], CacheAccess); exports.CacheAccess = CacheAccess; //# sourceMappingURL=cache.access.js.map