Data loading and caching for Rill.
npm install @rill/loaderUtility to handle cached data loading in Rill.
``console`
npm install @rill/loader
#### Load the data in middleware.
`js
const app = require('rill')()
const loader = require('@rill/loader')
app.use(loader())
app.get('/my-view',
(ctx, next)=> {
// A #load function will be attached to the context.
return ctx.load('myStuff', ...).then((myStuff)=> {
// Loaded data cached automatically attached to ctx.locals
ctx.locals.myStuff; //-> 'data'
})
}
)
`
#### Register a data loader.
`js
const loader = require('@rill/loader')
// Register a loadable item.
loader.register(
{ name: 'myStuff', ttl: '30 minutes' },
(ctx, ...)=> {
// Return any promise of data and it will be cached.
return myApi.fetchMyStuff();
}
);
`
###ctx.load(name:String, arguments...)name
Requests data from a registered loader and returns cached data if possible. is the name of the loader and arguments are provided to the loader.
###loader.register(opts:Object, getter:Function)ctx.locals
Registers a getter function with the loader.
This function will be cached and automatically set it's data on when loaded.
#### Register Options
`jsctx.locals
{
// The name where the data will be stored on .
name: "myStuff",
// A timeout (in milliseconds or as a string) that the data will deleted in.
ttl: 3000,
// If true the ttl option will be reset every time the data is loaded.
refresh: false,
// If shared is set the true then a global cache will be used on the server side. (By default is uses the users session).
shared: false
}
`
---
* Use npm test` to run tests.
Please feel free to create a PR!