简体   繁体   English

Flask-Caching 使用 UWSGI 缓存 NGINX

[英]Flask-Caching use UWSGI cache with NGINX

The UWSGI is connected to the flask app per UNIX-Socket: UWSGI 通过 UNIX-Socket 连接到 flask 应用程序:

NGINX (LISTEN TO PORT 80) <-> UWSGI (LISTER PER UNIX-SOCKER) <-> FLASK-APP NGINX(监听端口 80)<-> UWSGI(每个 UNIX-SOCKER 的列表)<-> FLASK-APP

I have initalized a uwsgi cache to handle global data.我已经初始化了一个 uwsgi 缓存来处理全局数据。 I want to handle the cache with python package flask-caching.我想用 python package flask-caching 处理缓存。

I am trying to init the Cache-instance with the correct cache address.我正在尝试使用正确的缓存地址初始化缓存实例。 There seems to be something wrong.似乎有什么不对劲。 I think, that the parameters for app.run() are not relevant for uwsgi.我认为,app.run() 的参数与 uwsgi 无关。

If I am setting a cache entry, it return always None:如果我正在设置缓存条目,它总是返回 None:

app.route("/")
def test():
    cache.set("test", "OK", timeout=0)
    a = cache.get("test")
    return a

main.py主程序

from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
# Check Configuring Flask-Caching section for more details
cache = Cache(app, config={'CACHE_TYPE': 'uwsgi', 'CACHE_UWSGI_NAME':'mycache@localhost'})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

uwsgi.ini uwsgi.ini文件

[uwsgi]
module = main
callable = app
cache2 = name=mycache,items=100

nginx.conf nginx.conf

server {
    listen 80;
    location / {
        try_files $uri @app;
    }
    location @app {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
    location /static {
        alias /app/testapp/static;
    }
}

I am working with the docker build from https://github.com/tiangolo/uwsgi-nginx-flask-docker .我正在使用来自https://github.com/tiangolo/uwsgi-nginx-flask-docker的 docker 版本。 The app is working, expect the cache.该应用程序正在运行,期待缓存。

Be aware of using of spawning multiple processes for NGINX. Every process handles its own cache.请注意为 NGINX 使用生成多个进程。每个进程处理自己的缓存。 Without an additional layer, it is not possible to access to a cache from different nginx process.如果没有附加层,则无法从不同的 nginx 进程访问缓存。


This answer was posted as an edit to the question Flask-Caching use UWSGI cache with NGINX by the OP ewro under CC BY-SA 4.0.此答案作为对问题Flask-Caching 使用 UWSGI 缓存与 NGINX编辑发布,由 CC BY-SA 4.0 下的 OP ewro 发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM