How can I configure Flask-Cache with infinite timeout
Asked Answered
K

2

13

In the Flask-Cache documentation all the examples use a finite timeout.

I'd like to never refresh the cache while the app is running. Is this possible, and if so how do I do it?

Kikuyu answered 30/7, 2013 at 5:8 Comment(2)
You can't just set it to a really big number? If you want it cached forever, maybe you should just save it to a file and serve that?Stenotype
Both solutions will do, but I was just curious if infinite caching was supported at allKikuyu
B
23

Flask-Cache uses werkzeug.contrib.cache behind the scenes. From the documentation it's made clear that

A timeout of 0 indicates that the cache never expires.

So yes, infinite caching is supported and can be turned on by setting the timeout to zero.

Boatwright answered 20/4, 2016 at 19:38 Comment(3)
Here is an example: cache.set(event, value, timeout=0)Tientiena
Documentation link is broken, here is one for Flask-Caching (not Flask-Cache!): flask-caching.readthedocs.io/en/latestCholeric
Does an infinite timeout mean the cache files are never deleted?Virilism
E
8

There does not seem to be anything listed in the docs. I have used the following and it works fine.

     cache = Cache(webapp, config={
         'CACHE_TYPE': 'filesystem',
         'CACHE_DIR': 'cache-dir', 
         'CACHE_DEFAULT_TIMEOUT': 922337203685477580,
         'CACHE_THRESHOLD': 922337203685477580
     })

That is way more years than you will need to worry about so for all intents and purposes, let's call that infinite.

Elwandaelwee answered 13/10, 2014 at 14:20 Comment(2)
As far as CACHE_THRESHOLD goes you could set it to e.g. math.inf which feels cleaner.Northcliffe
Big, undefined numbers lead to poor code readability. If I was another dev looking through this code years after it was written, I would have no idea what those numbers mean or how they were generated. Using something more symbolic like 0 or math.inf is much better than this.Chromatism

© 2022 - 2024 — McMap. All rights reserved.