source: Dev/branches/rest-dojo-ui/client/dojo/store/Cache.js @ 263

Last change on this file since 263 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 5.2 KB
Line 
1define(["../_base/lang","../_base/Deferred"
2],function(lang, Deferred) {
3        // module:
4        //              dojo/store/Cache
5        // summary:
6        //              TODOC
7
8var store = lang.getObject("dojo.store", true);
9
10/*=====
11dojo.declare("dojo.store.__CacheArgs", null, {
12        constructor: function(){
13                // summary:
14                //              These are additional options for how caching is handled.
15                // isLoaded: Function?
16                //              This is a function that will be called for each item in a query response to determine
17                //              if it is cacheable. If isLoaded returns true, the item will be cached, otherwise it
18                //              will not be cached. If isLoaded is not provided, all items will be cached.
19                this.isLoaded = isLoaded;
20        }
21});
22=====*/
23store.Cache = function(masterStore, cachingStore, /*dojo.store.__CacheArgs*/ options){
24        // summary:
25        //              The Cache store wrapper takes a master store and a caching store,
26        //              caches data from the master into the caching store for faster
27        //              lookup. Normally one would use a memory store for the caching
28        //              store and a server store like JsonRest for the master store.
29        // masterStore:
30        //              This is the authoritative store, all uncached requests or non-safe requests will
31        //              be made against this store.
32        // cachingStore:
33        //              This is the caching store that will be used to store responses for quick access.
34        //              Typically this should be a local store.
35        // options:
36        //              These are additional options for how caching is handled.
37        options = options || {};
38        return lang.delegate(masterStore, {
39                query: function(query, directives){
40                        var results = masterStore.query(query, directives);
41                        results.forEach(function(object){
42                                if(!options.isLoaded || options.isLoaded(object)){
43                                        cachingStore.put(object);
44                                }
45                        });
46                        return results;
47                },
48                // look for a queryEngine in either store
49                queryEngine: masterStore.queryEngine || cachingStore.queryEngine,
50                get: function(id, directives){
51                        return Deferred.when(cachingStore.get(id), function(result){
52                                return result || Deferred.when(masterStore.get(id, directives), function(result){
53                                        if(result){
54                                                cachingStore.put(result, {id: id});
55                                        }
56                                        return result;
57                                });
58                        });
59                },
60                add: function(object, directives){
61                        return Deferred.when(masterStore.add(object, directives), function(result){
62                                // now put result in cache
63                                return cachingStore.add(typeof result == "object" ? result : object, directives);
64                        });
65                },
66                put: function(object, directives){
67                        // first remove from the cache, so it is empty until we get a response from the master store
68                        cachingStore.remove((directives && directives.id) || this.getIdentity(object));
69                        return Deferred.when(masterStore.put(object, directives), function(result){
70                                // now put result in cache
71                                return cachingStore.put(typeof result == "object" ? result : object, directives);
72                        });
73                },
74                remove: function(id, directives){
75                        return Deferred.when(masterStore.remove(id, directives), function(result){
76                                return cachingStore.remove(id, directives);
77                        });
78                },
79                evict: function(id){
80                        return cachingStore.remove(id);
81                }
82        });
83};
84/*=====
85dojo.declare("dojo.store.Cache", null, {
86        // example:
87        //      |       var master = new dojo.store.Memory(data);
88        //      |       var cacher = new dojo.store.Memory();
89        //      |       var store = new dojo.store.Cache(master, cacher);
90        //
91        query: function(query, directives){
92                // summary:
93                //              Query the underlying master store and cache any results.
94                // query: Object|String
95                //              The object or string containing query information. Dependent on the query engine used.
96                // directives: dojo.store.util.SimpleQueryEngine.__queryOptions?
97                //              An optional keyword arguments object with additional parameters describing the query.
98                // returns: dojo.store.util.QueryResults
99                //              A QueryResults object that can be used to iterate over.
100        },
101        get: function(id, directives){
102                // summary:
103                //              Get the object with the specific id.
104                // id: Number
105                //              The identifier for the object in question.
106                // directives: dojo.store.__GetOptions?
107                //              Any additional parameters needed to describe how the get should be performed.
108                // returns: dojo.store.util.QueryResults
109                //              A QueryResults object.
110        },
111        add: function(object, directives){
112                // summary:
113                //              Add the given object to the store.
114                // object: Object
115                //              The object to add to the store.
116                // directives: dojo.store.__AddOptions?
117                //              Any additional parameters needed to describe how the add should be performed.
118                // returns: Number
119                //              The new id for the object.
120        },
121        put: function(object, directives){
122                // summary:
123                //              Put the object into the store (similar to an HTTP PUT).
124                // object: Object
125                //              The object to put to the store.
126                // directives: dojo.store.__PutOptions?
127                //              Any additional parameters needed to describe how the put should be performed.
128                // returns: Number
129                //              The new id for the object.
130        },
131        remove: function(id, directives){
132                // summary:
133                //              Remove the object with the specific id.
134                // id: Number
135                //              The identifier for the object in question.
136                // directives: dojo.store.__RemoveOptions?
137                //              Any additional parameters needed to describe how the remove should be performed.
138        },
139        evict: function(id){
140                // summary:
141                //              Remove the object with the given id from the underlying caching store.
142                // id: Number
143                //              The identifier for the object in question.
144        }
145});
146=====*/
147return store.Cache;
148});
Note: See TracBrowser for help on using the repository browser.