Cache.js 5.2 KB

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