Flask缓存设置方法抛出KeyError吗? [英] Flask cache set method throwing KeyError?

查看:223
本文介绍了Flask缓存设置方法抛出KeyError吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了缓存一些数据,我正在调用cache.set方法.但是,它抛出KeyError.错误日志:

In order to cache some data, I am calling cache.set method. However it is throwing KeyError. The error log:

 File "D:\Sample_Project\SomeRouter.py", line 176, in run
    FetchFeed._prepareCache(f_content, cache, _program, _sprint)
  File "D:\Sample_Project\SomeRouter.py", line 197, in _prepareCache
    cache.set(cKey, data[last_index:last_index + MAX_DATA_PER_PAGE])
  File "c:\python\lib\site-packages\flask_caching\__init__.py", line 254, in set
    return self.cache.set(*args, **kwargs)
  File "c:\python\lib\site-packages\flask_caching\__init__.py", line 246, in cache
    return app.extensions["cache"][self]
KeyError: <flask_caching.Cache object at 0x04F9C8D0>

服务器模块如下:

cache_type = 'simple' if 'FLASK_ENV' in os.environ and os.environ['FLASK_ENV'] == 'development' else 'uwsgi'
cache = Cache(config={'CACHE_TYPE': cache_type})
app = Flask("MY_PROJECT")
cache.init_app(app)

# some api.route functions
# goes here ....

if __name__ == "__main__":
    with app.app_context():
        cache.clear()
    app.run(host="0.0.0.0")

和SomeRouter模块:

And SomeRouter module:

from server import cache

@staticmethod
def _prepareCache(data, cache, program):
    total_records = len(data)
    if total_records > 0:
        cKey = FetchFeed \
            ._constructCacheKey(program)
        cache.set(cKey, data)
    else:
        print("data size is empty.")

注意:我已删除了不必要的代码.

我还放置了断点,并在服务器模块本身中调用了cache.set(some_key,some_value).它返回True,但是在SomeRouter模块中导入和使用时,同一缓存对象将引发KeyError.可能是我导入对象的方式错误吗?我也尝试在使用缓存对象之前先导入它,但是它没有用.知道这里发生了什么吗?

I also put breakpoints, and called cache.set(some_key, some_value) in server module itself. It is returning True, but same cache object is throwing KeyError when imported and used in SomeRouter module. Can it be that the way I am importing an object is wrong? I also tried importing the cache object right before using it, but it did not work. Any idea what is going on here?

推荐答案

问题是我在请求上下文之外(即在"SomeRouter"模块中)访问cache对象,因此我不知道它在哪个上下文中已被使用.

The problem was I was accessing cache object outside request context i.e. in "SomeRouter" module, and because of which it was unaware that under which context it is been used.

在已接收到请求的server模块中,缓存了解应用程序应用程序,但是在SomeRouter模块中的cache.set(cKey, data)期间,它将引发KeyError.如上所述,此错误是合理的.

In server module where request has been received, the cache knows about the app application, but during cache.set(cKey, data) in SomeRouter module, it throws KeyError. This error is justified as mentioned above.

解决方案是按如下所示推送应用程序上下文:

The resolution is to push the application context as below:

from server import app, cache

# Using context
with app.app_context():
    cache.set(cKey, data)

这将推送一个新的应用程序上下文(使用应用程序的应用程序).

This will push a new application context (using the application of app).

非常感谢马克·希尔德雷斯,感谢他对 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆