Skip to content

Commit a16f12e

Browse files
committed
Initial commit
0 parents  commit a16f12e

File tree

6 files changed

+208
-0
lines changed

6 files changed

+208
-0
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include README.rst

README.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
pymemcache_ integration for Flask
2+
=================================
3+
4+
.. _pymemcache: https://github.com/pinterest/pymemcache
5+
6+
Initialize
7+
----------
8+
9+
::
10+
11+
memcache = FlaskPyMemcache(app)
12+
13+
or::
14+
15+
memcache = FlaskPyMemcache()
16+
memcache.init_app(app)
17+
18+
19+
Configuration
20+
-------------
21+
22+
Put kwargs for pymemcache to `PYMEMCACHE` in your Flask configuration.
23+
24+
::
25+
26+
PYMEMCACHE = {
27+
'server': ('localhost', 11211),
28+
'connect_timeout': 1.0,
29+
'timeout': 0.5,
30+
'no_delay': True,
31+
}
32+
33+
You can use different config key with `conf_key` keyword::
34+
35+
session = FlaskPyMemcache(conf_key='MEMCACHE_SESSION')
36+
cache = FlaskPyMemcache(conf_key='MEMCACHE_CACHE')
37+
38+
session.init_app(app)
39+
cache.init_app(app)
40+
41+
In addition to normal pymemcache kwargs, Flask-PyMemcache provides following
42+
configuration options.
43+
44+
* `prefix` -- Add prefix to all key. (Default: b'')
45+
* `close_on_teardown` -- Close connection to memcached when app teardown.
46+
47+
Use
48+
---
49+
50+
::
51+
52+
memcache.client.set('foo', 'bar')
53+
54+

flask_pymemcache.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
pymemcache_ integration for Flask
4+
=================================
5+
6+
.. _pymemcache: https://github.com/pinterest/pymemcache
7+
8+
Initialize
9+
----------
10+
11+
::
12+
13+
memcache = FlaskPyMemcache(app)
14+
15+
or::
16+
17+
memcache = FlaskPyMemcache()
18+
memcache.init_app(app)
19+
20+
21+
Configuration
22+
-------------
23+
24+
Put kwargs for pymemcache to `PYMEMCACHE` in your Flask configuration.
25+
26+
::
27+
28+
PYMEMCACHE = {
29+
'server': ('localhost', 11211),
30+
'connect_timeout': 1.0,
31+
'timeout': 0.5,
32+
'no_delay': True,
33+
}
34+
35+
You can use different config key with `conf_key` keyword::
36+
37+
session = FlaskPyMemcache(conf_key='MEMCACHE_SESSION')
38+
cache = FlaskPyMemcache(conf_key='MEMCACHE_CACHE')
39+
40+
session.init_app(app)
41+
cache.init_app(app)
42+
43+
In addition to normal pymemcache kwargs, Flask-PyMemcache provides following
44+
configuration options.
45+
46+
* `prefix` -- Add prefix to all key. (Default: b'')
47+
* `close_on_teardown` -- Close connection to memcached when app teardown.
48+
49+
Use
50+
---
51+
52+
::
53+
54+
memcache.client.set('foo', 'bar')
55+
56+
"""
57+
from __future__ import absolute_import, division, print_function
58+
import flask
59+
import pymemcache
60+
61+
62+
class FlaskMemcacheClient(pymemcache.client.Client):
63+
"""PyMemcache client supporting prefix"""
64+
def __init__(self, prefix=b'', **kwargs):
65+
if not isinstance(prefix, bytes):
66+
prefix = prefix.encode('ascii')
67+
self.prefix = prefix
68+
super(FlaskMemcacheClient, self).__init__(**kwargs)
69+
70+
def check_key(self, key):
71+
return super(FlaskMemcacheClient, self).check_key(self.prefix + key)
72+
73+
74+
class FlaskPyMemcache(object):
75+
#: :type: memcache.Client
76+
client = None
77+
78+
def __init__(self, app=None, conf_key=None):
79+
"""
80+
:type app: flask.Flask
81+
:parm str conf_key: Key of flask config.
82+
"""
83+
self.conf_key = conf_key
84+
if app is not None:
85+
self.init_app(app, conf_key)
86+
87+
def init_app(self, app, conf_key=None):
88+
"""
89+
:type app: flask.Flask
90+
:parm str conf_key: Key of flask config.
91+
"""
92+
conf_key = conf_key or self.conf_key or 'MEMCACHE'
93+
self.conf_key = conf_key
94+
conf = app.config[conf_key]
95+
if not isinstance(conf, dict):
96+
raise TypeError("Flask-PyMemcache conf should be dict")
97+
98+
close_on_teardown = conf.pop('close_on_teardown', False)
99+
client = FlaskPyMemcache(**conf)
100+
app.extensions[self] = client
101+
102+
if close_on_teardown:
103+
@app.teardown_appcontext
104+
def close_connection(exc=None):
105+
client.disconnect_all()
106+
107+
@property
108+
def client(self):
109+
"""
110+
:rtype: pymemcache.client.Client
111+
"""
112+
return current_app.extensions[self]

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[wheel]
2+
universal=1

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import, division, print_function
3+
4+
from setuptools import setup
5+
6+
try:
7+
with open('README.rst') as f:
8+
readme = f.read()
9+
except IOError:
10+
readme = ''
11+
12+
setup(
13+
name="Flask-PyMemcache",
14+
description="pymemcache integration for Flask",
15+
long_description=readme,
16+
install_requires=["Flask", "pymemcache>=1.2.1"],
17+
)

test_flask_pymemcache.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import, division, print_function
3+
4+
import pymemcache.client
5+
import flask
6+
import flask.ext.pymemcache
7+
8+
9+
def test_simple():
10+
pymc = pymemcache.client.Client(('localhost', 11211))
11+
memcache = flask.ext.pymemcache.FlaskPyMemcache()
12+
app = flask.Flask(__name__)
13+
app.config['PYMEMCACHE'] = {
14+
'server': ('localhost', 11211),
15+
'prefix': b'px'}
16+
memcache.init_app(app)
17+
18+
with app.app_context():
19+
memcache.client.set(b'foo', b'bar')
20+
assert memcache.client.get(b'foo') == b'bar'
21+
22+
assert pymc.get(b'pxfoo') == b'bar'

0 commit comments

Comments
 (0)