I was expecting the following AppEngine code:
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
memcache.put("Foo", "Bar", Expiration.onDate(new Date(1)));
System.out.println(memcache.get("Foo"));
System.out.println(memcache.put("Foo", "Baz", null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT));
to yield the following output:
null
true
I.e. by setting an Expiration date in 1970 when putting the entry into the cache, I was expecting it to get dropped immediately and be available for reuse.
Instead, I get:
Bar
false
I.e. the entry is still around.
Now, the strange thing is that if I change my code to Expiration.onDate(new Date())
(without 1
), i.e. setting the expiration to right before doing the put operation, I do get the expected "null, true".
Does Memcache somehow interpret expiration dates that are too far in the past as relative to now rather than absolute??? But even that wouldn't fit the result, because 1ms from the put should still have expired by the time the get comes around?!?
What value can I set Expiration to, to guarantee an immediate expiration (and deletion!) of the put entry? Keep in mind that simply using the current time-stamp might not work reliably as AppEngine does not offer any clock-synchronization guarantees across servers!
I know that wanting to do this sounds nonsensical at first glance (why not just delete it?), but I would like to use it here: AppEngine Memcache atomic get-and-delete (sentence right above "Conclusion").