@loveduk/cache-manager 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 LoveKui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # resource-cache
2
+
3
+ resource-cache
4
+
5
+ ```
6
+ <!DOCTYPE html>
7
+ <html lang="en">
8
+ <head>
9
+ <meta charset="UTF-8">
10
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
11
+
12
+ <title>Document</title>
13
+
14
+
15
+ </head>
16
+ <body>
17
+ <div id="map"></div>
18
+
19
+
20
+ </body>
21
+ <script src="../node_modules/ajax-hook/dist/ajaxhook.min.js"></script>
22
+ <script src="../node_modules/localforage/dist/localforage.min.js"></script>
23
+
24
+ <script src="../dist//bundle.umd.js"></script>
25
+ <script>
26
+ const cacheManager=new CacheManager({
27
+ storeUrls: ["/image/"]
28
+ });
29
+ cacheManager.calculateCacheSize().then(res=>{
30
+ console.log(res);
31
+ })
32
+ </script>
33
+
34
+
35
+ <style>
36
+ #map{
37
+ width: 100%;
38
+ height: 100vh;
39
+ position: fixed;
40
+ }
41
+ </style>
42
+ <script >
43
+
44
+
45
+
46
+
47
+ </script>
48
+ </html>
49
+
50
+ ```
@@ -0,0 +1,14 @@
1
+ export default class CacheManager {
2
+ private storeUrls;
3
+ private store;
4
+ private expireTime;
5
+ constructor(options: {
6
+ storeUrls: string[];
7
+ expireTime?: undefined | number;
8
+ });
9
+ private init;
10
+ clear(): void;
11
+ calculateCacheSize(): Promise<string>;
12
+ private formatSize;
13
+ clearExpiredCache(): Promise<void>;
14
+ }
@@ -0,0 +1,2 @@
1
+ import CacheManager from "./CacheManager";
2
+ export default CacheManager;
@@ -0,0 +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};
@@ -0,0 +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)}}))}}});
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@loveduk/cache-manager",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "module": "dist/bundle.esm.js",
6
+ "types": "dist/@types/index.d.ts",
7
+ "main": "dist/bundle.esm.js",
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1",
10
+ "dev": "rollup -w -c --environment MODE:dev",
11
+ "build": "rollup -c --environment MODE:prod"
12
+ },
13
+ "keywords": [],
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "author": "",
18
+ "license": "ISC",
19
+ "dependencies": {
20
+ "@babel/core": "^7.24.4",
21
+ "@babel/plugin-external-helpers": "^7.24.1",
22
+ "@babel/preset-env": "^7.24.4",
23
+ "ajax-hook": "^3.0.3",
24
+ "crypto-js": "^4.2.0",
25
+ "localforage": "^1.10.0",
26
+ "rollup-plugin-babel": "^4.4.0",
27
+ "rollup-plugin-uglify": "^6.0.4"
28
+ },
29
+ "devDependencies": {
30
+ "@rollup/plugin-alias": "^5.1.0",
31
+ "@rollup/plugin-commonjs": "^25.0.7",
32
+ "@rollup/plugin-json": "^6.0.0",
33
+ "@rollup/plugin-node-resolve": "^15.2.1",
34
+ "@rollup/plugin-terser": "^0.4.4",
35
+ "@rollup/plugin-typescript": "^11.1.5",
36
+ "rollup": "^4.9.1",
37
+ "rollup-plugin-copy": "^3.5.0",
38
+ "rollup-plugin-dts": "^6.1.0",
39
+ "rollup-plugin-font": "^1.1.2",
40
+ "rollup-plugin-serve": "^1.1.1",
41
+ "rollup-plugin-typescript2": "^0.36.0",
42
+ "tslib": "^2.6.2",
43
+ "typescript": "^5.4.3"
44
+ },
45
+ "type": "module",
46
+ "umdModuleName": "CacheManager",
47
+ "files": [
48
+ "/dist/*",
49
+ "/package.json",
50
+ "/README.md",
51
+ "/LICENSE"
52
+ ]
53
+ }