]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/store/Cache.js.uncompressed.js
fd94521b51ba0dc84e23b21f15761f99aa0d826f
1 define("dojo/store/Cache", ["../_base/lang","../_base/Deferred" /*=====, "../_base/declare", "./api/Store" =====*/],
2 function(lang
, Deferred
/*=====, declare, Store =====*/){
7 var Cache = function(masterStore
, cachingStore
, options
){
8 options
= options
|| {};
9 return lang
.delegate(masterStore
, {
10 query: function(query
, directives
){
11 var results
= masterStore
.query(query
, directives
);
12 results
.forEach(function(object
){
13 if(!options
.isLoaded
|| options
.isLoaded(object
)){
14 cachingStore
.put(object
);
19 // look for a queryEngine in either store
20 queryEngine
: masterStore
.queryEngine
|| cachingStore
.queryEngine
,
21 get: function(id
, directives
){
22 return Deferred
.when(cachingStore
.get(id
), function(result
){
23 return result
|| Deferred
.when(masterStore
.get(id
, directives
), function(result
){
25 cachingStore
.put(result
, {id
: id
});
31 add: function(object
, directives
){
32 return Deferred
.when(masterStore
.add(object
, directives
), function(result
){
33 // now put result in cache
34 cachingStore
.add(typeof result
== "object" ? result
: object
, directives
);
35 return result
; // the result from the add should be dictated by the masterStore and be unaffected by the cachingStore
38 put: function(object
, directives
){
39 // first remove from the cache, so it is empty until we get a response from the master store
40 cachingStore
.remove((directives
&& directives
.id
) || this.getIdentity(object
));
41 return Deferred
.when(masterStore
.put(object
, directives
), function(result
){
42 // now put result in cache
43 cachingStore
.put(typeof result
== "object" ? result
: object
, directives
);
44 return result
; // the result from the put should be dictated by the masterStore and be unaffected by the cachingStore
47 remove: function(id
, directives
){
48 return Deferred
.when(masterStore
.remove(id
, directives
), function(result
){
49 return cachingStore
.remove(id
, directives
);
53 return cachingStore
.remove(id
);
57 lang
.setObject("dojo.store.Cache", Cache
);
62 // These are additional options for how caching is handled.
63 // isLoaded: Function?
64 // This is a function that will be called for each item in a query response to determine
65 // if it is cacheable. If isLoaded returns true, the item will be cached, otherwise it
66 // will not be cached. If isLoaded is not provided, all items will be cached.
69 Cache = declare(Store, {
71 // The Cache store wrapper takes a master store and a caching store,
72 // caches data from the master into the caching store for faster
73 // lookup. Normally one would use a memory store for the caching
74 // store and a server store like JsonRest for the master store.
76 // | var master = new Memory(data);
77 // | var cacher = new Memory();
78 // | var store = new Cache(master, cacher);
80 constructor: function(masterStore, cachingStore, options){
82 // This is the authoritative store, all uncached requests or non-safe requests will
83 // be made against this store.
85 // This is the caching store that will be used to store responses for quick access.
86 // Typically this should be a local store.
87 // options: __CacheArgs?
88 // These are additional options for how caching is handled.
90 query: function(query, directives){
92 // Query the underlying master store and cache any results.
93 // query: Object|String
94 // The object or string containing query information. Dependent on the query engine used.
95 // directives: dojo/store/api/Store.QueryOptions?
96 // An optional keyword arguments object with additional parameters describing the query.
97 // returns: dojo/store/api/Store.QueryResults
98 // A QueryResults object that can be used to iterate over.
100 get: function(id, directives){
102 // Get the object with the specific id.
104 // The identifier for the object in question.
105 // directives: Object?
106 // Any additional parameters needed to describe how the get should be performed.
107 // returns: dojo/store/api/Store.QueryResults
108 // A QueryResults object.
110 add: function(object, directives){
112 // Add the given object to the store.
114 // The object to add to the store.
115 // directives: dojo/store/api/Store.AddOptions?
116 // Any additional parameters needed to describe how the add should be performed.
118 // The new id for the object.
120 put: function(object, directives){
122 // Put the object into the store (similar to an HTTP PUT).
124 // The object to put to the store.
125 // directives: dojo/store/api/Store.PutDirectives?
126 // Any additional parameters needed to describe how the put should be performed.
128 // The new id for the object.
130 remove: function(id){
132 // Remove the object with the specific id.
134 // The identifier for the object in question.
138 // Remove the object with the given id from the underlying caching store.
140 // The identifier for the object in question.