| "use strict"; | ||
| 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()); | ||
| }); | ||
| }; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.CacheMoudle = void 0; | ||
| const node_cache_1 = __importDefault(require("node-cache")); | ||
| // 先提供一个同步版本的获取信息 | ||
| class CacheMoudle extends node_cache_1.default { | ||
| /**5s 检查一次 */ | ||
| static init() { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| return true; | ||
| }); | ||
| } | ||
| /** | ||
| * 创建cache | ||
| * @param name 模块名字 如果没有名字将不支持自动update功能 | ||
| * @param ttl 数据有效期 等于0的时候不更新 小于0的时候不计算ttl | ||
| */ | ||
| static createCache(name, ttl, keep) { | ||
| keep = keep || {}; | ||
| // 默认不开启克隆模式 | ||
| keep.useClones = keep.useClones || false; | ||
| ttl = ttl || 0; | ||
| if (ttl < 0) | ||
| ttl = 0; | ||
| keep.stdTTL = Math.floor(ttl / 1000); | ||
| if (name == undefined) { | ||
| return new CacheMoudle(keep); | ||
| } | ||
| else { | ||
| if (!this.cacheMap.hasOwnProperty(name)) { | ||
| this.cacheMap[name] = new CacheMoudle(keep); | ||
| } | ||
| return this.cacheMap[name]; | ||
| } | ||
| } | ||
| // 移除cache | ||
| static removeCache(name) { | ||
| delete this.cacheMap[name]; | ||
| } | ||
| hasOwnProperty(key) { | ||
| return this.has(key); | ||
| } | ||
| /** 查询且续订 */ | ||
| get(key) { | ||
| if (this.has(key)) { | ||
| super.ttl(key, this.options.stdTTL || Infinity); | ||
| } | ||
| return super.get(key); | ||
| } | ||
| /** 不续订的查询 */ | ||
| getNttl(key) { | ||
| // if (this.has(key)) { | ||
| // super.ttl(key, this.options.stdTTL || Infinity) | ||
| // } | ||
| return super.get(key); | ||
| } | ||
| /** | ||
| * 遍历内容 | ||
| * @param cb 当方法返回 true时 结束遍历(break) | ||
| */ | ||
| forEach(cb) { | ||
| let kStrings = this.keys(); | ||
| for (let i = 0; i < kStrings.length; i++) { | ||
| let key = kStrings[i]; | ||
| let value = this.get(key); | ||
| if (!value) | ||
| continue; | ||
| if (cb(value, key)) { | ||
| return value; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
| clear() { | ||
| return this.close(); | ||
| } | ||
| } | ||
| exports.CacheMoudle = CacheMoudle; | ||
| CacheMoudle.cacheMap = {}; |
| import Cac from "node-cache"; | ||
| // 先提供一个同步版本的获取信息 | ||
| export class CacheMoudle<T> extends Cac { | ||
| /**5s 检查一次 */ | ||
| static async init() { | ||
| return true; | ||
| } | ||
| /** | ||
| * 创建cache | ||
| * @param name 模块名字 如果没有名字将不支持自动update功能 | ||
| * @param ttl 数据有效期 等于0的时候不更新 小于0的时候不计算ttl | ||
| */ | ||
| static createCache<T>(name?: string, ttl?: number, keep?: Cac.Options): CacheMoudle<T> { | ||
| keep = keep || {}; | ||
| // 默认不开启克隆模式 | ||
| keep.useClones = keep.useClones || false; | ||
| ttl = ttl || 0; | ||
| if (ttl < 0) ttl = 0; | ||
| keep.stdTTL = Math.floor(ttl / 1000); | ||
| if (name == undefined) { | ||
| return new CacheMoudle<T>(keep); | ||
| } | ||
| else { | ||
| if (!this.cacheMap.hasOwnProperty(name)) { | ||
| this.cacheMap[name] = new CacheMoudle<T>(keep); | ||
| } | ||
| return this.cacheMap[name]; | ||
| } | ||
| } | ||
| // 移除cache | ||
| static removeCache(name: string) { | ||
| delete this.cacheMap[name] | ||
| } | ||
| static cacheMap: { [key: string]: CacheMoudle<any> } = {}; | ||
| hasOwnProperty(key: string) { | ||
| return this.has(key); | ||
| } | ||
| /** 查询且续订 */ | ||
| get<T>( | ||
| key: Cac.Key | ||
| ) { | ||
| if (this.has(key)) { | ||
| super.ttl(key, this.options.stdTTL || Infinity) | ||
| } | ||
| return super.get<T>(key) | ||
| } | ||
| /** 不续订的查询 */ | ||
| getNttl<T>( | ||
| key: Cac.Key | ||
| ) { | ||
| // if (this.has(key)) { | ||
| // super.ttl(key, this.options.stdTTL || Infinity) | ||
| // } | ||
| return super.get<T>(key) | ||
| } | ||
| /** | ||
| * 遍历内容 | ||
| * @param cb 当方法返回 true时 结束遍历(break) | ||
| */ | ||
| forEach(cb: (value: T, key: string) => boolean | void) { | ||
| let kStrings = this.keys() | ||
| for (let i = 0; i < kStrings.length; i++) { | ||
| let key = kStrings[i]; | ||
| let value = this.get<T>(key); | ||
| if (!value) continue; | ||
| if (cb(value, key)) { | ||
| return value | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
| clear() { | ||
| return this.close() | ||
| } | ||
| } |
+3
-2
| { | ||
| "name": "mx-tool", | ||
| "version": "1.0.12", | ||
| "version": "1.0.13", | ||
| "description": "#### Description 提供摩西的标准时间 等基础能力", | ||
@@ -22,4 +22,5 @@ "main": "src/index.js", | ||
| "@types/debug": "^4.1.5", | ||
| "debug": "^4.1.1" | ||
| "debug": "^4.1.1", | ||
| "node-cache": "^5.1.2" | ||
| } | ||
| } |
155976
3.64%30
7.14%3496
4.95%3
50%+ Added
+ Added
+ Added