Memcached expire 设置错误引起的set(key,exp,value)为true而get(key)为null的问题

        最近项目中使用到了Memcached,而客户端选择了XMemcached ,在设置过期时间时,因对Memcached 不熟悉,将expire 设置为1000000000,本意表示尽量长的时间不要过期,但在测试时发现,memcachedClient.set(key,exp,value)结果返回true,即代表该项已成功存入缓存,但当调用memcachedClient.get(key)时始终返回为null,起初以为key的生成策略有误,后来当把exp填了个1000时,Memcached的set 和 get方法均返回true,看来问题出在expire,所以查了下文档,发现Memcached默认expire为0即代表永不过期,而这个过期时间最长为30天= 30*24*60*60 = 2592000秒,所以又将expire配置为2592000秒,发现正常执行,为了测试Memcached是否为最长30天,又将该值加了1变成2592001,此时出现了和开始一样的错误,set返回 true,但get返回为null,这样应该映证了30长最长时间的猜想。而这个时间应该是Memcached本身设置的,经过查找发现Memcached源码memcached.c里边有如下代码。注意第一行 #define REALTIME_MAXDELTA 60*60*24*30 即为30天。

#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);
    }
}
XMemcached是一个新java memcached client。Memcached 是一个高性能的分布式内存对象的key-value缓存系统,用于动态Web应用以减轻数据库负载,
现在也有很多人将它作为内存式数据库在使用,memcached通过它的自定义协议与客户端交互,而XMemcached就是它的一个java客户端实现。

关于XMemcached详细说明,参考XMemcached简介



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值