kwhitley / apicache

Simple API-caching middleware for Express/Node.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there any way to get directly from cache without using route ?

manojktechie opened this issue · comments

I have one route which give me the list of the countries and save it to cache. I have another route which give me the country details by country id. Since I already have all the countries list in cache, instead of fetching data from database for single country, I would like to filter country list from cache and then return the response and also save it into cache with different key. Is this possible ??

My present Code:

/* * Get list of countries */ this.list = function (req, res) { req.apicacheGroup = COUNTRIES country .findAll() .then(function (data) { res.status(200).send(data); }) .catch(err => { res.status(500).send({ message: 'Unable to get countries' }); }); };

/* * Get Country by Id */ this.getById = function (req, res) { req.apicacheGroup = COUNTRIES const id = req.params.id country .findByPk(id) .then(function (data) { res.status(200).send(data); }) .catch(err => { res.status(500).send({ message: 'Unable to get country by id' }); }); };

In the second API, instead of doing country.findByPK(id), I would like to get cache using cacheKey and then filter the data, if available. If not available, it will make fetch from database and store it in cache

Regards,

  • Manoj

I don't believe so, no (short answer). The unfortunate thing about how this library evolved is that it doesn't enforce a common interface for the memory caches via some sort of wrapper. This leaves a lot of "if-then" junk throughout the code to check and handle specifically for the memory cache vs the redis implementation.

This is a great idea, and one I'd love to expose once we actually get an interface pattern to modularize caches so they can be a drop in to apicache!