Security News
cURL Project and Go Security Teams Reject CVSS as Broken
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
cache-service
Advanced tools
A multi-tiered caching solution for node.
If you use superagent from node, check out superagent-cache to get superagent queries with cache-service built right in.
cache-service allows you to create redundant, cache-agnostic caching configurations. By default, it supports redis (using node_redis) and node-cache, but you can add any cache you want as long as you follow the same interface.
Require and instantiate cache-service as follows:
var cs = require('cache-service').cacheService;
var cacheService = new cs();
This gives you the default configuration. Now you can cache like normal with the benefit of a tiered solution:
function getData(key, cb){
cacheService.get(key, function (err, response){
if(!err){
cb(err, response);
}
else{
performQuery(key, function (err, response){
var value = response.body.user;
cacheService.set(key, value);
cb(err, user);
});
}
});
}
npm install cache-service
npm test
By following the Basic Usage example above, cache-service will:
All caches will have a defaultExpiration of 900 seconds unless specified otherwise.
cache-service's constructor takes two optional parameters in the following order: cacheServiceConfig and cacheModuleConfig:
var cacheService = new cs(cacheServiceConfig, cacheModuleConfig);
This is where you set cache-service-level config options.
A namespace to be applied to all keys generated for this instance of cache-service.
When false, cache-service will log only errors. When true, cache-service will log all activity (useful for testing and debugging).
Let's say you have an instance of cache-service with two caches inside of it: cacheA and cacheB. If cacheA has a shorter defaultExpiration than cacheB and cacheA does not have the key for which you're looking, cache-service will then look in cacheB. If cache-service finds the desired key in cacheB and writeToVolatileCaches
is true
, cache-service will then write that key to cacheA.
This is particularly useful if you want to have a short-term in-memory cache with the most used queries and a longer-term redis cache with all of the cached data.
This is where you tell cache-service which caches you want and how you want them configured. Here is an example cacheModuleConfig:
var cacheModuleConfig = [
{
type: 'node-cache',
defaultExpiration: 300,
cacheWhenEmpty: false
},
{
type: 'redis',
redisEnv: 'REDISCLOUD_URL',
defaultExpiration: 900,
cacheWhenEmpty: false
}
]
This config would attempt to create a primary node-cache instance with a fallback redis cache. The node-cache instance would have a 5-minute defaultExpiration and the redis instance would have a 15-minute default expiraiton.
Here are all the available options:
This is the type of cache you want to use. Currently, the only options are 'redis', 'node-cache', and 'custom'. If you choose 'redis' or 'node-cache', cache-service will create an instance of that cache type for you using the assiciated config options. If you choose 'custom', you can pass in your own cache instance and it will work as long as you match the Cache Module Interface.
If you need external access to a redis or node-cache instance, you can instantiate a cache-module and then pass it as a 'custom' cache. See Standalone Cache Module Usage for more info.
type
'redis')This is the most generic way to pass in your redis configuraiton options.
var redisData = {
port: myRedisPort,
hostname: myRedisHostname,
auth: myRedisAuth
}
type
'redis')If you have all of your redis params already prepared as a URL in the following format: http://uri:password@hostname:port
, then you can simply pass that URL with the object key redisUrl
.
type
'redis')If you have a redis URL contained in an env variable (in process.env[redisEnv]), cache-service can retrieve it for you if you pass the env variable name with the object key redisEnv
.
The expiration to include when executing cache set commands. Can be overridden via .set()
's optional expiraiton param.
By default, if two subsequent caches have the same defaultExpiraiton
, the second of the two caches will be checked in the event that the first cache does not have a key. If checkOnPreviousEmpty
is false
, cache-service will not check subsequent caches with the same defaultExpiration
.
Whether a cache should not be written to. Useful if you're sharing a redis cache with another team and your contract with them is that you will not alter their data.
Only for use with superagent-cache. Whether this cache should be evaluated only in the event of an API failure. This is useful when you want to have an extremely long-term cache to serve data when an API is down. Currently, only the first cache module with postApi set to true will be used.
Retrieve a value by a given key.
Retrieve the values belonging to a series of keys. If a key is not found, it will not be in response
.
Set a value by a given key.
Set multiple values to multiple keys
This function exposes a heirarchy of expiration values as follows:
expiration
property of a key that also contains a cacheValue
property will override all other expirations. (This means that, if you are caching an object, the string 'cacheValue' is a reserved property name within that object.)cacheValue
and expiration
as properties is not present, the expiration
provided to the .mset()
argument list will be used.defaultExpiration
will be applied.Delete a key or an array of keys and their associated values.
Flush all keys and values from an instance of cache-service.
When you pass a cacheModuleConfig
to cache-service's constructor, it internally instantiates cache modules based on what data you provide. For example, the following cacheModuleConfig
will internally instantiate a single nodeCacheModule instance with the given settings:
[
{
type: 'node-cache',
defaultExpiration: 60,
cacheWhenEmpty: false
}
]
But what if you want to manually instantiate your cache modules? There are several reason you might want to do this including:
.mset()
or built-in logging)cache-service provides two native cache modules. A cache module is simply a wrapper for a cache type. The modules provided are for node-cache and redis. To require nodeCacheModule for manual instantiation, do the following:
var nodeCacheModule = require('cache-service').nodeCacheModule;
To instantiate it, simply pass almost the same object we passed above in the cacheModuleConfig
array as follows:
var nodeCacheModuleInstance = new nodeCacheModule({
//type is not necessary since we're instantiating a specific type manually
defaultExpiration: 60,
cacheWhenEmpty: false
}).cache;
Now let's pass our manually instantiated nodeCacheModuleInstance into our cache-service constructor:
var cacheService = new cs({}, [
{
type: 'custom', //A type of 'custom' tells cache-service that this cache has already been instantiated
cache: nodeCacheModuleInstance
}
]);
Documentation coming soon.
.mget()
and .mset()
functions.mset()
and .mget()
provide the same callback params from both redisCacheModule and nodeCacheModuleFAQs
A tiered caching solution for node.
The npm package cache-service receives a total of 115 weekly downloads. As such, cache-service popularity was classified as not popular.
We found that cache-service demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.