Caching in Loopback

1,269 views
Skip to first unread message

noah.p...@storyworldwide.com

unread,
Nov 17, 2015, 11:37:42 AM11/17/15
to LoopbackJS
To what extent does loopback cache responses, if at all? I couldn't find any documentation that speaks to this, but it seems like there is some caching of model relations as evidence by the__cachedRelations property. 

If there is no internal caching, what are some recommended approaches to implementing caching?

Raymond Feng

unread,
Nov 17, 2015, 11:57:18 AM11/17/15
to LoopbackJS
There is no built-in cache in LoopBack (yet). I wish we can add it as a policy to the CRUD operations. 

To start, you can try https://github.com/ptarjan/node-cache and use it with operation hooks.

Thanks,

---
Raymond Feng (rf...@us.ibm.com)
Co-Founder and Architect @ StrongLoop, Inc. (An IBM Company)

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

On Nov 17, 2015, at 8:37 AM, noah.p...@storyworldwide.com wrote:

To what extent does loopback cache responses, if at all? I couldn't find any documentation that speaks to this, but it seems like there is some caching of model relations as evidence by the__cachedRelations property. 

If there is no internal caching, what are some recommended approaches to implementing caching?

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/6d074c83-efd4-4d5a-a77e-2139892c2505%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

noah.p...@storyworldwide.com

unread,
Nov 17, 2015, 1:07:07 PM11/17/15
to LoopbackJS
Wonderful, thank you Raymond.

noah.p...@storyworldwide.com

unread,
Nov 17, 2015, 2:25:59 PM11/17/15
to LoopbackJS
One more thing. Say I add a global beforeRemote hook in server/boot/hooks.js. If I find something in the cache for the request and set ctx.result accordingly, how do I keep Loopback from processing the request as it normally would?


On Tuesday, November 17, 2015 at 11:37:42 AM UTC-5, noah.p...@storyworldwide.com wrote:

noah.p...@storyworldwide.com

unread,
Nov 17, 2015, 3:53:44 PM11/17/15
to LoopbackJS
I was able to bypass normal processing of the request with the following, but curious if there is a better approach:

var getCacheKey = function( ctx )
{
return new Buffer( ctx.req.url + JSON.stringify( ctx.req.remotingContext.args ) ).toString('base64');
}

remotes.before('*.__get__', function(ctx, next) {
var key = getCacheKey( ctx );
if( memoryCache.get( key ) )
{
 var result = memoryCache.get( key );
 ctx.res.status( ctx.res.statusCode ).send( result );
}
else
{
 next();
}
});


On Tuesday, November 17, 2015 at 11:37:42 AM UTC-5, noah.p...@storyworldwide.com wrote:
Reply all
Reply to author
Forward
0 new messages