在向memcached中存放对象时,可以只能一个过期时间,对于这个过期时间,看memcached java client api docs 上也没有说的太清楚,看到两个不同地方使用了不同的过期时间的表示法,一种是 new Date(1000 * s), 另一种是 new Date(System.currentTimeMillis() + 1000 * s),看了这两种写法,我开始以为肯定是某一中是错误的,通过测试,这两种方式都能很好的工作,这些彻底把我搞糊涂了,后来看了一些memcached server的源代码,其中有一个function是,
#define REALTIME_MAXDELTA 60*60*24*30/* * given time value that's either unix time or delta from current unix time, return * unix time. Use the fact that delta can't exceed one month (and real time value can't * be that low). */static rel_time_t realtime(const time_t exptime) { /* no. of seconds in 30 days - largest possible delta exptime */
if (exptime == 0) return 0; /* 0 means never expire */
if (exptime > REALTIME_MAXDELTA) { /* if item expiration is at/before the server started, give it an expiration time of 1 second after the server started. (because 0 means don't expire). without this, we'd underflow and wrap around to some large value way in the future, effectively making items expiring in the past really expiring never */ if (exptime <= stats.started) return (rel_time_t)1; return (rel_time_t)(exptime - stats.started); } else { return (rel_time_t)(exptime + current_time); }}
此时才明白为什么两种写法都行。上述第一种是传给服务器一个过期的秒数,即多少秒后过期,第二种是设置什么时候过期,设置时间在在client边,判断过期又是在在服务器上,如果两个机器的时间不一致,且差别大的时候,可能回遇到写入的对象无法读出来(其实是由于时间不一致,过期了),所以推荐使用第一种方式!
版权声明:本文为博主原创文章,未经博主允许不得转载。