diff --git a/dist/cache.common.js b/dist/cache.common.js index 60aeeb0..5ef5b88 100644 --- a/dist/cache.common.js +++ b/dist/cache.common.js @@ -53,14 +53,15 @@ class CacheController { return cache } - generateRequest(fn) { + generateRequest(key,fn) { return async (params) => { - if (this.haveCache(fn)) { - return this.getCache(fn) + const cacheKey = `${key}-${JSON.stringify(params)}`; + if (this.haveCache(cacheKey)) { + return this.getCache(cacheKey) } try { const data = await fn(params); - this.setCache(fn(params), { data, size: 1 }); + this.setCache(cacheKey, { data, size: 1 }); return data } catch (e) { throw new Error(e) diff --git a/dist/cache.common.min.js b/dist/cache.common.min.js index f9a19c7..9500e30 100644 --- a/dist/cache.common.min.js +++ b/dist/cache.common.min.js @@ -3,4 +3,4 @@ * (c) 2020-2021 emjio * Released under the MIT License. */ -"use strict";module.exports=class{#e=0;#s=new Map;#t={};#i=10;#h=3;constructor(e){const{maxSize:s,maxUseTime:t}={...e};if(s<=0||isNaN(s))throw new Error("maxSize should be a number");if(t<=0||isNaN(t))throw new Error("maxSize should be a number");this.#i=s,this.#h=t,this.axiosInstance=e.axios}setCache(e,s){for(;this.#e+s.size>this.#i&&0!==this.#e;)this.removeEarliestCache();this.#s.set(e,s),this.#t[e]=0,this.#e+=s.size}haveCache(e){return this.#s.has(e)}getCache(e){let s=null;return e in this.#t&&(s=this.#s.get(e).data,this.#t[e]+=1,this.#t[e]>=this.#h&&this.deleteCache(e)),s}generateRequest(e){return async s=>{if(this.haveCache(e))return this.getCache(e);try{const t=await e(s);return this.setCache(e(s),{data:t,size:1}),t}catch(e){throw new Error(e)}}}removeEarliestCache(){const e=Object.keys(this.#t).sort(((e,s)=>this.#t[e]-this.#t[s]))[0];delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e)}deleteCache(e){this.#s.has(e)&&(delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e))}}; +"use strict";module.exports=class{#e=0;#s=new Map;#t={};#i=10;#h=3;constructor(e){const{maxSize:s,maxUseTime:t}={...e};if(s<=0||isNaN(s))throw new Error("maxSize should be a number");if(t<=0||isNaN(t))throw new Error("maxSize should be a number");this.#i=s,this.#h=t,this.axiosInstance=e.axios}setCache(e,s){for(;this.#e+s.size>this.#i&&0!==this.#e;)this.removeEarliestCache();this.#s.set(e,s),this.#t[e]=0,this.#e+=s.size}haveCache(e){return this.#s.has(e)}getCache(e){let s=null;return e in this.#t&&(s=this.#s.get(e).data,this.#t[e]+=1,this.#t[e]>=this.#h&&this.deleteCache(e)),s}generateRequest(e,s){return async t=>{const i=`${e}-${JSON.stringify(t)}`;if(this.haveCache(i))return this.getCache(i);try{const e=await s(t);return this.setCache(i,{data:e,size:1}),e}catch(e){throw new Error(e)}}}removeEarliestCache(){const e=Object.keys(this.#t).sort(((e,s)=>this.#t[e]-this.#t[s]))[0];delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e)}deleteCache(e){this.#s.has(e)&&(delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e))}}; diff --git a/dist/cache.esm.js b/dist/cache.esm.js index 684116c..09b31f3 100644 --- a/dist/cache.esm.js +++ b/dist/cache.esm.js @@ -51,14 +51,15 @@ class CacheController { return cache } - generateRequest(fn) { + generateRequest(key,fn) { return async (params) => { - if (this.haveCache(fn)) { - return this.getCache(fn) + const cacheKey = `${key}-${JSON.stringify(params)}`; + if (this.haveCache(cacheKey)) { + return this.getCache(cacheKey) } try { const data = await fn(params); - this.setCache(fn(params), { data, size: 1 }); + this.setCache(cacheKey, { data, size: 1 }); return data } catch (e) { throw new Error(e) diff --git a/dist/cache.esm.min.js b/dist/cache.esm.min.js index c6512bb..d78afe2 100644 --- a/dist/cache.esm.min.js +++ b/dist/cache.esm.min.js @@ -3,4 +3,4 @@ * (c) 2020-2021 emjio * Released under the MIT License. */ -class e{#e=0;#s=new Map;#t={};#i=10;#h=3;constructor(e){const{maxSize:s,maxUseTime:t}={...e};if(s<=0||isNaN(s))throw new Error("maxSize should be a number");if(t<=0||isNaN(t))throw new Error("maxSize should be a number");this.#i=s,this.#h=t,this.axiosInstance=e.axios}setCache(e,s){for(;this.#e+s.size>this.#i&&0!==this.#e;)this.removeEarliestCache();this.#s.set(e,s),this.#t[e]=0,this.#e+=s.size}haveCache(e){return this.#s.has(e)}getCache(e){let s=null;return e in this.#t&&(s=this.#s.get(e).data,this.#t[e]+=1,this.#t[e]>=this.#h&&this.deleteCache(e)),s}generateRequest(e){return async s=>{if(this.haveCache(e))return this.getCache(e);try{const t=await e(s);return this.setCache(e(s),{data:t,size:1}),t}catch(e){throw new Error(e)}}}removeEarliestCache(){const e=Object.keys(this.#t).sort(((e,s)=>this.#t[e]-this.#t[s]))[0];delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e)}deleteCache(e){this.#s.has(e)&&(delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e))}}export{e as default}; +class e{#e=0;#s=new Map;#t={};#i=10;#h=3;constructor(e){const{maxSize:s,maxUseTime:t}={...e};if(s<=0||isNaN(s))throw new Error("maxSize should be a number");if(t<=0||isNaN(t))throw new Error("maxSize should be a number");this.#i=s,this.#h=t,this.axiosInstance=e.axios}setCache(e,s){for(;this.#e+s.size>this.#i&&0!==this.#e;)this.removeEarliestCache();this.#s.set(e,s),this.#t[e]=0,this.#e+=s.size}haveCache(e){return this.#s.has(e)}getCache(e){let s=null;return e in this.#t&&(s=this.#s.get(e).data,this.#t[e]+=1,this.#t[e]>=this.#h&&this.deleteCache(e)),s}generateRequest(e,s){return async t=>{const i=`${e}-${JSON.stringify(t)}`;if(this.haveCache(i))return this.getCache(i);try{const e=await s(t);return this.setCache(i,{data:e,size:1}),e}catch(e){throw new Error(e)}}}removeEarliestCache(){const e=Object.keys(this.#t).sort(((e,s)=>this.#t[e]-this.#t[s]))[0];delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e)}deleteCache(e){this.#s.has(e)&&(delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e))}}export{e as default}; diff --git a/dist/cache.js b/dist/cache.js index 110334b..618dad6 100644 --- a/dist/cache.js +++ b/dist/cache.js @@ -57,14 +57,15 @@ return cache } - generateRequest(fn) { + generateRequest(key,fn) { return async (params) => { - if (this.haveCache(fn)) { - return this.getCache(fn) + const cacheKey = `${key}-${JSON.stringify(params)}`; + if (this.haveCache(cacheKey)) { + return this.getCache(cacheKey) } try { const data = await fn(params); - this.setCache(fn(params), { data, size: 1 }); + this.setCache(cacheKey, { data, size: 1 }); return data } catch (e) { throw new Error(e) diff --git a/dist/cache.min.js b/dist/cache.min.js index 3ee3c1e..a7669b4 100644 --- a/dist/cache.min.js +++ b/dist/cache.min.js @@ -3,4 +3,4 @@ * (c) 2020-2021 emjio * Released under the MIT License. */ -!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):(e="undefined"!=typeof globalThis?globalThis:e||self).Cache=s()}(this,(function(){"use strict";return class{#e=0;#s=new Map;#t={};#i=10;#h=3;constructor(e){const{maxSize:s,maxUseTime:t}={...e};if(s<=0||isNaN(s))throw new Error("maxSize should be a number");if(t<=0||isNaN(t))throw new Error("maxSize should be a number");this.#i=s,this.#h=t,this.axiosInstance=e.axios}setCache(e,s){for(;this.#e+s.size>this.#i&&0!==this.#e;)this.removeEarliestCache();this.#s.set(e,s),this.#t[e]=0,this.#e+=s.size}haveCache(e){return this.#s.has(e)}getCache(e){let s=null;return e in this.#t&&(s=this.#s.get(e).data,this.#t[e]+=1,this.#t[e]>=this.#h&&this.deleteCache(e)),s}generateRequest(e){return async s=>{if(this.haveCache(e))return this.getCache(e);try{const t=await e(s);return this.setCache(e(s),{data:t,size:1}),t}catch(e){throw new Error(e)}}}removeEarliestCache(){const e=Object.keys(this.#t).sort(((e,s)=>this.#t[e]-this.#t[s]))[0];delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e)}deleteCache(e){this.#s.has(e)&&(delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e))}}})); +!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):(e="undefined"!=typeof globalThis?globalThis:e||self).Cache=s()}(this,(function(){"use strict";return class{#e=0;#s=new Map;#t={};#i=10;#h=3;constructor(e){const{maxSize:s,maxUseTime:t}={...e};if(s<=0||isNaN(s))throw new Error("maxSize should be a number");if(t<=0||isNaN(t))throw new Error("maxSize should be a number");this.#i=s,this.#h=t,this.axiosInstance=e.axios}setCache(e,s){for(;this.#e+s.size>this.#i&&0!==this.#e;)this.removeEarliestCache();this.#s.set(e,s),this.#t[e]=0,this.#e+=s.size}haveCache(e){return this.#s.has(e)}getCache(e){let s=null;return e in this.#t&&(s=this.#s.get(e).data,this.#t[e]+=1,this.#t[e]>=this.#h&&this.deleteCache(e)),s}generateRequest(e,s){return async t=>{const i=`${e}-${JSON.stringify(t)}`;if(this.haveCache(i))return this.getCache(i);try{const e=await s(t);return this.setCache(i,{data:e,size:1}),e}catch(e){throw new Error(e)}}}removeEarliestCache(){const e=Object.keys(this.#t).sort(((e,s)=>this.#t[e]-this.#t[s]))[0];delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e)}deleteCache(e){this.#s.has(e)&&(delete this.#t[e],this.#e-=this.#s.get(e).size,this.#s.delete(e))}}})); diff --git a/example/testcase01/app.js b/example/testcase01/app.js index 4e17dbe..def8368 100644 --- a/example/testcase01/app.js +++ b/example/testcase01/app.js @@ -3,17 +3,20 @@ const requestCacheController = new cacheController({ maxSize: 50, maxUseTime: 3 const sendbtn = document.querySelector('#sendRequest') const resultContent = document.querySelector('#result') -const request = ()=>{ - return fetch('https://api.apiopen.top/api/sentences') +const request = (params)=>{ + + return fetch(`https://api.apiopen.top/api/sentences?page=${params.page}&size=${params.size}`) .then((res)=> res.json()) .then((json)=> json) } -const api = requestCacheController.generateRequest(request) +const params = { + page:1, + size:2 +} +const api = requestCacheController.generateRequest(`sentences`,request) sendbtn.addEventListener('click',()=>{ - console.log(api) - api().then(res=>{ - console.log(requestCacheController) + api(params).then(res=>{ if(res.code===200){ resultContent.innerText = `${res.result.name} ---${res.result.from}` } diff --git a/src/index.js b/src/index.js index e0be5a9..f702533 100644 --- a/src/index.js +++ b/src/index.js @@ -51,14 +51,15 @@ export default class CacheController { return cache } - generateRequest(fn) { + generateRequest(key,fn) { return async (params) => { - if (this.haveCache(fn)) { - return this.getCache(fn) + const cacheKey = `${key}-${JSON.stringify(params)}` + if (this.haveCache(cacheKey)) { + return this.getCache(cacheKey) } try { const data = await fn(params); - this.setCache(fn(params), { data, size: 1 }); + this.setCache(cacheKey, { data, size: 1 }); return data } catch (e) { throw new Error(e)