Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
exp-asynccache
Advanced tools
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.
npm install --save exp-asynccache
Callback usage:
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:
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.
var AsyncCache = require("exp-asynccache");
var LRU = require("lru-cache"); // any lru-cache compatible cache will do
var 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:
var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();
cache.lookup("foo", function (resolve) {
resolve(null, "baz", 1000); // Let foo live for one second
});
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
Don't use more data from the closure than what is used to construct the cache key:
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.
FAQs
A small async cache
The npm package exp-asynccache receives a total of 1,110 weekly downloads. As such, exp-asynccache popularity was classified as popular.
We found that exp-asynccache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 12 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.