|
| 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] |
0 commit comments