SessionMemcachedStore

Session store using the Memcached memory object caching system.

class SessionMemcachedStore.SessionMemcachedStore(app)

Bases: SessionStore

A session store using Memcached.

Stores the sessions in a single Memcached store using ‘last write wins’ semantics. This increases fault tolerance and allows server clustering. In clustering configurations with concurrent writes for the same session(s) the last writer will always overwrite the session.

The keys are prefixed with a configurable namespace, allowing you to store other data in the same Memcached system.

Cleaning/timing out of sessions is performed by Memcached itself since no single application can know about the existence of all sessions or the last access for a given session. Besides it is built in Memcached functionality. Consequently, correct sizing of Memcached is necessary to hold all user’s session data.

Due to the way Memcached works, methods requiring access to the keys or for clearing the store do not work. You can configure whether you want to ignore such calls or raise an error in this case. By default, you will get a warning. It would be possible to emulate these functions by storing additional data in the memcache, such as a namespace counter or the number or even the full list of keys. However, if you are using more than one application instance, this would require fetching that data every time, since we cannot know whether another instance changed it. So we refrained from doing such sophisticated trickery and instead kept the implementation intentionally very simple and fast.

You need to install python-memcached to be able to use this module: https://www.tummy.com/software/python-memcached/ You also need a Memcached server: https://memcached.org/

Contributed by Steve Schwarz, March 2010. Small improvements by Christoph Zwerschke, April 2010.

__init__(app)

Initialize the session store.

Subclasses must invoke super.

application()

Return the application owning the session store.

cleanStaleSessions(_task=None)

Clean stale sessions.

Memcached does this on its own, so we do nothing here.

clear()

Clear the session store, removing all of its items.

Not supported by Memcached. We could emulate this by incrementing an additional namespace counter, but then we would need to fetch the current counter from the memcache before every access in order to keep different application instances in sync.

decoder()

Return the value deserializer for the store.

encoder()

Return the value serializer for the store.

get(key, default=None)

Return value if key available, else return the default.

has_key(key)

Check whether the session store has a given key.

items()

Return a list with the (key, value) pairs for all sessions.

iteritems()

Return an iterator over the (key, value) pairs for all sessions.

iterkeys()

Return an iterator over the stored session keys.

itervalues()

Return an iterator over the stored values of all sessions.

keys()

Return a list with the keys of all the stored sessions.

Not supported by Memcached (see FAQ for explanation).

mcKey(key)

Create the real key with namespace to be used with Memcached.

pop(key, default=<class 'MiscUtils.NoDefault'>)

Return value if key available, else default (also remove key).

setEncoderDecoder(encoder, decoder)

Set the serializer and deserializer for the store.

setdefault(key, default=None)

Return value if key available, else default (also setting it).

storeAllSessions()

Permanently save all sessions in the store.

Should be used (only) when the application server is shut down. This closes the connection to the Memcached servers.

storeSession(session)

Save potentially changed session in the store.

values()

Return a list with the values of all stored sessions.