سؤال

can anybody tell me (or point to some documentation that documents) how memcached counters work? Specifically: how do they expire? I'm using the java spymemcached client.

The method

net.spy.memcached.MemcachedClient.incr(String key, int by, long def, int exp) 

takes a parameter "exp" described as "the expiration of this object"

Does that mean each individual call to the counter has its own expiry time? Or does calling incr bump the expiry time for the key?

Also, shouldn't there be a method to get a counter value? Or should I call incr with an increment of 0?

هل كانت مفيدة؟

المحلول

The int exp will overwrite the existing expiration time, and "bump" the key your incrementing. Check out the protocol docs for additional info.

Also, shouldn't there be a method to get a counter value? Or should I call incr with an increment of 0?

You can get the value of the counter cell by using MemcachedClient.get method. The counter cell will need to be set from MemcachedClient.set before you are able to increment it. I'm not 100% sure about , but some libraries will auto set the counter cell to 0 -- before incrementation.

A note on expiration time. If the int expr is -1 memcached will use the previous expiration time. If int expr is greater than 2592000 (30 days), it will be treated as an UNIX timestamp.

نصائح أخرى

From the comments in the source code :

get the expiration to set in case of a new entry.

So, I suspect that both the flags passed in def and the expr time are used only in case that the key is missing.

I find the code hard to follow, as I am not a Java programmer, but this foo seems to perform something like: if incr(key,value) fails, try to perform add(key,value,def,expr) and if this fails as well, then try incr(key,value) again.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top