memcached java客户端的过期时间问题

在向memcached中存放对象时,可以只能一个过期时间,对于这个过期时间,看memcached java client api docs 上也没有说的太清楚,看到两个不同地方使用了不同的过期时间的表示法,一种是 new Date(1000 * s), 另一种是 new Date(System.currentTimeMillis() + 1000  * s),看了这两种写法,我开始以为肯定是某一中是错误的,通过测试,这两种方式都能很好的工作,这些彻底把我搞糊涂了,后来看了一些memcached server的源代码,其中有一个function是,

#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 <= stats.started)             return (rel_time_t)1;         return (rel_time_t)(exptime - stats.started);     } else {         return (rel_time_t)(exptime + current_time);     } }

此时才明白为什么两种写法都行。上述第一种是传给服务器一个过期的秒数,即多少秒后过期,第二种是设置什么时候过期,设置时间在在client边,判断过期又是在在服务器上,如果两个机器的时间不一致,且差别大的时候,可能回遇到写入的对象无法读出来(其实是由于时间不一致,过期了),所以推荐使用第一种方式!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值