memjs icon indicating copy to clipboard operation
memjs copied to clipboard

Expiration value could indicate clearer the two different modes (interval vs timestamp)

Open adam-26 opened this issue 9 years ago • 3 comments

Hi, I was attempting to set the TTL value in milliseconds instead of seconds using the SET method. The value i was using for TTL was 1 hour. The SET command would complete without error, but any following GET requests could not find the data. After realizing the method expects TTL in seconds, everything worked as expected.

What is the maximum permitted TTL value? I know this is my own fault for not reading the documentation more closely, but it would be nice to receive an error message if the TTL value is invalid instead of silently failing.

Cheers, Adam.

adam-26 avatar Nov 20 '15 14:11 adam-26

So, memjs just passes along the value to memcache. There actually isn't a maximum TTL value in memcache. However, anything over 30 days (2592000 seconds) is treated as an absolute epoch time (seconds since Unix 1970). The documentation for Client#create describes this.

All ears if you think there is a good way to reflect this in the API, though!

alevy avatar Dec 01 '15 22:12 alevy

Could you inspect the TTL, and if over the 30 day limit, return an error if the epoch is in the past (before now)? If you are caching with a TTL, defining an expiry date in the past doesn't make a lot of sense.

adam-26 avatar Dec 02 '15 11:12 adam-26

That's a possibility. I see a couple issues with that:

  1. The semantics of the TTL are to use the clock of the server running memcache, while checking on the client-side would have to use the local clock, which may not be synchronized. So it's not unreasonable that by trying to validate the TTL on the client would result in an incorrect error.
  2. The behavior of memcache is not to return an error if the TTL is in the past. This could very well be behavior that a user exploits (for example, to elide a test for the validity of some value when setting it, and just letting memcache do that work).

This seems like an unfortunate choice of the memcache protocol, but a choice we need to live with. This is a command that accepts two types of parameters, that happen to have the same type, without anything in the protocol to differentiate what the caller means (and an arbitrary cutoff point).

I'd be open, instead to adding some type information to commands that accept a TTL to help a user be explicit about their choices. For example, I can imagine the TTL value taking an object that is either an Interval or a Timestamp and converting it to an integer for memcache accordingly. It'd just have to do this in a backwards compatible way...

alevy avatar Dec 02 '15 23:12 alevy