Cache Mixin
Managing a distributed cache requires flexibility. A client application may need to expire a cache based on response headers, or on a per feature basis.
A resource service includes a caching plan in a ServiceCacheMixin
. Customize the caching needs for your application by creating your own mixin. Or redefine the caching methods in the service prototype.
The ServiceCacheMixin
mixin provides a basic caching plan based on a time value for resource expiry. The default resource cacheDuration
property is seven (7) minutes.
With the default mixin, the resource service caches deserialized resources. The resource (model) prototype and the service cache mixin provide a basic plan for the cache behavior. A resource service is a combination of an adapter, serializer and cache object. An initializer injects the service into resources. The store service is a facade to the resource services in the application. Each service is an extension of an adapter, for a specific type of resource. And each service may use the default mixin for a caching strategy, or customize as needed. A note on the store… this.store.find('entity')
will find the appropriate service then call the service's find
method.
When a request for a resource includes other resources (with ?include=relation
) the service caches them. A resource's cacheDuration
property value defines the timing for expiry.
Cache methods
The application adapter and serializer use hooks to interact with the service's cache.
The adapter calls the cacheResource
method; which by default is a empty function (no-op). The mixin defines this method. It also defines the cacheLookup
method to retrieve a resource from the service's cache.
Using the Cache
The resource defines cacheDuration
and isCacheExpired
properties for use by the caching strategy. Set the cacheDuration
property when extending the Resource
prototype. Also setup a property for isCacheExpired
or use the default computed property.
To determine whether to retrieve a resource from cache (memory) check the isCacheExpired
property. If expired, then fetch the resource over again.
A service calls the cacheLookup
method when finding related resources. The returned collection includes non-expired resources.
Use case: a post
has many comments
, and comments
each have one commenter
.
The commenters
may not be unique. Some commenters
may have many comments
on a single post
. When a service fetches a resource and its relationships, the service will check the cache or fetch from the server.
This behavior prevents redundant requests when fetching each relation of a to-many association.
The adapter's fetch
method always sends a request to the server. The response will replace the cached resource. If you want to find data from cache, use the store.all
method.
Below is an example model hook (in a route) that first checks the cache; otherwise fetches the resource from the server.
model(params) {
return new Ember.RSVP.Promise(function (resolve, reject) {
let found = this.store.all('posts').filter(function (post) {
return post.get('id') === params.post_id;
});
if (found.get('length') > 0) {
resolve(found[0]);
} else {
let options = {
id: params.post_id,
query: { include: 'author,comments' }
};
this.store.find('post', options).then(
function (post) {
resolve(post);
},
function (error) {
reject(error);
}
);
}
}.bind(this));
}