Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask cache gives me 'AttributeError: 'Cache' object has no attribute 'app''

I am using application factory pattern in which I have initialized my cache

from xyz.caching import cache # this is the cache object
def create_app():
    app = Flask(__name__)
    cache.init_app(app)
    # other relevant variables.
return app

My caching.py

from flask_caching import Cache
cache = Cache(config={....})

When I import this in any file xyz.caching import cache, this works totally fine. However, in my application I have a entry point script, run.py

run.py

from xyz.caching import cache

def run_this():
  cache.get('XXX')

if __name__ == "__main__":
    run_this()

After running python run.py, I get the following error

'AttributeError: 'Cache' object has no attribute 'app''

Pls. guide me what is wrong in this, why I am getting this error and what is the way to solve this ?

like image 447
Simplecode Avatar asked Jul 24 '26 20:07

Simplecode


1 Answers

I got this message because I forgot to initialize the app

cache.init_app(app)

I would try to debug if this method call is reached before cache.get('XXX') is called.

like image 143
maxim1500 Avatar answered Jul 28 '26 15:07

maxim1500



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!