fall-through-cache
Add fall-through caching with the cacheConnection
.
fallThroughCache( baseConnection )
Implements a getData
and getListData
that
check their cacheConnection for data. If there is data,
this data will be immediately returned.
In the background, the baseConnection
method will be called and used to update the instance or list.
Use
To use the fall-through-cache
, create a connection with a
cacheConnection and a behavior that implements
getData and getListData.
var QueryLogic = require("can-query-logic");
var queryLogic = new QueryLogic();
var cache = connect([
require("can-local-store")
],{
name: "todos",
queryLogic: queryLogic
});
var todoConnection = connect([
require("can-connect/fall-through-cache/fall-through-cache"),
require("can-connect/data/url/url"),
require("can-connect/constructor/constructor"),
require("can-connect/constructor/store/store")
], {
url: "/todos",
cacheConnection: cache,
queryLogic: queryLogic
});
Then, make requests. If the cache has the data, it will be returned immediately, and then the item or list updated later with the response from the base connection:
todoConnection.getList({due: "today"}).then(function(todos){
})
Demo
The following shows the fall-through-cache
behavior.
Clicking "Completed" or "Incomplete" will make one of the following requests and display the results in the page:
todoConnection.getList({completed: true});
todoConnection.getList({completed: false});
If you click back and forth between "Completed" and "Incomplete" multiple times you'll notice that the old data is displayed immediately and then updated after about a second.