1234567891011121314151617181920212223242526272829303132333435363738 |
- class CyCache {
- #data = new Map<string, any>();
- keyName = 'cy'
- maxSize = 1000;
- constructor(name: string) {
- this.keyName = name;
- }
- async get(key: string, cb: Function) {
- if (this.#data.size > this.maxSize) {
- this.#data.clear()
- }
- const k = this.keyName + '_' + key
- const temp = this.#data.get(k)
- if (temp) {
- return temp
- }
- const t = (await cb)(key)
- if (t !== null) {
- this.#data.set(k, t)
- }
- return t
- }
- setMaxSize(size: number) {
- this.maxSize = size;
- }
- clear() {
- this.#data.clear()
- }
- }
- export default CyCache
|