【发布时间】:2010-11-27 22:44:20
【问题描述】:
memcached 的最长密钥过期时间是多少?
如果我不提供过期时间并且缓存已满,会发生什么?
【问题讨论】:
标签: memcached
memcached 的最长密钥过期时间是多少?
如果我不提供过期时间并且缓存已满,会发生什么?
【问题讨论】:
标签: memcached
您可以通过提供 Unix 时间戳而不是天数来将密钥过期设置为日期。此日期可能是未来 30 天以上:
到期时间以无符号整数秒为单位指定。它们可以设置为从 0(表示“永不过期”)到 30 天 (60*60*24*30)。任何超过 30 天的时间都被解释为 unix 时间戳日期。如果您想在明年 1 月 1 日使对象过期,您可以这样做。
https://github.com/memcached/memcached/wiki/Programming#expiration
但是,as you say,如果您将密钥到期时间设置为时间量而不是日期,则最大值为 2,592,000 秒或 30 天。
【讨论】:
如果您不提供过期并且缓存已满,则最旧的键值将首先过期:
在存储新项目时也会回收内存。如果没有空闲块,并且相应的slab 类中没有空闲页,memcached 将在LRU 的末尾查找要“回收”的项目。它将在尾部的最后几个项目中搜索已经过期的项目,因此可以免费重复使用。但是,如果它找不到过期的项目,它将“驱逐”尚未过期的项目。然后在几个统计计数器中记录这一点
https://github.com/memcached/memcached/wiki/UserInternals#when-are-items-evicted
【讨论】:
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.
不,没有限制。 30 天的限制是如果你给出它应该在那里停留的秒数,但如果你给出一个时间戳,那么机器上只有最大的 long 或 int 值可以作为限制。
->set('key', 'value', time() + 24*60*60*365) 将使密钥在那里保留一年,但是如果缓存已满或在这期间重新启动,则可以删除此值。
【讨论】:
过期时间,以秒为单位。最长可达 30 天。 30天后, 被视为确切日期的 unix 时间戳。
https://code.google.com/p/memcached/wiki/NewCommands#Standard_Protocol
【讨论】:
好的,我发现秒数可能不会超过2592000(30天)。所以最长有效期是30天。
【讨论】:
看起来有些答案不再有效。
我发现当 TTL 太高时根本没有设置密钥。例如2992553564。
使用以下 PHP 代码测试:
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"
版本是memcached 1.4.14-0ubuntu9。
【讨论】:
在 laravel config.session.lifetime 设置上,如果设置为相当于上述 30 天,将被视为时间戳(这将在每次假设使用 memcached 时给出 token mismatch 的错误)。
为了回答,memcached 的过期时间可以随时设置。 (Laravel 的默认设置(在 v5.0 上)会将您设置为已经过期的时间戳)。如果您没有设置它,将使用默认值。
【讨论】: