Skip to content

Commit 1261d17

Browse files
committed
Remove extra memcache ops for non-cacheable entities
Fixes issue #329
1 parent 2194d08 commit 1261d17

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

src/main/java/com/googlecode/objectify/cache/EntityMemcache.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,27 +325,35 @@ public void empty(Iterable<Key> keys)
325325

326326
/**
327327
* Put buckets in the cache, checking for cacheability and collisions.
328-
* @return the set of keys that were *successfully* put without collision
328+
* @return the set of keys that were *successfully* handled. That includes buckets that were put without collision
329+
* and buckets that didn't need to be cached.
329330
*/
330331
private Set<Key> cachePutIfUntouched(Iterable<Bucket> buckets)
331332
{
332-
Map<Key, CasValues> payload = new HashMap<>();
333+
final Map<Key, CasValues> payload = new HashMap<>();
334+
final Set<Key> successes = new HashSet<>();
333335

334336
for (Bucket buck: buckets)
335337
{
336-
if (!buck.isCacheable())
338+
if (!buck.isCacheable()) {
339+
successes.add(buck.getKey());
337340
continue;
341+
}
338342

339343
Integer expirySeconds = cacheControl.getExpirySeconds(buck.getKey());
340-
if (expirySeconds == null)
344+
if (expirySeconds == null) {
345+
successes.add(buck.getKey());
341346
continue;
347+
}
342348

343349
Expiration expiration = expirySeconds == 0 ? null : Expiration.byDeltaSeconds(expirySeconds);
344350

345351
payload.put(buck.getKey(), new CasValues(buck.iv, buck.getNextToStore(), expiration));
346352
}
347353

348-
return this.memcache.putIfUntouched(payload);
354+
successes.addAll(this.memcache.putIfUntouched(payload));
355+
356+
return successes;
349357
}
350358

351359
/**

src/test/java/com/googlecode/objectify/test/CachingUncacheableTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.google.appengine.api.memcache.MemcacheServiceFactory;
1010
import com.googlecode.objectify.Key;
1111
import com.googlecode.objectify.ObjectifyFactory;
12+
import com.googlecode.objectify.annotation.Cache;
1213
import com.googlecode.objectify.annotation.Entity;
1314
import com.googlecode.objectify.annotation.Id;
1415
import com.googlecode.objectify.test.util.TestBase;
@@ -35,6 +36,14 @@ static class Uncacheable {
3536
String stuff;
3637
}
3738

39+
/** */
40+
@Entity
41+
@Cache
42+
static class Cacheable {
43+
@Id Long id;
44+
String stuff;
45+
}
46+
3847
@Data
3948
static class CallCounter implements InvocationHandler {
4049
private final Object base;
@@ -75,6 +84,7 @@ public AsyncMemcacheService getAsyncMemcacheService(final String s) {
7584
});
7685

7786
fact().register(Uncacheable.class);
87+
fact().register(Cacheable.class); // needed to get caching in the code path
7888
}
7989

8090
/** */

0 commit comments

Comments
 (0)