略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 超时时间

2024-12-22

超时时间

一些存储命令在发送时会包含一个失效值(与一个元素或一个客户端操作请求相关)到服务端。所有这类用法,实际发送的值可以 是一个Unix时间戳(自1970年1月1日起至失效时间的整型秒数),或者是一个从现在算起的以秒为单位的数字。对于后一种情况,这个 秒数不能超过60×60×24×30(30天时间的秒数);如果失效的值大于这个值, 服务端会将其作为一个真实的Unix时间戳来处理而不是 自当前时间的偏移。

如果失效值被设置为0(默认),此元素永不过期(但是它可能由于服务端为了给其他新的元素分配空间而被删除)。

add a noteadd a note

User Contributed Notes 3 notes

up
6
valugi at gmail dot com
5 years ago
The fact that one sets an expiration time does not mean that the keys will expire at that particular time. I'm not sure what is happening in the background, if there is a process like a garbage collector that expire keys, but some function do not activate the expiration check and return the key as valid, for example `getAllKeys` is not atomic and returns even expired keys.

$memcached = new Memcached();
$memcached->set('key','value',10);
//waiting more than 10 sec
sleep(20);
$data = $memcached->getAllKeys();
var_dump($data); // key will still be listed
$key = $memcached->get('key'); // will trigger the expiration
up
-1
i dot caught dot air at gmail dot com
4 years ago
A TTL of n seconds will expire between n and n-1 seconds as memcache doesn't use a high-resolution clock internally.

This is important to consider if you're working with very short TTLs.

See https://github.com/memcached/memcached/issues/307
up
-61
info at tueena dot com
10 years ago
Note that if you pass the expiration time as an offset of seconds then the cache item will expire in current-second + offset, not in now + offset.

<?php
$Memcached
->add('foo', 42, 2);
?>

This item will expire in n seconds where n > 1 and <= 2, not in exactly 2 seconds.

官方地址:https://www.php.net/manual/en/memcached.expiration.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3