开发者

Does setting a memcached key that already exists refresh the expiration time?

开发者 https://www.devze.com 2023-03-07 16:45 出处:网络
Let\'s say I have the following code: Memcached->set(\'key\', \'value\', 60); (expire in one minute)

Let's say I have the following code:

Memcached->set('key', 'value', 60); (expire in one minute)

while (1) {
    sleep 1 second;
    data = Memcached->get('key');
    // update data
    Memcached->set('key', data, 60);
}

After 60 iterations of the loop, will the key expire and when reading it I'll get a NULL? Or will the continuous setting keep pushing the expiration time each time to 1 minute after the last Set?

The documentation mentions this, and I've tested this in different contexts and I'm pretty sure I got differe开发者_如何学编程nt results.


Ok, found my answer by experimentation in the end...

It turns out "Set" does extend the expiration, it's basically the same as deleting the item and Setting it again with a new expiration.

However, Increment doesn't extend the expiration. If you increment a key, it keeps the original expiration time it had when you Set it in the first place.


If you simply want to extend the expiration time for a particular key instead of essentially resetting the data each time, you can just use Memcached::touch

With the caveat that you must have binary protocol enabled according to the comment on the above page.

$memcached = new Memcached();
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->touch('key', 120);


The set doesn't care whatsoever about what may have been there, and can't assume that it even came from the same application.

What all did you test and what kinds of results did you get? Memcached never guarantees to return a value, so if you saw it go missing, it's quite possible to construct a test that would lose that regardless of expiration.


The best documentation source is the Memcached protocol description

First, the client sends a command line which looks like this:

<command name> <key> <flags> <exptime> <bytes> [noreply]\r\n

- <command name> is "set", "add", "replace", "append" or "prepend"

As you may see, each of the commands above have the exptime field which is mandatory.

So, yes - it will update expiration time. Moreover, memcached creates new item with its own key / flags / expiration / value and replaces the old one with it.


If your goal is to simply extends the expiration time, use the command touch, that was created to set a new expiration time for a key.

See https://manned.org/memctouch or http://docs.libmemcached.org/bin/memtouch.html

Debian package: libmemcached-tools

From the shell: man memctouch

other distros use "memtouch" as the name of the command line tool

+1 Link from memcached protocol, as a manual reference: https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L318

Example:

memctouch --servers=localhost --expire=60 $key

Where $key is your 'key', this will set the expire time to 60 seconds, as in your example, but without having to make a "get" AND re-set the key. What if your 'key' is not set yet and your 'get' doesn't return some data?

0

精彩评论

暂无评论...
验证码 换一张
取 消