pymemcache.client.base module

class pymemcache.client.base.Client(server, serde=None, serializer=None, deserializer=None, connect_timeout=None, timeout=None, no_delay=False, ignore_exc=False, socket_module=<module 'socket' from '/home/docs/.pyenv/versions/3.7.3/lib/python3.7/socket.py'>, key_prefix=b'', default_noreply=True, allow_unicode_keys=False, encoding='ascii', tls_context=None)

Bases: object

A client for a single memcached server.

Server Connection

The server parameter controls how the client connects to the memcached server. You can either use a (host, port) tuple for a TCP connection or a string containing the path to a UNIX domain socket.

The connect_timeout and timeout parameters can be used to set socket timeout values. By default, timeouts are disabled.

When the no_delay flag is set, the TCP_NODELAY socket option will also be set. This only applies to TCP-based connections.

Lastly, the socket_module allows you to specify an alternate socket implementation (such as gevent.socket).

Keys and Values

Keys must have a __str__() method which should return a str with no more than 250 ASCII characters and no whitespace or control characters. Unicode strings must be encoded (as UTF-8, for example) unless they consist only of ASCII characters that are neither whitespace nor control characters.

Values must have a __str__() method to convert themselves to a byte string. Unicode objects can be a problem since str() on a Unicode object will attempt to encode it as ASCII (which will fail if the value contains code points larger than U+127). You can fix this with a serializer or by just calling encode on the string (using UTF-8, for instance).

If you intend to use anything but str as a value, it is a good idea to use a serializer. The pymemcache.serde library has an already implemented serializer which pickles and unpickles data.

Serialization and Deserialization

The constructor takes an optional object, the “serializer/deserializer” (“serde”), which is responsible for both serialization and deserialization of objects. That object must satisfy the serializer interface by providing two methods: serialize and deserialize. serialize takes two arguments, a key and a value, and returns a tuple of two elements, the serialized value, and an integer in the range 0-65535 (the “flags”). deserialize takes three parameters, a key, value, and flags, and returns the deserialized value.

Here is an example using JSON for non-str values:

class JSONSerde(object):
    def serialize(self, key, value):
        if isinstance(value, str):
            return value, 1
        return json.dumps(value), 2

    def deserialize(self, key, value, flags):
        if flags == 1:
            return value

        if flags == 2:
            return json.loads(value)

        raise Exception("Unknown flags for value: {1}".format(flags))

Note

Most write operations allow the caller to provide a flags value to support advanced interaction with the server. This will override the “flags” value returned by the serializer and should therefore only be used when you have a complete understanding of how the value should be serialized, stored, and deserialized.

Error Handling

All of the methods in this class that talk to memcached can throw one of the following exceptions:

Instances of this class maintain a persistent connection to memcached which is terminated when any of these exceptions are raised. The next call to a method on the object will result in a new connection being made to memcached.

add(key, value, expire=0, noreply=None, flags=None)

The memcached “add” command.

Parameters
  • key – str, see class docs for details.

  • value – str, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

If noreply is True (or if it is unset and self.default_noreply is True), the return value is always True. Otherwise the return value is True if the value was stored, and False if it was not (because the key already existed).

append(key, value, expire=0, noreply=None, flags=None)

The memcached “append” command.

Parameters
  • key – str, see class docs for details.

  • value – str, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

True.

cache_memlimit(memlimit)

The memcached “cache_memlimit” command.

Parameters

memlimit – int, the number of megabytes to set as the new cache memory limit.

Returns

If no exception is raised, always returns True.

cas(key, value, cas, expire=0, noreply=False, flags=None)

The memcached “cas” command.

Parameters
  • key – str, see class docs for details.

  • value – str, see class docs for details.

  • cas – int or str that only contains the characters ‘0’-‘9’.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, False to wait for the reply (the default).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

If noreply is True (or if it is unset and self.default_noreply is True), the return value is always True. Otherwise returns None if the key didn’t exist, False if it existed but had a different cas value and True if it existed and was changed.

check_key(key)

Checks key and add key_prefix.

close()

Close the connection to memcached, if it is open. The next call to a method that requires a connection will re-open it.

decr(key, value, noreply=False)

The memcached “decr” command.

Parameters
  • key – str, see class docs for details.

  • value – int, the amount by which to decrement the value.

  • noreply – optional bool, False to wait for the reply (the default).

Returns

If noreply is True, always returns None. Otherwise returns the new value of the key, or None if the key wasn’t found.

delete(key, noreply=None)

The memcached “delete” command.

Parameters
  • key – str, see class docs for details.

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

Returns

If noreply is True (or if it is unset and self.default_noreply is True), the return value is always True. Otherwise returns True if the key was deleted, and False if it wasn’t found.

delete_many(keys, noreply=None)

A convenience function to delete multiple keys.

Parameters
  • keys – list(str), the list of keys to delete.

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

Returns

True. If an exception is raised then all, some or none of the keys may have been deleted. Otherwise all the keys have been sent to memcache for deletion and if noreply is False, they have been acknowledged by memcache.

delete_multi(keys, noreply=None)

A convenience function to delete multiple keys.

Parameters
  • keys – list(str), the list of keys to delete.

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

Returns

True. If an exception is raised then all, some or none of the keys may have been deleted. Otherwise all the keys have been sent to memcache for deletion and if noreply is False, they have been acknowledged by memcache.

disconnect_all()

Close the connection to memcached, if it is open. The next call to a method that requires a connection will re-open it.

flush_all(delay=0, noreply=None)

The memcached “flush_all” command.

Parameters
  • delay – optional int, the number of seconds to wait before flushing, or zero to flush immediately (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

Returns

True.

get(key, default=None)

The memcached “get” command, but only for one key, as a convenience.

Parameters
  • key – str, see class docs for details.

  • default – value that will be returned if the key was not found.

Returns

The value for the key, or default if the key wasn’t found.

get_many(keys)

The memcached “get” command.

Parameters

keys – list(str), see class docs for details.

Returns

A dict in which the keys are elements of the “keys” argument list and the values are values from the cache. The dict may contain all, some or none of the given keys.

get_multi(keys)

The memcached “get” command.

Parameters

keys – list(str), see class docs for details.

Returns

A dict in which the keys are elements of the “keys” argument list and the values are values from the cache. The dict may contain all, some or none of the given keys.

gets(key, default=None, cas_default=None)

The memcached “gets” command for one key, as a convenience.

Parameters
  • key – str, see class docs for details.

  • default – value that will be returned if the key was not found.

  • cas_default – same behaviour as default argument.

Returns

A tuple of (value, cas) or (default, cas_defaults) if the key was not found.

gets_many(keys)

The memcached “gets” command.

Parameters

keys – list(str), see class docs for details.

Returns

A dict in which the keys are elements of the “keys” argument list and the values are tuples of (value, cas) from the cache. The dict may contain all, some or none of the given keys.

incr(key, value, noreply=False)

The memcached “incr” command.

Parameters
  • key – str, see class docs for details.

  • value – int, the amount by which to increment the value.

  • noreply – optional bool, False to wait for the reply (the default).

Returns

If noreply is True, always returns None. Otherwise returns the new value of the key, or None if the key wasn’t found.

prepend(key, value, expire=0, noreply=None, flags=None)

The memcached “prepend” command.

Parameters
  • key – str, see class docs for details.

  • value – str, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

True.

quit()

The memcached “quit” command.

This will close the connection with memcached. Calling any other method on this object will re-open the connection, so this object can be re-used after quit.

replace(key, value, expire=0, noreply=None, flags=None)

The memcached “replace” command.

Parameters
  • key – str, see class docs for details.

  • value – str, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

If noreply is True (or if it is unset and self.default_noreply is True), the return value is always True. Otherwise returns True if the value was stored and False if it wasn’t (because the key didn’t already exist).

set(key, value, expire=0, noreply=None, flags=None)

The memcached “set” command.

Parameters
  • key – str, see class docs for details.

  • value – str, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

If no exception is raised, always returns True. If an exception is raised, the set may or may not have occurred. If noreply is True, then a successful return does not guarantee a successful set.

set_many(values, expire=0, noreply=None, flags=None)

A convenience function for setting multiple values.

Parameters
  • values – dict(str, str), a dict of keys and values, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

Returns a list of keys that failed to be inserted. If noreply is True, always returns empty list.

set_multi(values, expire=0, noreply=None, flags=None)

A convenience function for setting multiple values.

Parameters
  • values – dict(str, str), a dict of keys and values, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

  • flags – optional int, arbitrary bit field used for server-specific flags

Returns

Returns a list of keys that failed to be inserted. If noreply is True, always returns empty list.

stats(*args)

The memcached “stats” command.

The returned keys depend on what the “stats” command returns. A best effort is made to convert values to appropriate Python types, defaulting to strings when a conversion cannot be made.

Parameters

*arg – extra string arguments to the “stats” command. See the memcached protocol documentation for more information.

Returns

A dict of the returned stats.

touch(key, expire=0, noreply=None)

The memcached “touch” command.

Parameters
  • key – str, see class docs for details.

  • expire – optional int, number of seconds until the item is expired from the cache, or zero for no expiry (the default).

  • noreply – optional bool, True to not wait for the reply (defaults to self.default_noreply).

Returns

True if the expiration time was updated, False if the key wasn’t found.

version()

The memcached “version” command.

Returns

A string of the memcached version.

class pymemcache.client.base.PooledClient(server, serde=None, serializer=None, deserializer=None, connect_timeout=None, timeout=None, no_delay=False, ignore_exc=False, socket_module=<module 'socket' from '/home/docs/.pyenv/versions/3.7.3/lib/python3.7/socket.py'>, key_prefix=b'', max_pool_size=None, lock_generator=None, default_noreply=True, allow_unicode_keys=False, encoding='ascii', tls_context=None)

Bases: object

A thread-safe pool of clients (with the same client api).

Parameters
  • max_pool_size – maximum pool size to use (going above this amount triggers a runtime error), by default this is 2147483648L when not provided (or none).

  • lock_generator – a callback/type that takes no arguments that will be called to create a lock or semaphore that can protect the pool from concurrent access (for example a eventlet lock or semaphore could be used instead)

Further arguments are interpreted as for Client constructor.

Note: if serde is given, the same object will be used for all clients in the pool. Your serde object must therefore be thread-safe.

add(key, value, expire=0, noreply=None, flags=None)
append(key, value, expire=0, noreply=None, flags=None)
cas(key, value, cas, expire=0, noreply=False, flags=None)
check_key(key)

Checks key and add key_prefix.

client_class

Client class used to create new clients

alias of Client

close()
decr(key, value, noreply=False)
delete(key, noreply=None)
delete_many(keys, noreply=None)
delete_multi(keys, noreply=None)
disconnect_all()
flush_all(delay=0, noreply=None)
get(key, default=None)
get_many(keys)
get_multi(keys)
gets(key)
gets_many(keys)
incr(key, value, noreply=False)
prepend(key, value, expire=0, noreply=None, flags=None)
quit()
replace(key, value, expire=0, noreply=None, flags=None)
set(key, value, expire=0, noreply=None, flags=None)
set_many(values, expire=0, noreply=None, flags=None)
set_multi(values, expire=0, noreply=None, flags=None)
stats(*args)
touch(key, expire=0, noreply=None)
version()
pymemcache.client.base.check_key_helper(key, allow_unicode_keys, key_prefix=b'')

Checks key and add key_prefix.

pymemcache.client.base.normalize_server_spec(server)