AppEngine Memcache过期策略 [英] AppEngine Memcache expiration policies

查看:137
本文介绍了AppEngine Memcache过期策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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));

生成以下输出:

  null 
true

即通过在1970年将条目放入缓存时设置过期日期,我期待它立即下降并可供重用。



相反,我得到:

  Bar 
false

即,现在,奇怪的是,如果我将我的代码更改为 Expiration.onDate(new Date())
/ code>(没有 1 ),即在执行put操作之前将过期设置为正确,I do 获得期望的null ,真正的。



Memcache是​​否会以某种方式解释过去相对于现在而言过时的过期日期,而不是绝对的?但即使这样也不符合结果,因为1ms的投入应该在获得投资回报的时候过期?!?



我可以设置什么值过期为了保证put条目立即到期(和删除!)?请记住,简单地使用当前时间戳可能无法可靠地工作,因为AppEngine不会在服务器之间提供任何时钟同步保证!



我知道想要这样做听起来毫无意义(为什么不删除它?),但我想在这里使用它: AppEngine Memcache原子获取和删除(在结论右上方的句子)。

解决方案

哇!这很晦涩,但我知道了:



查看源代码中的doPut() - AsyncMemcacheServiceImpl.java中的方法,第505行为:

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

Expiration.getSecondsValue()包含以下行:

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

换句话说:到期值0等于没有到期(与 null ),并将过期时间转换为秒数,除以1000.



所以,如果我设置1ms过期,进入0秒(除以1000后向下取整),相当于没有过期...... Blah !!!

所以,答案是: Expiration.onDate(new Date(1000))应该立即过期,除非memcache服务器仍然存在于60年代... :)试过了。作品。完成...



(这些测试是在DevServer上完成的,我希望生产服务器的行为相同。)

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").

解决方案

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:

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

And Expiration.getSecondsValue() contains the following line:

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.

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!!!

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. Works. Done...

(These tests were done on the DevServer. I'm hoping that the production server will behave the same.)

这篇关于AppEngine Memcache过期策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆