source: Dev/trunk/src/client/dojo/store/Cache.js @ 485

Last change on this file since 485 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.2 KB
Line 
1define(["../_base/lang","../when" /*=====, "../_base/declare", "./api/Store" =====*/],
2function(lang, when /*=====, declare, Store =====*/){
3
4// module:
5//              dojo/store/Cache
6
7var 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);
15                                }
16                        });
17                        return results;
18                },
19                // look for a queryEngine in either store
20                queryEngine: masterStore.queryEngine || cachingStore.queryEngine,
21                get: function(id, directives){
22                        return when(cachingStore.get(id), function(result){
23                                return result || when(masterStore.get(id, directives), function(result){
24                                        if(result){
25                                                cachingStore.put(result, {id: id});
26                                        }
27                                        return result;
28                                });
29                        });
30                },
31                add: function(object, directives){
32                        return when(masterStore.add(object, directives), function(result){
33                                // now put result in cache
34                                cachingStore.add(object && 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
36                        });
37                },
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 when(masterStore.put(object, directives), function(result){
42                                // now put result in cache
43                                cachingStore.put(object && 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
45                        });
46                },
47                remove: function(id, directives){
48                        return when(masterStore.remove(id, directives), function(result){
49                                return cachingStore.remove(id, directives);
50                        });
51                },
52                evict: function(id){
53                        return cachingStore.remove(id);
54                }
55        });
56};
57lang.setObject("dojo.store.Cache", Cache);
58
59/*=====
60var __CacheArgs = {
61        // summary:
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.
67};
68
69Cache = declare(Store, {
70        // summary:
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.
75        // example:
76        //      |       var master = new Memory(data);
77        //      |       var cacher = new Memory();
78        //      |       var store = new Cache(master, cacher);
79        //
80        constructor: function(masterStore, cachingStore, options){
81                // masterStore:
82                //              This is the authoritative store, all uncached requests or non-safe requests will
83                //              be made against this store.
84                // cachingStore:
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.
89        },
90        query: function(query, directives){
91                // summary:
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.
99        },
100        get: function(id, directives){
101                // summary:
102                //              Get the object with the specific id.
103                // id: Number
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.
109        },
110        add: function(object, directives){
111                // summary:
112                //              Add the given object to the store.
113                // object: Object
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.
117                // returns: Number
118                //              The new id for the object.
119        },
120        put: function(object, directives){
121                // summary:
122                //              Put the object into the store (similar to an HTTP PUT).
123                // object: Object
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.
127                // returns: Number
128                //              The new id for the object.
129        },
130        remove: function(id){
131                // summary:
132                //              Remove the object with the specific id.
133                // id: Number
134                //              The identifier for the object in question.
135        },
136        evict: function(id){
137                // summary:
138                //              Remove the object with the given id from the underlying caching store.
139                // id: Number
140                //              The identifier for the object in question.
141        }
142});
143=====*/
144
145return Cache;
146});
Note: See TracBrowser for help on using the repository browser.