This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make recursive true defaults, tests and more
- Loading branch information
1 parent
d04e5c5
commit ef824d3
Showing
11 changed files
with
190 additions
and
366 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
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
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
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,83 @@ | ||
'use strict' | ||
const hashlru = require('hashlru') | ||
|
||
/** | ||
* Time Aware Least Recent Used Cache | ||
* @see https://arxiv.org/pdf/1801.00390 | ||
* @todo move this to ipfs-utils or it's own package | ||
* | ||
* @class TLRU | ||
*/ | ||
class TLRU { | ||
/** | ||
* Creates an instance of TLRU. | ||
* | ||
* @param {number} maxSize | ||
* @memberof TLRU | ||
*/ | ||
constructor (maxSize) { | ||
this.lru = hashlru(maxSize) | ||
} | ||
|
||
/** | ||
* Get the value from the a key | ||
* | ||
* @param {string} key | ||
* @returns {any} | ||
* @memberof TLRU | ||
*/ | ||
get (key) { | ||
const value = this.lru.get(key) | ||
if (value) { | ||
if ((value.expire) && (value.expire < Date.now())) { | ||
this.lru.remove(key) | ||
return undefined | ||
} | ||
} | ||
return value.value | ||
} | ||
|
||
/** | ||
* Set a key value pair | ||
* | ||
* @param {string} key | ||
* @param {any} value | ||
* @param {number} ttl - in miliseconds | ||
* @memberof TLRU | ||
*/ | ||
set (key, value, ttl) { | ||
this.lru.set(key, { value, expire: Date.now() + ttl }) | ||
} | ||
|
||
/** | ||
* Find if the cache has the key | ||
* | ||
* @param {string} key | ||
* @returns {boolean} | ||
* @memberof TLRU | ||
*/ | ||
has (key) { | ||
return this.lru.has(key) | ||
} | ||
|
||
/** | ||
* Remove key | ||
* | ||
* @param {string} key | ||
* @memberof TLRU | ||
*/ | ||
remove (key) { | ||
this.lru.remove(key) | ||
} | ||
|
||
/** | ||
* Clears the cache | ||
* | ||
* @memberof TLRU | ||
*/ | ||
clear () { | ||
this.lru.clear() | ||
} | ||
} | ||
|
||
module.exports = TLRU |
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
Oops, something went wrong.