14

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 different results.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
  • From [this question](http://programmingbulls.com/php-memcache-key-expire) I am guessing "Yes". But this is not authoritative nor do I use memcached. Happy coding. –  May 20 '11 at 04:48

5 Answers5

10

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.

Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
5

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);
nateevans
  • 426
  • 4
  • 8
  • The ascii protocol also supports touch with more recent versions of php-memcached and memcached. You aren't limited to just the binary protocol anymore. – Bjorn Sep 01 '16 at 14:12
0

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.

Cachelot
  • 106
  • 1
  • 5
0

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?

eddy85br
  • 284
  • 4
  • 12
0

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.

Dustin
  • 89,080
  • 21
  • 111
  • 133
  • ...assuming the value is only evicted upon expiration (imagine this is strictly enforced for sake of question) and always returned otherwise... –  May 20 '11 at 04:39
  • I'm not sure I understand... You are saying that the expiration gets renewed on that Set? – Daniel Magliola May 20 '11 at 08:42
  • I'm saying it's an entirely different item. There's no relationship between what was there and what you set. It's not renewing it, it's creating something new altogether. – Dustin May 22 '11 at 23:52