English | Site Directory

Functions

The google.appengine.api.memcache package provides the following functions:

set(key, value, time=0, min_compress_len=0)

Sets a key's value, regardless of previous contents in cache.

Arguments:

key
Key to set. The Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
value
Value to set.
time
Optional expiration time, either relative number of seconds from current time (up to 1 month), or an absolute Unix epoch time. By default, items never expire, though items may be evicted due to memory pressure. Float values will be rounded up to the nearest whole second.
min_compress_len
Ignored option for compatibility.
The return value is True if set, False on error.
set_multi(mapping, time=0, key_prefix='', min_compress_len=0)

Set multiple keys' values at once. Reduces the network latency of doing many requests in serial.

Arguments:

mapping
Dictionary of keys to values.
time
Optional expiration time, either relative number of seconds from current time (up to 1 month), or an absolute Unix epoch time. By default, items never expire, though items may be evicted due to memory pressure. Float values will be rounded up to the nearest whole second.
key_prefix
Prefix for to prepend to all keys.
min_compress_len
Ignored option for compatibility.
The return value is a list of keys whose values were NOT set. On total success, this list should be empty.
get(key)

Looks up a single key in memcache.

Arguments:

key
The key in memcache to look up. The Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
The return value is the value of the key, if found in memcache, else None.
get_multi(keys, key_prefix='')

Looks up multiple keys from memcache in one operation. This is the recommended way to do bulk loads.

Arguments:

keys
List of keys to look up. A Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
key_prefix
Prefix to prepend to all keys when talking to the server; not included in the returned dictionary.
The returned value is a dictionary of the keys and values that were present in memcache. Even if the key_prefix was specified, that key_prefix won't be on the keys in the returned dictionary.
delete(key, seconds=0)

Deletes a key from memcache.

Arguments:

key
Key to delete. A Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
seconds
Optional number of seconds to make deleted items 'locked' for 'add' operations. Value can be a delta from current time (up to 1 month), or an absolute Unix epoch time. Defaults to 0, which means items can be immediately added. With or without this option, a 'set' operation will always work. Float values will be rounded up to the nearest whole second.
The return value is 0 (DELETE_NETWORK_FAILURE) on network failure, 1 (DELETE_ITEM_MISSING) if the server tried to delete the item but didn't have it, and 2 (DELETE_SUCCESSFUL) if the item was actually deleted. This can be used as a boolean value, where a network failure is the only bad condition.
delete_multi(keys, seconds=0, key_prefix='')

Delete multiple keys at once.

Arguments:

keys
List of keys to delete. A Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
seconds
Optional number of seconds to make deleted items 'locked' for 'add' operations. Value can be a delta from current time (up to 1 month), or an absolute Unix epoch time. Defaults to 0, which means items can be immediately added. With or without this option, a 'set' operation will always work. Float values will be rounded up to the nearest whole second.
key_prefix
Prefix to put on all keys when sending specified keys to memcache. See docs for get_multi() and set_multi()
The return value is True if all operations completed successfully. False if one or more failed to complete.
add(key, value, time=0, min_compress_len=0)

Sets a key's value, if and only if the item is not already in memcache.

Arguments:

key
Key to set. The Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
value
Value to set.
time
Optional expiration time, either relative number of seconds from current time (up to 1 month), or an absolute Unix epoch time. By default, items never expire, though items may be evicted due to memory pressure. Float values will be rounded up to the nearest whole second.
min_compress_len
Ignored option for compatibility.
The return value is True if added, False on error.
replace(key, value, time=0, min_compress_len=0)

Replaces a key's value, failing if item isn't already in memcache.

Arguments:

key
Key to set. The Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
value
Value to set.
time
Optional expiration time, either relative number of seconds from current time (up to 1 month), or an absolute Unix epoch time. By default, items never expire, though items may be evicted due to memory pressure. Float values will be rounded up to the nearest whole second.
min_compress_len
Ignored option for compatibility.
The return value is True if replaced. False on error or cache miss.
incr(key, delta=1)

Atomically increments a key's value. Internally, the value is a unsigned 64-bit integer. Memcache doesn't check 64-bit overflows. The value, if too large, will wrap around.

The key must already exist in the cache to be incremented. To initialize a counter, set() it to the initial value, as an ASCII decimal integer. Future get()s of the key, post-increment, will still be an ASCII decimal value.

Arguments:

key
Key to increment. The Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
delta
Non-negative integer value (int or long) to increment key by, defaulting to 1.
The return value is a new long integer value, or None if key was not in the cache or could not be incremented for any other reason.
decr(key, delta=1)

Atomically decrements a key's value. Internally, the value is a unsigned 64-bit integer. Memcache doesn't check 64-bit overflows. The value, if too large, will wrap around.

The key must already exist in the cache to be decremented. To initialize a counter, set() it to the initial value, as an ASCII decimal integer. Future get()s of the key, post-increment, will still be an ASCII decimal value.

Arguments:

key
Key to decrement. The Key can be a string or a tuple of (hash_value, string) where the hash_value, normally used for sharding onto a memcache instance, is instead ignored, as Google App Engine deals with the sharding transparently.
delta
Non-negative integer value (int or long) to decrement key by, defaulting to 1.
The return value is a new long integer value, or None if key was not in the cache or could not be decremented for any other reason.
flush_all()

Deletes everything in memcache.

The return value is True on success, False on RPC or server error.
get_stats()

Gets memcache statistics for this application. All of these statistics may reset due to various transient conditions. They provide the best information available at the time of being called.

The return value is a dictionary mapping statistic names to associated values. Statistics and their associated meanings:
hits: Number of cache get requests resulting in a cache hit.
misses: Number of cache get requests resulting in a cache miss.
byte_hits: Sum of bytes transferred on get requests. Rolls over to zero on overflow.
items: Number of key/value pairs in the cache.
bytes: Total size of all items in the cache.
oldest_item_age: How long in seconds since the oldest item in the cache was accessed. Effectively, this indicates how long a new item will survive in the cache without being accessed. This is _not_ the amount of time that has elapsed since the item was created.