Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpbberry/cache
A caching structure with aquittable TTD
https://github.com/jpbberry/cache
Last synced: about 1 month ago
JSON representation
A caching structure with aquittable TTD
- Host: GitHub
- URL: https://github.com/jpbberry/cache
- Owner: jpbberry
- Created: 2021-03-18T13:27:51.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-30T21:13:39.000Z (almost 4 years ago)
- Last Synced: 2024-11-21T11:39:21.189Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cache
## Installing
`npm i @jpbberry/cache`
## Usage
Cache is an extension of Discord.JS's Collection, you can find docs [here](https://discord.js.org/#/docs/collection/master/class/Collection)
Cache is essentially a temporary hold for data, that is cleared after inactivity.
You pass a `time` paramater to the constructor, which will wait that amount of time before clearing the data out of the cache. However everytime you do something with said data, like .get() it, said data's time til death with be reset, making it so you hold onto data until it is no longer actively being looked for.
## Usage
```js
const { Cache } = require('@jpbberry/cache')const cache = new Cache(1000) // time in milliseconds
cache.set('a', 'b')
setTimeout(() => {
console.log(cache.get('a')) // null
}, 1001)
```But for example if you made the setTimeout 500, you would reset the time til death timer, so the cache would be available for another second.
I find this super easy in database caching implementation, where active searches are cached until they're no longer active.