Memcached expire設定エラーによるset(key,exp,value)がtrueでget(key)がnullである問題


最近のプロジェクトではMemcachedを使用しましたが、クライアントはXMemcachedを選択しました。期限が切れた時、Memcachedに不慣れなので、expireを100000万に設定しました。本来は期限が切れないように長い時間をかけていましたが、テスト時にmemcachedClint.set(key,exp,value)がtrueに戻りました。つまり、キャッシュに成功しました。しかし、memcachedCliennt.get(key)を呼び出した時はずっとnullに戻ります。最初はkeyの生成戦略が間違っていたと思いましたが、その後、expを1000個記入した時、Memcachedのsetとget方法はいずれもtrueに戻ります。expireに問題があるようです。だから、文書を調べたら、Memcachexpireがデフォルトexpireは0である。即ち、いつまでも期限が切れないということが分かりました。したがって、expireを2592000秒に設定し、正常に実行されていることを確認するために、Memcachedが最長30日間かどうかをテストするために、この値をプラスして1が2592001になりました。この時は最初と同じエラーが発生しました。setはtrueに戻りますが、getはnullに戻ります。これは30時間の最長の予想を反映するべきです。この時間はMemcached自身が設置したはずです。検索したらMemcachedソースmemcached.cの中に下記のコードがあります。1行目に注意してください。MAX DELTA 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は、データベースの負荷を軽減するために動的なWebアプリケーションのための、高性能な分散メモリオブジェクトのkey-valueキャッシュシステムである。
今も多くの人がメモリ型データベースとして使っています。memcachedはそのカスタムプロトコルを通じてクライアントと対話します。XMemcachedはその一つのjavaクライアントとして実現します。
XMemcachedの詳細については、XMemcachedの概要を参照してください。