Welcome to the QLache docs!
QLache is intended for use with the Express library, in line with the middleware design pattern. For background on using middleware functions, check out the Express Official Documentation.
To add QLache to your project, navigate to the root directory from your command line and install via npm, using the following command:
npm install qlache
Next, you can use the library in your code by requiring it in the file where your intended route is defined. This stores the QLache class to the label QLache.
const QLache = require('qlache');
Once the QLache class is available in memory, creating a cache is as simple as instantiating an object from the class.
const cache = new QLache(apiURL, evictionMethod, cacheSize);
apiURL
A string containing the path to the GraphQL API you want to request from.
evictionMethod
A string to decide the eviction method of the instantiated cache. Must strictly be one of "LFU", "LRU", or "MRU", corresponding to least frequently used, least recently used, and most recently used, respectively.
cacheSize
The number of entries available in your cache before data is evicted from memory.
After initializing your cache, adding its query method as a middleware function to your route will enable server-side caching.
app.post('/gqlapi', cache.query, function(req, res) {
res.send(res.locals.queryRes);
})
Note that in the final function, the the query result is sent back in the response to the client as "res.locals.queryRes". By default, the query method stores the result of your GraphQL query, whether cached or not, to the res.locals object under the key "queryRes".
That's all it takes to set up server-side caching with QLache!