-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lru cache and block info pagination
- Loading branch information
Showing
4 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Cache { | ||
constructor(limit = 1000) { | ||
this.limit = limit; | ||
this.cache = new Map(); | ||
} | ||
|
||
set(key, value) { | ||
if (this.cache.size >= this.limit) { | ||
const firstItemKey = this.cache.keys().next().value; | ||
this.cache.delete(firstItemKey); | ||
} | ||
this.cache.set(key, value); | ||
} | ||
|
||
async get(func, args) { | ||
let cache_key = JSON.stringify(args); | ||
if (this.cache.has(cache_key)) { | ||
const temp = this.cache.get(cache_key); | ||
this.cache.delete(cache_key); | ||
this.cache.set(cache_key, temp); | ||
return temp; | ||
} | ||
let result = await func(args); | ||
console.log("Actual call", args); | ||
this.set(cache_key, result); | ||
return result; | ||
} | ||
} | ||
|
||
var cache = new Cache(); | ||
module.exports = cache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters