Memcached最大缓存时间

首先要说,以前还真少想过,一般都是自己设置需要的时间,没有涉及到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 , 而且从实际应用上,这么变态的时间也已经足够了。看源代码是终极办法啊。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值