CacheflowQL is an npm package with complex caching algorithms that provide developers deep insights into their GraphQL queries and metrics about their cached data, allowing for in-depth effective runtime and query analysis.
CacheflowQL is for any developer looking for a lightweight way to cache data on GraphQL servers and receive in-depth metrics concerning the perfomance of their application in order to optimize run-time by utilizing server-side and Redis caching solutions.
Download our NPM Package Here
- Install your npm dependencies: run
npm install cacheflowql
in your terminal
const { initCache, cache } = require('cacheflowql');
Local:
- checkExpire: Determines the interval that cacheflow checks for expired data
- globalThreshold: A threshold value that users can set to prevent caching until input value is met
Redis:
- To connect to a redis store provide the host ip, port, and password
initCache({
local: {
checkExpire: 10,
//How often to check if data should be deleted, in seconds
globalThreshold: 10,
//How often data needs to be requested to be cached, in seconds
},
redis: {
host: '127.0.0.1',
port: '6379',
password: <'redis store password'>
},
});
Wrap resolver within the callback function and return data from inside callback function
resolver(parent, args, ctx, info) {
return cache(
{ location: 'local', maxAge: 60, smartCache: true },
info,
function callback() { return data };
);
},
configObj = {
location: 'local',
//either 'local' or 'redis'
maxAge: 60,
//How long data will remain in local cache, in seconds
smartCache: true,
//boolean, determine if smart cache should be enabled
};
- Set cache location; either local or on redis. Local and/or redis must be initiated using initCache function if either is being used.
- How long data will remain in local cache. After maxAge (in seconds) is passed, data is marked as expired and will be deleted.
- If you want to incorporate the smartCache capabilities of cacheflowQL you need to include a parameter in your cacheConfig object called smartCache and set it equal to true. Smartcache will prevent caching until thresholds are met. Thresholds are established by comparing metrics from specific resolvers to average metrics for all of the user's cached data.
- If you want the cache function to work with mutations you need to wrap mutation resolvers in cache function as well. In the cache config object include a parameter 'mutate' whose value is equal to the name of the query resolver you want to have update.
resolver(parent, args, context, info) {
return cache(
{ location: 'redis', maxAge: 10, mutate: 'name of resolver to mutate' },
info,
function callback() {
return data;
}
);
},
The other two parameters in the cache function are info and a callback.
- The info parameter is the info parameter from the resolver itself
- The callback parameter will be whatever was in your resolver before using cacheflowQL
Simply return the call to cache with your three input parameters and you are set to cache!
return cache(cacheConfigObject, info, callback);
To run the terminal commands run node node_modules/cacheflowql/metricsTerminal.js
. The terminal will then ask whether you want to view local or global metrics.
If you want to see data about all data cached using the cache function input 'Global Metrics.' If you want to see data about a specific resolver simply enter the name of the resolver.
We’re off to a great start but are always looking to add new features! Send your comments, questions, and suggestions to [email protected] or simply fork our repo, checkout to a feature branch and get to work improving our product. Happy Caching!
cacheflowQL is a beta product developed under OSLabs