1. redis部署与设置
1.1
cd /home/user
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar zxvf redis-4.0.8.tar.gz
1.2
cd redis-4.0.8
make
cd src
make install PREFIX=/usr/local/redis [注意:PREFIX一定要大写]
1.3
cd ../
mkdir /usr/local/redis/etc
mv redis.conf /usr/local/redis/etc
cd /usr/local/redis/etc
vi redis.conf
daemonize no ==> daemonize yes
stop-writes-on-bgsave-error yes ==> stop-writes-on-bgsave-error no
bind 127.0.0.1 ==> bind 10.193.**.**
1.4
1.4.1 将redis加入到开机启动
vi /etc/rc.local
添加:
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
1.4.2 启动服务
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
1.4.3 停止redis
pkill redis
1.4.4 登录客户端
redis-cli -h 10.193.**.** -p 6379
1.4.5 redis密码
查看现有的redis密码
10.193.**.**> config get requirepass
设置redis密码
10.193.**.**> config set requirepass ****
重启服务生效,登录
redis-cli -h 10.193.**.** -p 6379 -a ****
2. flask_cache部署
pip3 install flask-caching --user --proxy=http://10.88.**.**:8888
[pip3 install flask-cache --user --proxy=http://10.88.**.**:8888, 安装后flask-cache后,使用过程中报错]
pip3 install redis --user --proxy=http://10.88.**.**:8888
使用过程中遇到werkzeug问题,因为版本问题,werkzeug 1.0版本造成的。
pip3 uninstall werkzeug --user --proxy=http://10.88.**.**:8888
pip3 install werkzeug==0.15.2 --user --proxy=http://10.88.**.**:8888
3. 修改server程序,增加如下内容
from flask_caching import Cache
cache = Cache()
class config():
# 缓存的存储类型
CACHE_TYPE = 'redis'
# 缓存的ip, 端口, 数据库
CACHE_REDIS_HOST = '10.193.98.91'
CACHE_REDIS_PORT = 6379
# CACHE_REDIS_DB = 5
app = Flask(__name__, static_url_path='')
CORS(app)
app.config.from_object(config)
cache.init_app(app)
@app.route('/pct_stat', methods=['GET', 'POST'])
@cache.cached(timeout=60*2)
def pct_stat():
return jsonify({"sum":"123456"})