我们知道,缓存有其过期时间,具体到 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);
}
}
通过上面的源码,我们可以清楚的看到:
REALTIME_MAXDELTA
宏规定了 memcached 的过期时间最长不能超过 30 天。- memcached 接受两种过期时间的表示,一种是过期时间间隔,即多少时间后过期;一种是过期时间点,即什么时候过期。
- 如果超过最长过期时间,memcached 会判断参数是否是过期时间点的表示,如果不是,返回1秒,相当于立即过期;如果是,返回和进程启动时间的间隔。