java 缓存 memcached_memcached 默认最大缓存时间问题

本文围绕Memcached Java客户端展开,探讨了不设置过期时间时的默认情况。查看server源码发现最大过期时间为30天,有人验证不设过期时间默认是30天。作者更改源码为60秒并编译安装,用驱动程序验证结果却不对,项目中不设过期时间常取不到key,寻求解惑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)为空,不知道是什么问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值