Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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: {
Expand All @@ -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!
Expand Down
49 changes: 31 additions & 18 deletions rediscache.js
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -9,15 +16,15 @@ 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);
// try to connect again with server config
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);
}
});
Expand All @@ -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');
Expand All @@ -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
Expand All @@ -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+'_*';

Expand All @@ -113,6 +126,6 @@ module.exports = function(Model, options) {

}else{
next();
}
}
});
}
}