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)[[email protected] 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)[[email protected] simple_flaskcache]$
測試執行過程:
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/2
2 - Tue Aug 5 14:01:49 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/hello/1
1 - Tue Aug 5 14:01:54 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/1
1 - Tue Aug 5 14:02:02 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/1
1 - Tue Aug 5 14:02:02 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/1
1 - Tue Aug 5 14:02:02 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/2
2 - Tue Aug 5 14:02:05 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/2
2 - Tue Aug 5 14:02:05 2014
(flask_project)[[email protected] ~]$ curl http://10.210.71.145:5000/2
2 - Tue Aug 5 14:02:05 2014
(flask_project)[[email protected] ~]$
對比分析可見,對于帶參數的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)[[email protected] 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效果一至的