Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dist/cache.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dist/cache.common.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions dist/cache.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dist/cache.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions dist/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dist/cache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions example/testcase01/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down