安装
pip install flask-cache
pip install redis
增加配置 config.py
CACHE_TYPE = 'redis'
CACHE_DEFAULT_TIMEOUT = 60 * 60 * 24
CACHE_REDIS_HOST = 'localhost'
CACHE_REDIS_PORT = 6379
CACHE_REDIS_DB = 0
修改message资源
class MessageResource(Resource):
"""Single object resource
"""
method_decorators = [jwt_required]
@cache.cached(key_prefix='%s')
def get(self, message_id):
schema = MessageSchema()
message = Message.query.get_or_404(message_id)
return schema.dump(message).data
@admin_required
def delete(self, message_id):
message = Message.query.get_or_404(message_id)
db.session.delete(message)
db.session.commit()
cache.clear()
return {"msg": "message deleted"}
打开redis-cli 即可查到访问后的缓存数据
网友评论