mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-11 13:52:50 +00:00
缓存
This commit is contained in:
parent
fb32283ded
commit
e686095ea2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,4 @@
|
||||
logs/
|
||||
cache/
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
node_modules/
|
||||
|
||||
64
core/src/cache/store.ts
vendored
Normal file
64
core/src/cache/store.ts
vendored
Normal file
@ -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<T>(key: string): Promise<T> {
|
||||
return await this.store.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param key
|
||||
* @param value
|
||||
* @param ttl
|
||||
*/
|
||||
async set<T>(key: string, value: T, ttl: number): Promise<void> {
|
||||
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<void> {
|
||||
await this.store.del(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置
|
||||
*/
|
||||
async reset(): Promise<void> {
|
||||
await this.store.reset();
|
||||
}
|
||||
}
|
||||
|
||||
export const CoolCacheStore = function (options = {}) {
|
||||
return new FsCacheStore(options);
|
||||
};
|
||||
64
plugin-cli/src/cache/store.ts
vendored
Normal file
64
plugin-cli/src/cache/store.ts
vendored
Normal file
@ -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<T>(key: string): Promise<T> {
|
||||
return await this.store.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置
|
||||
* @param key
|
||||
* @param value
|
||||
* @param ttl
|
||||
*/
|
||||
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
|
||||
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<void> {
|
||||
await this.store.del(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置
|
||||
*/
|
||||
async reset(): Promise<void> {
|
||||
await this.store.reset();
|
||||
}
|
||||
}
|
||||
|
||||
export const CoolCacheStore = function (options = {}) {
|
||||
return new FsCacheStore(options);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user