flask cache简单的使用有两个装饰器
1.memoize
@cache.memoize(timeout=50)
def big_foo(a, b):
return a + b + random.randrange(0, 1000)
2.cached
@cache.cached(timeout=50)
def index():
return render_template('index.html')
然后文档里面有句话是:
Note
With functions that do not receive arguments, cached() and memoize() are effectively the same.
对以上的测试代码:
(pythonenv)[xluren@test simple_flaskcache]$ cat hello.py
import time
from flask.ext.cache import Cache
from flask import Flask
app = Flask(__name__)
app.config['CACHE_TYPE'] = 'simple'
app.cache = Cache(app)
@app.cache.cached(timeout=50)
def get_current_time_and_name(name):
return "%s - %s" % (name, time.ctime())
@app.route("/<name>")
def view(name):
return get_current_time_and_name(name)
@app.cache.memoize(timeout=50)
def get_current_time_and_name_memoize(name):
return "%s - %s" % (name, time.ctime())
@app.route("/hello/<name>")
def memoize_view(name):
return get_current_time_and_name_memoize(name)
if __name__ == "__main__":
app.run(debug=True, port=5000, host='0.0.0.0')
(pythonenv)[xluren@test simple_flaskcache]$
测试执行过程:
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/1
1 - Tue Aug 5 14:02:02 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/1
1 - Tue Aug 5 14:02:02 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/1
1 - Tue Aug 5 14:02:02 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/2
2 - Tue Aug 5 14:02:05 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/2
2 - Tue Aug 5 14:02:05 2014
(flask_project)[baoning1@test ~]$ curl http://10.210.71.145:5000/2
2 - Tue Aug 5 14:02:05 2014
(flask_project)[baoning1@test ~]$
对比分析可见,对于带参数的cache,两者都会根据参数的不同进行cache的
其实cache类似 k-v ,key是用来区分cache的。
然后测试下产生的cache_key ,如果cache_key在两种装饰下的结果
234 cache_key = decorated_function.make_cache_key(*args, **kwargs)
235 print "#"*10
236 print "cached cache_key"
237 print cache_key
238 print "#"*10
239 rv = self.cache.get(cache_key)
还有一处
442 try:
443 cache_key = decorated_function.make_cache_key(f, *args, **kwargs)
444 print "#"*10
445 print "memoize cache_key"
446 print cache_key
447 print "#"*10
448 rv = self.cache.get(cache_key)
测试结果,访问方式按照上面的curl进行
(pythonenv)[xluren@test simple_flaskcache]$ python hello.py
* Running on http://0.0.0.0:5000/
* Restarting with reloader
##########
memoize cache_key
HCTGsWtt6wdG+jOES/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:49] "GET /hello/2 HTTP/1.1" 200 -
##########
memoize cache_key
HCTGsWtt6wdG+jOES/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:50] "GET /hello/2 HTTP/1.1" 200 -
##########
memoize cache_key
HCTGsWtt6wdG+jOES/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:51] "GET /hello/2 HTTP/1.1" 200 -
##########
memoize cache_key
HCTGsWtt6wdG+jOES/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:52] "GET /hello/2 HTTP/1.1" 200 -
##########
memoize cache_key
HCTGsWtt6wdG+jOES/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:53] "GET /hello/2 HTTP/1.1" 200 -
##########
memoize cache_key
MMfu3Hx4T5s5EUV9S/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:54] "GET /hello/1 HTTP/1.1" 200 -
##########
memoize cache_key
MMfu3Hx4T5s5EUV9S/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:55] "GET /hello/1 HTTP/1.1" 200 -
##########
memoize cache_key
MMfu3Hx4T5s5EUV9S/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:56] "GET /hello/1 HTTP/1.1" 200 -
##########
memoize cache_key
MMfu3Hx4T5s5EUV9S/SNUa
##########
10.210.71.145 - - [05/Aug/2014 14:01:57] "GET /hello/1 HTTP/1.1" 200 -
##########
cached cache_key
view//1
##########
10.210.71.145 - - [05/Aug/2014 14:02:02] "GET /1 HTTP/1.1" 200 -
##########
cached cache_key
view//1
##########
10.210.71.145 - - [05/Aug/2014 14:02:03] "GET /1 HTTP/1.1" 200 -
##########
cached cache_key
view//1
##########
10.210.71.145 - - [05/Aug/2014 14:02:03] "GET /1 HTTP/1.1" 200 -
##########
cached cache_key
view//2
##########
10.210.71.145 - - [05/Aug/2014 14:02:05] "GET /2 HTTP/1.1" 200 -
##########
cached cache_key
view//2
##########
10.210.71.145 - - [05/Aug/2014 14:02:06] "GET /2 HTTP/1.1" 200 -
##########
cached cache_key
view//2
##########
10.210.71.145 - - [05/Aug/2014 14:02:06] "GET /2 HTTP/1.1" 200 -
以上curl访问和print cache_key 两种方式都可以看出来,cache.cached() 和cache.memoize() 两种方式对于参数cache效果一至的