These methods operate on 64-bit integer counters. They provide
efficient mutation and retrieval of values. You can use these in
place of the
set family of methods when
working with numeric values. For example:
key = "counter" try: result = c.get("counter") c.set(key, result.value + 1) except KeyNotFoundError: c.add(key, 10)
key = "counter" c.incr(key, initial=10)
These methods accept the
ttl argument to set
the expiration time for their values, as well as an
amount value that indicates by what amount to
modify their values. Additionally, an
keyword argument is available to provide the default value for
the counter if it does not yet exist. If an
initial argument is not provided and the key
does not exist, an exception is raised.
The value for the counter stored must either not exist (if
initial is used) or should be a "Number,"
that is, a textual representation of an integer.
If using the default
FMT_JSON, then your
integers are already compliant.
If the existing value is not already a number, the server raises
an exception (specifically, a
Arithmetic methods return a
object (subclass of
value property can be used to obtain the
current value of the counter.
c.incr(key, amount=1, ttl=0)
Increments the value stored under the key.
c.decr(key, amount=1, ttl=0)
Decrements the value stored under the key. In this case,
amount is how much to subtract from the