@cave/cache
Advanced tools
+21
-0
@@ -13,2 +13,9 @@ import { Level } from 'level'; | ||
| validData: T; | ||
| data?: undefined; | ||
| } | { | ||
| state: boolean; | ||
| data: any; | ||
| ts?: undefined; | ||
| rawData?: undefined; | ||
| validData?: undefined; | ||
| }>; | ||
@@ -44,2 +51,9 @@ del(key: string): Promise<boolean>; | ||
| validData: T; | ||
| data?: undefined; | ||
| } | { | ||
| state: boolean; | ||
| data: any; | ||
| ts?: undefined; | ||
| rawData?: undefined; | ||
| validData?: undefined; | ||
| }>; | ||
@@ -68,2 +82,9 @@ del(key: string): Promise<boolean>; | ||
| validData: T; | ||
| data?: undefined; | ||
| } | { | ||
| state: boolean; | ||
| data: any; | ||
| ts?: undefined; | ||
| rawData?: undefined; | ||
| validData?: undefined; | ||
| }>; | ||
@@ -70,0 +91,0 @@ _delWithPrefix(key: string, prefix: string | number): Promise<boolean>; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CCache=exports.CCacheFork=void 0;const level_1=require("level");class CCacheFork{cache;prefix;period;constructor(t,e,i){this.cache=t,this.prefix=e,this.period=i}async set(t,e){return await this.cache._setWithPrefix(t,e,this.prefix)}async get(t){return await this.cache._getWithPrefix(t,{prefix:this.prefix,period:this.period})}async del(t){return await this.cache._delWithPrefix(t,this.prefix)}async setMany(t){return await this.cache._setManyWithPrefix(t,this.prefix)}async getMany(t){return await this.cache._getManyWithPrefix(t,{prefix:this.prefix,period:this.period})}async delMany(t){return await this.cache._delManyWithPrefix(t,this.prefix)}}exports.CCacheFork=CCacheFork;class CCache{db;period;constructor(t,e=72e5){this.db=new level_1.Level(t,{valueEncoding:"json"}),this.period=e}fork({prefix:t,period:e}){return new CCacheFork(this,t,e)}getId(t,e){return`${t}${e}`}async init(){await this.db.open()}async set(t,e){return await this._setWithPrefix(t,e,"")}async get(t){return await this._getWithPrefix(t,{prefix:"",period:this.period})}async del(t){return await this._delWithPrefix(t,"")}async setMany(t){await this._setManyWithPrefix(t,"")}async getMany(t){return await this._getManyWithPrefix(t,{prefix:"",period:this.period})}async delMany(t){return await this._delManyWithPrefix(t,"")}async clean(t=6048e5){try{const{db:e}=this;for await(const i of e.iterator()){const a=i[0],r=i[1],s=Date.now()-t;r.ts<s&&await e.del(a)}return!0}catch(t){return!1}}async _setWithPrefix(t,e,i){const a=this.getId(i,t);await this.db.put(a,{ts:Date.now(),payload:e},{valueEncoding:"json"})}async _getWithPrefix(t,e){const i=this.getId(e.prefix,t),a=await this.db.get(i,{valueEncoding:"json"}),r=a.payload,s=Date.now()-a.ts<e.period;return{state:s,ts:a.ts,rawData:r,validData:s?r:void 0}}async _delWithPrefix(t,e){try{const i=this.getId(e,t);return await this.db.del(i),!0}catch(t){return!1}}async _setManyWithPrefix(t,e){try{return await this.db.batch(t.map((t=>({type:"put",key:this.getId(e,t.key),value:{ts:Date.now(),payload:t.value}}))),{valueEncoding:"json"}),!0}catch(t){return!1}}async _getManyWithPrefix(t,e){const i=e.prefix?t.map((t=>this.getId(e.prefix,t))):t;return(await this.db.getMany(i,{valueEncoding:"json"})).map((t=>{const i=t.payload,a=Date.now()-t.ts<e.period;return{state:a,ts:t.ts,rawData:i,validData:a?i:void 0}}))}async _delManyWithPrefix(t,e){try{return await this.db.batch(t.map((t=>({type:"del",key:this.getId(e,t)})))),!0}catch(t){return!1}}}exports.CCache=CCache; | ||
| "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CCache=exports.CCacheFork=void 0;const level_1=require("level");class CCacheFork{cache;prefix;period;constructor(t,e,a){this.cache=t,this.prefix=e,this.period=a}async set(t,e){return await this.cache._setWithPrefix(t,e,this.prefix)}async get(t){return await this.cache._getWithPrefix(t,{prefix:this.prefix,period:this.period})}async del(t){return await this.cache._delWithPrefix(t,this.prefix)}async setMany(t){return await this.cache._setManyWithPrefix(t,this.prefix)}async getMany(t){return await this.cache._getManyWithPrefix(t,{prefix:this.prefix,period:this.period})}async delMany(t){return await this.cache._delManyWithPrefix(t,this.prefix)}}exports.CCacheFork=CCacheFork;class CCache{db;period;constructor(t,e=72e5){this.db=new level_1.Level(t,{valueEncoding:"json"}),this.period=e}fork({prefix:t,period:e}){return new CCacheFork(this,t,e)}getId(t,e){return`${t}${e}`}async init(){await this.db.open()}async set(t,e){return await this._setWithPrefix(t,e,"")}async get(t){return await this._getWithPrefix(t,{prefix:"",period:this.period})}async del(t){return await this._delWithPrefix(t,"")}async setMany(t){await this._setManyWithPrefix(t,"")}async getMany(t){return await this._getManyWithPrefix(t,{prefix:"",period:this.period})}async delMany(t){return await this._delManyWithPrefix(t,"")}async clean(t=6048e5){try{const{db:e}=this;for await(const a of e.iterator()){const i=a[0],r=a[1],s=Date.now()-t;r.ts<s&&await e.del(i)}return!0}catch(t){return!1}}async _setWithPrefix(t,e,a){const i=this.getId(a,t);await this.db.put(i,{ts:Date.now(),payload:e},{valueEncoding:"json"})}async _getWithPrefix(t,e){try{const a=this.getId(e.prefix,t),i=await this.db.get(a,{valueEncoding:"json"}),r=i.payload,s=Date.now()-i.ts<e.period;return{state:s,ts:i.ts,rawData:r,validData:s?r:void 0}}catch(t){return{state:!1,data:void 0}}}async _delWithPrefix(t,e){try{const a=this.getId(e,t);return await this.db.del(a),!0}catch(t){return!1}}async _setManyWithPrefix(t,e){try{return await this.db.batch(t.map((t=>({type:"put",key:this.getId(e,t.key),value:{ts:Date.now(),payload:t.value}}))),{valueEncoding:"json"}),!0}catch(t){return!1}}async _getManyWithPrefix(t,e){const a=e.prefix?t.map((t=>this.getId(e.prefix,t))):t;return(await this.db.getMany(a,{valueEncoding:"json"})).map((t=>{const a=t.payload,i=Date.now()-t.ts<e.period;return{state:i,ts:t.ts,rawData:a,validData:i?a:void 0}}))}async _delManyWithPrefix(t,e){try{return await this.db.batch(t.map((t=>({type:"del",key:this.getId(e,t)})))),!0}catch(t){return!1}}}exports.CCache=CCache; |
+1
-1
| { | ||
| "name": "@cave/cache", | ||
| "version": "1.0.4", | ||
| "version": "1.0.5", | ||
| "publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
6845
8.43%103
25.61%