简体   繁体   English

AppEngine Memcache过期政策

[英]AppEngine Memcache expiration policies

I was expecting the following AppEngine code: 我期待以下AppEngine代码:

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

Ie 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. 即,通过将条目设置到高速缓存中时在1970年设置一个到期日期,我期望它会立即被删除并可供重用。

Instead, I get: 相反,我得到:

Bar
false

Ie the entry is still around. 即条目仍然存在。

Now, the strange thing is that if I change my code to Expiration.onDate(new Date()) (without 1 ), ie setting the expiration to right before doing the put operation, I do get the expected "null, true". 现在,奇怪的是,如果我将代码更改为Expiration.onDate(new Date()) (不带1 ),即在执行放置操作之前将到期时间设置为正确,我确实会得到预期的“ null,true”。

Does Memcache somehow interpret expiration dates that are too far in the past as relative to now rather than absolute??? Memcache是​​否以某种方式将过去的过期日期解释为相对于现在而不是绝对的过期日期? But even that wouldn't fit the result, because 1ms from the put should still have expired by the time the get comes around?!? 但是,即使那样也不适合结果,因为从推杆算起,从推杆算起的1毫秒应该还已经到期?

What value can I set Expiration to, to guarantee an immediate expiration (and deletion!) of the put entry? 我可以将Expiration设置为什么值以保证put条目立即到期(和删除!)? 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! 请记住,仅使用当前时间戳可能无法可靠地工作,因为AppEngine不提供跨服务器的任何时钟同步保证!

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"). 我知道想要乍一看听起来很荒谬(为什么不删除它呢?),但是我想在这里使用它: AppEngine Memcache原子获取和删除 (句子在“结论”上方)。

Wow! 哇! This was obscure, but I figure it out: 这是模糊的,但我知道了:

Taking a look at the source for the doPut()-method in AsyncMemcacheServiceImpl.java , line 505 reads: 看一下AsyncMemcacheServiceImpl.java中doPut()方法源代码 ,第505行显示:

itemBuilder.setExpirationTime(expires == null ? 0 : expires.getSecondsValue());

And Expiration.getSecondsValue() contains the following line: Expiration.getSecondsValue()包含以下行:

return (int) (getMillisecondsValue() / 1000);

In other words: An expiration value of "0" is equivalent to no expiration (same as null ), and the expiration time is converted to seconds, by dividing by 1000. 换句话说:到期值“ 0”等于没有到期时间(与null相同),并且到期时间被除以1000转换为秒。

So, if I set an expiration of 1ms, it gets turned into 0 seconds (rounded down when divided by 1000), which is equivalent to no expiration... Blah!!! 因此,如果我将到期时间设置为1ms,它将变为0秒(除以1000时将四舍五入),这相当于没有到期时间……Bl!

So, the answer is: Expiration.onDate(new Date(1000)) should expire immediately, unless the memcache server is still living in the 60's... :) Tried it. 因此,答案是: Expiration.onDate(new Date(1000))应该立即过期,除非内存缓存服务器仍然生活在60年代... :)尝试了它。 Works. 作品。 Done... 完成...

(These tests were done on the DevServer. I'm hoping that the production server will behave the same.) (这些测试是在DevServer上完成的。我希望生产服务器的行为相同。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM