https://github.com/lragji/hit-cache
A memory caching library, which uses hits to a key as cache expiry vector. Wrapper on top of https://github.com/ptarjan/node-cache
https://github.com/lragji/hit-cache
cache cachehits hit-cache memory-cache
Last synced: 5 months ago
JSON representation
A memory caching library, which uses hits to a key as cache expiry vector. Wrapper on top of https://github.com/ptarjan/node-cache
- Host: GitHub
- URL: https://github.com/lragji/hit-cache
- Owner: LRagji
- License: bsd-2-clause
- Created: 2020-01-02T10:20:19.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T04:18:02.000Z (about 3 years ago)
- Last Synced: 2025-06-01T08:51:45.795Z (8 months ago)
- Topics: cache, cachehits, hit-cache, memory-cache
- Language: JavaScript
- Homepage: https://lragji.github.io/
- Size: 162 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# hit-cache
A memory caching library, which uses hits to a key as cache expiry vector.
This is a wrapper on top of the https://github.com/ptarjan/node-cache which provides time & hits based cache expiry, simply put content which are fetched frequently stay in the cache for longer vs content which do not have same frequency get pushed out of it.
## Example:
```javascript
// const cache = require('memory-cache');
// const hitCacheType = require('memory-cache').HitCache;
// const hitCache = new hitCacheType(cache);
//OR
const hitCache = new hitCacheType();
hitCache.set("Laukik", "Disappears in 5 seconds, cause not hits.", 5000);
setTimeout(() => {
console.log("Laukik: " + hitCache.get("Laukik")); //Expected: Null
}, 6000);
hitCache.set("Popular-Laukik", "Will not disapper in 5 seconds, cause it got hit.", 5000);
//IMP:Every call to get for a given key will increase the life by lifespan param provided at the time of set.
setTimeout(() => {
console.log("Popular-Laukik: " + hitCache.get("Popular-Laukik")); //Expected: "Will not disapper in 5 seconds, cause it got hit."
}, 2000);
setTimeout(() => {
console.log("Popular-Laukik: " + hitCache.get("Popular-Laukik")); //Expected: "Will not disapper in 5 seconds, cause it got hit."
}, 6000);
```
## API
### Constructor `constructor(cache)`
* Parameter `cache`: Instance of the cache which should hold values. Eg:`require('cache').Cache` or `require('cache')`
### set `set(key, value, lifeSpan)`
* Parameter `key` : Key for the cached value.
* Parameter `value` : Content to be cached.
* Parameter `lifeSpan` : Default expiry time for the content, after which it will be evicted from cache unless it has hits.
### get `get(key)`
* Parameter `key` : Key for the cached value.
* Returns `null` if the key is not found, else returns content and increases its lifespan by lifespan parameter defined at the time of set.