fall-through-cache
A fall through cache that checks another cacheConnection
.
fallThroughCache( baseConnection )
Implements a getData
and getListData
that
check their cacheConnection for data and then
in the background update the instance or list with data
retrieved using the base connection.
Use
To use the fall-through-cache
, create a connection with a
cacheConnection and a behavior that implements
getData and getListData.
var cache = connect([
require("can-connect/data/localstorage-cache/localstorage-cache")
],{
name: "todos"
});
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
});
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.