Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add client_class option #3

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ authors = [
]
dependencies = [
"Flask",
"pymemcache>=1.3.4",
"pymemcache>=3.4.4",
]
dev-dependencies = [
"pytest",
Expand Down
21 changes: 9 additions & 12 deletions src/flask_pymemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@
::

memcache.client.set('foo', 'bar')

"""

from __future__ import annotations

import flask
import pymemcache.client
import pymemcache.client.hash
import pymemcache


class FlaskPyMemcache:
def __init__(self, app=None, conf_key=None):
def __init__(self, app=None, conf_key=None, client_class=None) -> None:
"""
:type app: flask.Flask
:parm str conf_key: Key of flask config.
"""
self.conf_key = conf_key
if app is not None:
self.init_app(app, conf_key)
self.init_app(app, conf_key, client_class)

def init_app(self, app, conf_key=None):
def init_app(self, app, conf_key=None, client_class=None) -> None:
"""
:type app: flask.Flask
:parm str conf_key: Key of flask config.
Expand All @@ -84,9 +84,9 @@ def init_app(self, app, conf_key=None):

if isinstance(conf["server"], list):
conf["servers"] = conf.pop("server")
client = pymemcache.client.hash.HashClient(**conf)
client = (client_class or pymemcache.HashClient)(**conf)
elif isinstance(conf["server"], tuple):
client = pymemcache.client.Client(**conf)
client = (client_class or pymemcache.Client)(**conf)
else:
raise TypeError(
"Flask-PyMemcache conf['server'] should be tuple or list of tuples"
Expand All @@ -102,8 +102,5 @@ def close_connection(exc=None):
client.close()

@property
def client(self):
"""
:rtype: pymemcache.client.Client
"""
def client(self) -> pymemcache.Client:
return flask.current_app.extensions["pymemcache"][self]
14 changes: 14 additions & 0 deletions tests/test_flask_pymemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ def test_simple(self):
assert memcache.client.get(b"foo") == b"bar"

assert pymc.get(b"pxfoo") == b"bar"

def test_pool(self):
memcache = flask_pymemcache.FlaskPyMemcache()
app = flask.Flask(__name__)
app.config["PYMEMCACHE"] = {
"server": ("localhost", 11211),
"key_prefix": b"px",
"close_on_teardown": False,
}
memcache.init_app(app, client_class=pymemcache.PooledClient)

with app.app_context():
assert memcache.client.get(b"foo") is None
assert isinstance(memcache.client, pymemcache.PooledClient)