Implementing TTL Cache in Python (3)
In this article, we will extend our previously implemented TTL cache decorator with a toggle feature for enabling and disabling the cache.
Overview of Cache Toggle Feature
Here are the specifications for the new cache toggle feature:
- Functions decorated with
lru_cache
will have an additionaluse_cache
argument, allowing one to toggle the cache functionality. - If
use_cache
is set toFalse
, the function will execute without using the cache,
but its result will still be stored in the cache. - If toggled back to
use_cache=True
, the cached content will be utilized.
Implementation
There are various possible ways to implement this feature. Here’s how I approached it: