Memcache maximum key expiration time

前端 未结 7 1090
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 03:45

What\'s memcached\'s maximum key expiration time?

If I don\'t provide an expiration time and the cache gets full, what happens?

相关标签:
7条回答
  • 2020-12-05 04:26

    No there is no limit. The 30 days limit is if you give the amount of seconds it should stay there, but if you give a timestamp, there is only the max long or int value on the machine which can be a limit.

    ->set('key', 'value', time() + 24*60*60*365) will make the key stay there for a year for example, but yeah if the cache gets full or restarted in between, this value can be deleted.

    0 讨论(0)
  • 2020-12-05 04:28

    You can set key expiration to a date, by supplying a Unix timestamp instead of a number of days. This date can be more than 30 days in the future:

    Expiration times are specified in unsigned integer seconds. They can be set from 0, meaning "never expire", to 30 days (60*60*24*30). Any time higher than 30 days is interpreted as a unix timestamp date. If you want to expire an object on january 1st of next year, this is how you do that.

    https://github.com/memcached/memcached/wiki/Programming#expiration

    But, as you say, if you’re setting key expiration to an amount of time rather than a date, the maximum is 2,592,000 seconds, or 30 days.

    0 讨论(0)
  • 2020-12-05 04:30

    OK, I found out that the number of seconds may not exceed 2592000 (30 days). So the maximum expiration time is 30 days.

    0 讨论(0)
  • 2020-12-05 04:31

    Looks like some answers are not valid anymore.

    I found out a key does not get set at all when the TTL is too high. For example 2992553564.

    Tested with the following PHP code:

    var_dump($memcached->set($id, "hello", 2992553564);  // true
    var_dump($memcached->get($id));                      // empty!
    
    var_dump($memcached->set($id, "hello", 500);  // true
    var_dump($memcached->get($id));               // "hello"
    

    Version is memcached 1.4.14-0ubuntu9.

    0 讨论(0)
  • 2020-12-05 04:44

    If you don't provide expiration and cache gets full then the oldest key-values are expired first:

    Memory is also reclaimed when it's time to store a new item. If there are no free chunks, and no free pages in the appropriate slab class, memcached will look at the end of the LRU for an item to "reclaim". It will search the last few items in the tail for one which has already been expired, and is thus free for reuse. If it cannot find an expired item however, it will "evict" one which has not yet expired. This is then noted in several statistical counters

    https://github.com/memcached/memcached/wiki/UserInternals#when-are-items-evicted

    0 讨论(0)
  • 2020-12-05 04:46

    An expiration time, in seconds. Can be up to 30 days. After 30 days, is treated as a unix timestamp of an exact date.

    https://code.google.com/p/memcached/wiki/NewCommands#Standard_Protocol

    0 讨论(0)
提交回复
热议问题