Express.js, Wordrpress API and Cache
This is a sample made with express.js and 2 libraries: node-wpapi and node-cache.
You can find the code here.
git clone https://github.com/monchopena/express-wordpressapi-cache cd express-wordpressapi-cache npm i npm start
Into “lib/wordpress.js” you can put your API url page.
This is the file where we are checking if we can access to cached data “routes/index.js”.
var isCached = 'posts'; var cachePosts = cache.get(isCached);
If we can’t access to cached data then we get the posts from the API and then create the cache.
Into the file “routes/index”:
cache.put('posts', passPosts, 5000); // 5 seconds
In this case we put 5000ms. If time isn’t passed in, it is stored forever.
Using cache, displaying the pages will be much more efficient.