https://github.com/csbun/lazy-cache
lazy cache, automatic reset cache asynchronously when expired
https://github.com/csbun/lazy-cache
Last synced: about 1 month ago
JSON representation
lazy cache, automatic reset cache asynchronously when expired
- Host: GitHub
- URL: https://github.com/csbun/lazy-cache
- Owner: csbun
- License: mit
- Created: 2015-07-06T03:40:21.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-07-21T06:33:00.000Z (almost 11 years ago)
- Last Synced: 2025-02-08T07:05:06.122Z (over 1 year ago)
- Language: JavaScript
- Size: 156 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lazycache
lazy inmemory cache for node, automatic reset cache asynchronously when expired
[](https://nodei.co/npm/lazycache/)
[](https://travis-ci.org/csbun/lazy-cache)
[](https://coveralls.io/github/csbun/lazy-cache?branch=master)
## Install
```
npm i lazycache --save
```
## Usage
### lazyCache()
Use the factory method to init a cache object:
```javascript
var defaultCacheTime = 300000; // 300000 ms = 5 min
var cache = require('lazyCache')(defaultCacheTime);
```
if `defaultCacheTime < 0`, a `noCache` object will return, which offer the same API but do __NOT__ cache anything.
### cache.get(key)
Get value from cache
- key: cache key
```javascript
cache.set('key', 'my cache value');
```
### cache.set(key, val [, cacheTime] [, reseter])
Set value into cache
- key: cache key
- val: cache value
- cacheTime: cache time (ms)
- reseter: a function which reset the `cache value`
```javascript
var reseter = function (cb) {
// async
setTimeout(function () {
cb(null, 'new value'); // `null` for NO error
}, 30);
};
cache.set('key', 'initial value', reseter)
```