1.ubuntu16.04 install redis
# ubuntu 16.04 之後推薦使用 apt
sudo apt update
sudo apt install build-essential tcl
sudo apt install redis-server
# 安裝 stable 版本
[參考](https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04)
2.flask-cache簡單應用
from flask import Flask
from flask_cache import Cache
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
cache = Cache(app, config={'CACHE_TYPE': 'redis',
'CACHE_REDIS_HOST': '127.0.0.1',
'CACHE_REDIS_PORT': 6379,
'CACHE_REDIS_PASSWORD': '0000',
'CACHE_REDIS_DB': 2}
@app.route('/hello')
@cache.cached(timeout=300, key_prefix='view_%s', unless=None)
def hello():
print('view hello called')
return render_template('hello.html')
@cache.cached(timeout=50, key_prefix='get_list')
def get_list():
print('get_list')
return ['a','b','c','d','e']
3.memoize
@cache.memoize(timeout=50)
def create_list(num):
print('method create_list called')
l = []
for i in range(num):
l.append(str(i))
return l
@app.route('/list/<int:num>')
def list(num):
return ', '.join(create_list(num))
4.清除 cache
cache.delete('get_list')
cache.delete_many('get_list', 'view_hello')
cache.delete_memoized('create_list', 5)
cache.clear()