From 871cb4d6755c1de65b00916ced7702160c6a5591 Mon Sep 17 00:00:00 2001 From: Benny Iskandar Date: Wed, 6 Mar 2019 12:44:04 -0800 Subject: [PATCH] Set default cache expiry in config --- README.md | 15 +++++++++------ rediscache.js | 49 +++++++++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index efa3c06..f88fcf2 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,9 @@ loopback-redis-cache is currently extended with the following plugins. At your model (using config.json settings) ``` "mixins": { - "Rediscache": {} + "Rediscache": { + "expiryInSecs": 3600, + } } ``` At your model (using external redis server) @@ -55,19 +57,20 @@ At your model (using external redis server) "client": { "host": "redis.server.ip.address", "password": "redis-password" - } + }, + "expiryInSecs": 3600 } } ``` - Example + Example `` http://0.0.0.0:3000/api/games?cache=120 `` cache value in seconds - + ### AngularJS SDK example - + ``` Category.findOne({ filter: { @@ -82,7 +85,7 @@ At your model (using external redis server) console.log(err); }); ``` - + ### See also ----------------------- Host your web site easily, instantly and FREE PHP, MySQL and free SSL One Click WordPress Installation! diff --git a/rediscache.js b/rediscache.js index 57223dc..80902a7 100644 --- a/rediscache.js +++ b/rediscache.js @@ -1,4 +1,11 @@ module.exports = function(Model, options) { + + var cachExpire = 0; // set default to 5 minutes + + if(options.expiryInSecs){ // set from config + cachExpire = options.expiryInSecs; + } + if(options.client){ var clientSettings = options.client; }else{ @@ -9,7 +16,7 @@ module.exports = function(Model, options) { var redis = require("redis"), client = redis.createClient(clientSettings); - var redisDeletePattern = require('redis-delete-pattern'); + var redisDeletePattern = require('redis-delete-pattern'); client.on("error", function (err) { console.log(err); @@ -17,7 +24,7 @@ module.exports = function(Model, options) { if(err.toString().indexOf("invalid password") !== -1){ console.log("Invalid password... reconnecting with server config..."); var app = require('../../server/server'); - var clientSettings = app.get('redis'); + var clientSettings = app.get('redis'); client = redis.createClient(clientSettings); } }); @@ -26,9 +33,11 @@ module.exports = function(Model, options) { Model.beforeRemote('**', function(ctx, res, next) { // get all find methods and search first in cache if((ctx.method.name.indexOf("find") !== -1 || ctx.method.name.indexOf("__get") !== -1) && client.connected){ - if(typeof ctx.req.query.cache != 'undefined'){ + if(cachExpire>0){ var modelName = ctx.method.sharedClass.name; - var cachExpire = ctx.req.query.cache; + if (ctx.req.query.cache) { + cachExpire = ctx.req.query.cache; + } // set key name var cache_key = modelName+'_'+new Buffer(JSON.stringify(ctx.req.query)).toString('base64'); @@ -47,24 +56,26 @@ module.exports = function(Model, options) { }else{ //return data next(); - } - }); + } + }); }else{ next(); } }else{ next(); - } - }); + } + }); Model.afterRemote('**', function(ctx, res, next) { // get all find methods and search first in cache - if not exist save in cache if((ctx.method.name.indexOf("find") !== -1 || ctx.method.name.indexOf("__get") !== -1) && client.connected){ - if(typeof ctx.req.query.cache != 'undefined'){ + if(cachExpire>0){ var modelName = ctx.method.sharedClass.name; - var cachExpire = ctx.req.query.cache; - + if (ctx.req.query.cache) { + cachExpire = ctx.req.query.cache; + } + // set key name var cache_key = modelName+'_'+new Buffer(JSON.stringify(ctx.req.query)).toString('base64'); // search for cache @@ -80,23 +91,25 @@ module.exports = function(Model, options) { next(); }else{ next(); - } - }); + } + }); }else{ next(); } }else{ next(); - } + } }); Model.afterRemote('**', function(ctx, res, next) { // delete cache on patchOrCreate, create, delete, update, destroy, upsert if((ctx.method.name.indexOf("find") == -1 && ctx.method.name.indexOf("__get") == -1) && client.connected){ var modelName = ctx.method.sharedClass.name; - var cachExpire = ctx.req.query.cache; - + if (ctx.req.query.cache) { + cachExpire = ctx.req.query.cache; + } + // set key name var cache_key = modelName+'_*'; @@ -113,6 +126,6 @@ module.exports = function(Model, options) { }else{ next(); - } + } }); -} \ No newline at end of file +}