@loveduk/cache-manager 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bundle.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{proxy}from"ajax-hook";import localStorage from"localforage";class CacheManager{storeUrls;store;expireTime;constructor(e){this.storeUrls=e?.storeUrls,0!==this.storeUrls.length&&(this.expireTime=e?.expireTime||void 0,this.store=localStorage.createInstance({name:"CacheDB"}),this.init())}init(){proxy({onRequest:async(e,t)=>{const{url:a,headers:r}=e;if(this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return!a?.includes(".terrain")&&(e||a.includes(t))},!1)){var s=await this.store?.getItem(a);if(s?.data)return void t.resolve({config:e,status:200,headers:r,response:s?.data})}t.next(e)},onResponse:(e,t)=>{const a=e["config"]["url"];var r,s;this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return e||a.includes(t)},!1)&&(r={dataTimestamp:(s=new Date).getTime(),expireTime:void 0},this.expireTime?(s=new Date(s.getTime()+24*this.expireTime*60*60*1e3).getTime(),r.expireTime=s):r.expireTime=void 0,s={metaData:r,data:e.response},this.store?.setItem(a,s)),t.next(e)}})}clear(){this.store?.clear()}async calculateCacheSize(){let s=0;var e=await this.store?.keys()||[];return await Promise.all(e.map(async t=>{try{var e,a,r=await this.store?.getItem(t);r?.data instanceof ArrayBuffer?s+=r?.data?.byteLength:"string"==typeof r?.data&&(e=JSON.stringify(r?.data),a=new Blob([e]).size,s+=a)}catch(e){console.error(`Error getting size of key ${t}: `+e)}})),console.log("Total Cache Size:",this.formatSize(s)),this.formatSize(s)}formatSize(e){var t;return 0===e?"0 Byte":(t=parseInt(Math.floor(Math.log(e)/Math.log(1024))),Math.round(e/Math.pow(1024,t),2).toFixed(2)+" "+["Bytes","KB","MB","GB","TB"][t])}async clearExpiredCache(){const a=(new Date).getTime();var e=await this.store?.keys()||[];await Promise.all(e.map(async t=>{try{var e=await this.store?.getItem(t);e?.metaData&&e?.metaData?.expireTime&&e?.metaData?.expireTime<a&&await this.store?.removeItem(t)}catch(e){console.error(`Error clearing cache for key ${t}: `+e)}}))}}export{CacheManager as default};
|
|
1
|
+
import{proxy}from"ajax-hook";import localStorage from"localforage";class CacheManager{storeUrls;store;expireTime;constructor(e){this.storeUrls=e?.storeUrls,0!==this.storeUrls.length&&(this.expireTime=e?.expireTime||void 0,this.store=localStorage.createInstance({name:e?.storeName||"CacheDB"}),this.init())}init(){proxy({onRequest:async(e,t)=>{const{url:a,headers:r}=e;if(this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return!a?.includes(".terrain")&&(e||a.includes(t))},!1)){var s=await this.store?.getItem(a);if(s?.data)return void t.resolve({config:e,status:200,headers:r,response:s?.data})}t.next(e)},onResponse:(e,t)=>{const a=e["config"]["url"];var r,s;this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return e||a.includes(t)},!1)&&(r={dataTimestamp:(s=new Date).getTime(),expireTime:void 0},this.expireTime?(s=new Date(s.getTime()+24*this.expireTime*60*60*1e3).getTime(),r.expireTime=s):r.expireTime=void 0,s={metaData:r,data:e.response},this.store?.setItem(a,s)),t.next(e)}})}clear(){this.store?.clear()}async calculateCacheSize(){let s=0;var e=await this.store?.keys()||[];return await Promise.all(e.map(async t=>{try{var e,a,r=await this.store?.getItem(t);r?.data instanceof ArrayBuffer?s+=r?.data?.byteLength:"string"==typeof r?.data&&(e=JSON.stringify(r?.data),a=new Blob([e]).size,s+=a)}catch(e){console.error(`Error getting size of key ${t}: `+e)}})),console.log("Total Cache Size:",this.formatSize(s)),this.formatSize(s)}formatSize(e){var t;return 0===e?"0 Byte":(t=parseInt(Math.floor(Math.log(e)/Math.log(1024))),Math.round(e/Math.pow(1024,t),2).toFixed(2)+" "+["Bytes","KB","MB","GB","TB"][t])}async clearExpiredCache(){const a=(new Date).getTime();var e=await this.store?.keys()||[];await Promise.all(e.map(async t=>{try{var e=await this.store?.getItem(t);e?.metaData&&e?.metaData?.expireTime&&e?.metaData?.expireTime<a&&await this.store?.removeItem(t)}catch(e){console.error(`Error clearing cache for key ${t}: `+e)}}))}}export{CacheManager as default};
|
package/dist/bundle.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("ajax-hook"),require("localforage")):"function"==typeof define&&define.amd?define(["ajax-hook","localforage"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).CacheManager=t(e.ah,e.localforage)}(this,function(e,t){"use strict";return class{storeUrls;store;expireTime;constructor(e){this.storeUrls=e?.storeUrls,0!==this.storeUrls.length&&(this.expireTime=e?.expireTime||void 0,this.store=t.createInstance({name:"CacheDB"}),this.init())}init(){e.proxy({onRequest:async(e,t)=>{const{url:a,headers:r}=e;if(this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return!a?.includes(".terrain")&&(e||a.includes(t))},!1)){var i=await this.store?.getItem(a);if(i?.data)return void t.resolve({config:e,status:200,headers:r,response:i?.data})}t.next(e)},onResponse:(e,t)=>{const a=e["config"]["url"];var r,i;this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return e||a.includes(t)},!1)&&(r={dataTimestamp:(i=new Date).getTime(),expireTime:void 0},this.expireTime?(i=new Date(i.getTime()+24*this.expireTime*60*60*1e3).getTime(),r.expireTime=i):r.expireTime=void 0,i={metaData:r,data:e.response},this.store?.setItem(a,i)),t.next(e)}})}clear(){this.store?.clear()}async calculateCacheSize(){let i=0;var e=await this.store?.keys()||[];return await Promise.all(e.map(async t=>{try{var e,a,r=await this.store?.getItem(t);r?.data instanceof ArrayBuffer?i+=r?.data?.byteLength:"string"==typeof r?.data&&(e=JSON.stringify(r?.data),a=new Blob([e]).size,i+=a)}catch(e){console.error(`Error getting size of key ${t}: `+e)}})),console.log("Total Cache Size:",this.formatSize(i)),this.formatSize(i)}formatSize(e){var t;return 0===e?"0 Byte":(t=parseInt(Math.floor(Math.log(e)/Math.log(1024))),Math.round(e/Math.pow(1024,t),2).toFixed(2)+" "+["Bytes","KB","MB","GB","TB"][t])}async clearExpiredCache(){const a=(new Date).getTime();var e=await this.store?.keys()||[];await Promise.all(e.map(async t=>{try{var e=await this.store?.getItem(t);e?.metaData&&e?.metaData?.expireTime&&e?.metaData?.expireTime<a&&await this.store?.removeItem(t)}catch(e){console.error(`Error clearing cache for key ${t}: `+e)}}))}}});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("ajax-hook"),require("localforage")):"function"==typeof define&&define.amd?define(["ajax-hook","localforage"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).CacheManager=t(e.ah,e.localforage)}(this,function(e,t){"use strict";return class{storeUrls;store;expireTime;constructor(e){this.storeUrls=e?.storeUrls,0!==this.storeUrls.length&&(this.expireTime=e?.expireTime||void 0,this.store=t.createInstance({name:e?.storeName||"CacheDB"}),this.init())}init(){e.proxy({onRequest:async(e,t)=>{const{url:a,headers:r}=e;if(this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return!a?.includes(".terrain")&&(e||a.includes(t))},!1)){var i=await this.store?.getItem(a);if(i?.data)return void t.resolve({config:e,status:200,headers:r,response:i?.data})}t.next(e)},onResponse:(e,t)=>{const a=e["config"]["url"];var r,i;this.storeUrls.reduce((e,t)=>{t=t.substring(0,t.lastIndexOf("/")+1);return e||a.includes(t)},!1)&&(r={dataTimestamp:(i=new Date).getTime(),expireTime:void 0},this.expireTime?(i=new Date(i.getTime()+24*this.expireTime*60*60*1e3).getTime(),r.expireTime=i):r.expireTime=void 0,i={metaData:r,data:e.response},this.store?.setItem(a,i)),t.next(e)}})}clear(){this.store?.clear()}async calculateCacheSize(){let i=0;var e=await this.store?.keys()||[];return await Promise.all(e.map(async t=>{try{var e,a,r=await this.store?.getItem(t);r?.data instanceof ArrayBuffer?i+=r?.data?.byteLength:"string"==typeof r?.data&&(e=JSON.stringify(r?.data),a=new Blob([e]).size,i+=a)}catch(e){console.error(`Error getting size of key ${t}: `+e)}})),console.log("Total Cache Size:",this.formatSize(i)),this.formatSize(i)}formatSize(e){var t;return 0===e?"0 Byte":(t=parseInt(Math.floor(Math.log(e)/Math.log(1024))),Math.round(e/Math.pow(1024,t),2).toFixed(2)+" "+["Bytes","KB","MB","GB","TB"][t])}async clearExpiredCache(){const a=(new Date).getTime();var e=await this.store?.keys()||[];await Promise.all(e.map(async t=>{try{var e=await this.store?.getItem(t);e?.metaData&&e?.metaData?.expireTime&&e?.metaData?.expireTime<a&&await this.store?.removeItem(t)}catch(e){console.error(`Error clearing cache for key ${t}: `+e)}}))}}});
|