首先要说,以前还真少想过,一般都是自己设置需要的时间,没有涉及到Memcached最大缓存时间的问题,今天面试时,对方提出这个问题。
我看到的一行代码里,Memcached的缓存时间被设置为86400*24*4, 等于是96天。而当调用set去存储的时候,memcached会返回true。而当你用get的时候,就是相反的结果了: FALSE 。
要说BT呢,因为用到Memcached的人,通常不会去给缓存96天,这种情况下,更好的选择是DB、filecache或者其他。尽管BT,我 还是去测试了一下最长时间,得到的结果是: 30 days。去memcached源码里查了一下,这下就明白多了:
#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 <= process_started)
return (rel_time_t)1;
return (rel_time_t)(exptime – process_started);
} else {
return (rel_time_t)(exptime + current_time);
}
}
Memcached最大缓存时间为30d , 而且从实际应用上,这么变态的时间也已经足够了。看源代码是终极办法啊。