Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bonniernews/exp-asynccache
A pluggable async cache for Node.JS
https://github.com/bonniernews/exp-asynccache
Last synced: about 8 hours ago
JSON representation
A pluggable async cache for Node.JS
- Host: GitHub
- URL: https://github.com/bonniernews/exp-asynccache
- Owner: BonnierNews
- License: mit
- Created: 2014-11-03T09:30:42.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-04-15T23:41:24.000Z (over 1 year ago)
- Last Synced: 2024-11-08T09:50:13.085Z (10 days ago)
- Language: JavaScript
- Homepage:
- Size: 191 KB
- Stars: 0
- Watchers: 68
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
asynccache
==========An async cache with a lookup function per key for node js with a different interface than async-cache.
Errors are not cached and the callback function is always called asynchronously even if the value is resolved
synchronously.### Installation
`npm install --save exp-asynccache`### Usage
Callback usage:
```javascript
var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();cache.lookup("foo", function (resolve) {
// Find foo asynchronously (or synchronously) then call resolve with the value
resolve(null, "baz");
}, function (err, value) {
console.log(value); // value will be "baz"
});
```Promise usage:
```javascript
var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();var hit = cache.lookup("foo", function (resolve) {
resolve(null, "baz");
});hit.then(function (value) {
console.log(value); // value will be "baz"
});
```By default a lru-cache cache with default settings is used to store cached objects but you can provide your own.
```javascript
var AsyncCache = require("exp-asynccache");
var LRU = require("lru-cache"); // any lru-cache compatible cache will dovar cache = new AsyncCache(new LRU({
max: 500,
maxAge: 1000 * 60 * 60
}));
```The resolve function can take more arguments than error and key. It will pass these to the underlying cache's set
method. So when using lru-cache you can provide a max age per key:```javascript
var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();cache.lookup("foo", function (resolve) {
resolve(null, "baz", 1000); // Let foo live for one second
});
```## Cache interface
The undelying cache should adhere to the same interface as LRUCache:
```
get(key) -> lookup value. undefined return value indicates cache miss.
set(key, value) -> set value
has(key) -> return true if key exists```
If the cache impmlemntation is asynchronous, promises can be returned
## Warning
Don't use more data from the closure than what is used to construct the cache key:
```javascript
var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();function getPerson(name, location, callback) {
cache.lookup(name, function (resolve) { // <-- Only name is used
personRepo.get(name, location, resolve); // <-- Both name and location is used
}, callback);
}
```In the above example there might be several different objects returned by personRepo for the same name but with
different locations but they are all cached only by the name. The correct code would be to construct the cache key
from both name and location.