Skip to content

Commit

Permalink
Includes a beautiful transparent layer for letting you cache datastore
Browse files Browse the repository at this point in the history
queries by simply calling the `setCacheKey("key")` method on the
datastore query
  • Loading branch information
lmatteis committed Oct 5, 2011
1 parent 820857b commit b65c479
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion war/WEB-INF/modules/googlestore.js
Expand Up @@ -4,6 +4,8 @@
*/
importPackage(com.google.appengine.api.datastore);

require("memcache.js");

var googlestore = (function(){

// syntax sugar
Expand Down Expand Up @@ -55,6 +57,7 @@ var googlestore = (function(){
}
},
put: function(entity) {
memcache.clearAll();
return this.datastore.put(entity);
},
// mimics JDO functionality
Expand All @@ -65,11 +68,13 @@ var googlestore = (function(){
return entity;
},
del: function(key) {
memcache.clearAll();
this.datastore["delete"](key);
},
query: function(kind) {
var q = new Query(kind);
var options = FetchOptions.Builder.withDefaults();
var cacheKey = null;
var self;
function filter(propertyName, operator, value) {
operator = filterOperators[operator] || operator;
Expand All @@ -93,10 +98,24 @@ var googlestore = (function(){
options = options.offset(offset);
return self;
}
function setCacheKey(key) {
cacheKey = key;
return self;
}
function fetch(num) {
if(cacheKey) {
var data = memcache.get(cacheKey);
if(data) {
//log("getting it from cache");
return data;
}
}
//log("getting it from datastore");
if (num) limit(num);
var preparedQuery = googlestore.datastore.prepare(q);
return preparedQuery.asList(options).toArray();
var ret = preparedQuery.asList(options).toArray();
memcache.put(cacheKey, ret);
return ret;
}
function fetchAsIterable(num) {
if (num) limit(num);
Expand All @@ -113,6 +132,7 @@ var googlestore = (function(){
setKeysOnly: setKeysOnly,
limit : limit,
offset : offset,
setCacheKey: setCacheKey,
fetch : fetch,
fetchAsIterable : fetchAsIterable,
count : count
Expand Down

0 comments on commit b65c479

Please sign in to comment.