Skip to content

Commit

Permalink
rye fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Mar 8, 2024
1 parent 1a6902b commit 14b08ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
23 changes: 13 additions & 10 deletions flask_pymemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
memcache.client.set('foo', 'bar')
"""

from __future__ import absolute_import, division, print_function
import flask
import pymemcache.client
import pymemcache.client.hash


class FlaskPyMemcache(object):

def __init__(self, app=None, conf_key=None):
"""
:type app: flask.Flask
Expand All @@ -76,26 +76,29 @@ def init_app(self, app, conf_key=None):
:type app: flask.Flask
:parm str conf_key: Key of flask config.
"""
conf_key = conf_key or self.conf_key or 'PYMEMCACHE'
conf_key = conf_key or self.conf_key or "PYMEMCACHE"
self.conf_key = conf_key
conf = app.config[conf_key]
if not isinstance(conf, dict):
raise TypeError("Flask-PyMemcache conf should be dict")

close_on_teardown = conf.pop('close_on_teardown', False)
close_on_teardown = conf.pop("close_on_teardown", False)

if isinstance(conf['server'], list):
conf['servers'] = conf.pop('server')
if isinstance(conf["server"], list):
conf["servers"] = conf.pop("server")
client = pymemcache.client.hash.HashClient(**conf)
elif isinstance(conf['server'], tuple):
elif isinstance(conf["server"], tuple):
client = pymemcache.client.Client(**conf)
else:
raise TypeError("Flask-PyMemcache conf['server'] should be tuple or list of tuples")
raise TypeError(
"Flask-PyMemcache conf['server'] should be tuple or list of tuples"
)

app.extensions.setdefault('pymemcache', {})
app.extensions['pymemcache'][self] = client
app.extensions.setdefault("pymemcache", {})
app.extensions["pymemcache"][self] = client

if close_on_teardown:

@app.teardown_appcontext
def close_connection(exc=None):
client.close()
Expand All @@ -105,4 +108,4 @@ def client(self):
"""
:rtype: pymemcache.client.Client
"""
return flask.current_app.extensions['pymemcache'][self]
return flask.current_app.extensions["pymemcache"][self]
17 changes: 9 additions & 8 deletions test_flask_pymemcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@

class TestFlaskPyMemcache(TestCase):
def test_simple(self):
pymc = pymemcache.client.Client(('localhost', 11211))
pymc = pymemcache.client.Client(("localhost", 11211))
memcache = flask_pymemcache.FlaskPyMemcache()
app = flask.Flask(__name__)
app.config['PYMEMCACHE'] = {
'server': ('localhost', 11211),
'key_prefix': b'px',
'close_on_teardown': False}
app.config["PYMEMCACHE"] = {
"server": ("localhost", 11211),
"key_prefix": b"px",
"close_on_teardown": False,
}
memcache.init_app(app)

with app.app_context():
memcache.client.set(b'foo', b'bar')
memcache.client.set(b"foo", b"bar")

with app.app_context():
assert memcache.client.get(b'foo') == b'bar'
assert memcache.client.get(b"foo") == b"bar"

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

0 comments on commit 14b08ef

Please sign in to comment.