diff --git a/.gitignore b/.gitignore index 4cafa26..6830170 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ logs/ -cache/ npm-debug.log yarn-error.log node_modules/ diff --git a/core/src/cache/store.ts b/core/src/cache/store.ts new file mode 100644 index 0000000..f52b23b --- /dev/null +++ b/core/src/cache/store.ts @@ -0,0 +1,64 @@ +import * as FsStore from "@cool-midway/cache-manager-fs-hash"; + +/** + * cool 基于磁盘的缓存 + */ +class FsCacheStore { + options: any; + + store: FsStore; + + constructor(options = {}) { + options = { + ...options, + path: "cache", + ttl: -1, + }; + this.options = options; + this.store = FsStore.create(options); + } + + /** + * 获得 + * @param key + * @returns + */ + async get(key: string): Promise { + return await this.store.get(key); + } + + /** + * 设置 + * @param key + * @param value + * @param ttl + */ + async set(key: string, value: T, ttl: number): Promise { + let t = ttl ? ttl : this.options.ttl; + if (t > 0) { + t = t / 1000; + } + await this.store.set(key, value, { + ttl: t, + }); + } + + /** + * 删除 + * @param key + */ + async del(key: string): Promise { + await this.store.del(key); + } + + /** + * 重置 + */ + async reset(): Promise { + await this.store.reset(); + } +} + +export const CoolCacheStore = function (options = {}) { + return new FsCacheStore(options); +}; diff --git a/plugin-cli/src/cache/store.ts b/plugin-cli/src/cache/store.ts new file mode 100644 index 0000000..44a74c1 --- /dev/null +++ b/plugin-cli/src/cache/store.ts @@ -0,0 +1,64 @@ +import * as FsStore from "@cool-midway/cache-manager-fs-hash"; + +/** + * cool 基于磁盘的缓存 + */ +export class FsCacheStore { + options: any; + + store: FsStore; + + constructor(options = {}) { + options = { + ...options, + path: "cache", + ttl: -1, + }; + this.options = options; + this.store = FsStore.create(options); + } + + /** + * 获得 + * @param key + * @returns + */ + async get(key: string): Promise { + return await this.store.get(key); + } + + /** + * 设置 + * @param key + * @param value + * @param ttl + */ + async set(key: string, value: T, ttl?: number): Promise { + let t = ttl ? ttl : this.options.ttl; + if (t > 0) { + t = t / 1000; + } + await this.store.set(key, value, { + ttl: t, + }); + } + + /** + * 删除 + * @param key + */ + async del(key: string): Promise { + await this.store.del(key); + } + + /** + * 重置 + */ + async reset(): Promise { + await this.store.reset(); + } +} + +export const CoolCacheStore = function (options = {}) { + return new FsCacheStore(options); +};