首页 > memcached 默认最大缓存时间问题

memcached 默认最大缓存时间问题

server版本:memcached-1.4.5
驱动:Memcached-Java-Client

如平常使用

client.set(key, value, expire)

,如果expire设置为0则为永不过期,但是如果不设时间

client.set(key, value)

是否默认是永不过期呢,还是说有默认的过期时间呢。
我查看了server里源码里的memcached.c,然后网上也查到大概在123~148直接定义了最大的过期时间,现把那段代码粘上

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

如示,最大时间是30天,网上查了下有人验证过好像不设过期时间,即set(key, value)的过期时间是30天,而不是永不过期,为此,我更改了源码,更改时间为60秒然后重新用源码编译安装

#define REALTIME_MAXDELTA 60

然后用驱动(客户端)写了个程序验证,看看是否默认是60s,结果好像不对劲啊,过了许久还是可以拿到value,所以就疑惑了,也不知道更改测试的对不对,改全了没,特来此问各位大大,解惑,万分感谢,因为目前项目中就是没设过期时间,然后经常会取不到key(key的value)为空,不知道是什么问题。


Memcached-Java-Client的源码中set(key, value)expire默认给的new Date(0),发送给服务器的 expiry.getTime() / 1000,即永不失效。

REALTIME_MAXDELTA是在用来处理超过30天的exptime的。

值失效的情况还有,设置的内存大小 小于 存入的内容大小,实际内存要小于存入的内容,服务器重启等。

【热门文章】
【热门文章】