Skip to content

Commit

Permalink
Explicit support for certain memcache client libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Lxstr committed Jan 15, 2024
1 parent 6833806 commit 191aa27
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Released 2023-00-00
- Permanent session otherwise empty will not be saved.
- Use `secrets` module to generate session identifiers, with 256 bits of
entropy (was previously 122).
- Explicitly name support for python-memcached, pylibmc and pymemcache.
- Introduce SESSION_KEY_LENGTH to control the length of the session key in bytes, default is 32.
- Fix expiry is None bug in SQLAlchemy.
- Drop support for Redis < 2.6.12.
Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Relevant configuration values:
:class:`MemcachedSessionInterface`
----------------------------------

Uses the Memcached as a session backend. (`pylibmc`_ or `memcache`_ required)
Uses the Memcached as a session backend. (`pylibmc`_ or `python-memcached`_ or `pymemcache` required)

- SESSION_MEMCACHED

Expand Down
2 changes: 1 addition & 1 deletion requirements/pytest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pytest-cov

# Requirements for interfaces
redis
python3-memcached
python-memcached
Flask-SQLAlchemy
pymongo

29 changes: 14 additions & 15 deletions src/flask_session/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,25 @@ class MemcachedSessionInterface(ServerSideSessionInterface):
def __init__(self, client, key_prefix, use_signer, permanent, sid_length):
if client is None:
client = self._get_preferred_memcache_client()
if client is None:
raise RuntimeError("no memcache module found")
self.client = client
super().__init__(client, key_prefix, use_signer, permanent, sid_length)

def _get_preferred_memcache_client(self):
servers = ["127.0.0.1:11211"]
try:
import pylibmc
except ImportError:
pass
else:
return pylibmc.Client(servers)
clients = [
("pylibmc", ["127.0.0.1:11211"]),
("memcache", ["127.0.0.1:11211"]),
("pymemcache.client.base", "127.0.0.1:11211"),
]

try:
import memcache
except ImportError:
pass
else:
return memcache.Client(servers)
for module_name, server in clients:
try:
module = __import__(module_name)
ClientClass = getattr(module, "Client")
return ClientClass(server)
except ImportError:
continue

raise ImportError("No memcache module found")

def _get_memcache_timeout(self, timeout):
"""
Expand Down

0 comments on commit 191aa27

Please sign in to comment.