3030 'connect_timeout': 1.0,
3131 'timeout': 0.5,
3232 'no_delay': True,
33+ 'key_prefix': b'myapp-',
3334 }
3435
3536You can use different config key with `conf_key` keyword::
4344In addition to normal pymemcache kwargs, Flask-PyMemcache provides following
4445configuration options.
4546
46- * `prefix` -- Add prefix to all key. (Default: b'')
4747* `close_on_teardown` -- Close connection to memcached when app teardown.
4848
4949Use
5959import pymemcache .client
6060
6161
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-
7462class FlaskPyMemcache (object ):
7563
7664 def __init__ (self , app = None , conf_key = None ):
@@ -94,8 +82,9 @@ def init_app(self, app, conf_key=None):
9482 raise TypeError ("Flask-PyMemcache conf should be dict" )
9583
9684 close_on_teardown = conf .pop ('close_on_teardown' , False )
97- client = FlaskMemcacheClient (** conf )
98- app .extensions [self ] = client
85+ client = pymemcache .client .Client (** conf )
86+ app .extensions .setdefault ('pymemcache' , {})
87+ app .extensions ['pymemcache' ][self ] = client
9988
10089 if close_on_teardown :
10190 @app .teardown_appcontext
@@ -107,4 +96,4 @@ def client(self):
10796 """
10897 :rtype: pymemcache.client.Client
10998 """
110- return flask .current_app .extensions [self ]
99+ return flask .current_app .extensions ['pymemcache' ][ self ]
0 commit comments