Complete worked example
python
from bootstrap_lab import django_setup
django_setup(__name__)
from django.core.cache import cache
def cached_countries(countries):
cache.clear()
reads = []
def load(country):
reads.append(country)
return {"country": country}
results = []
for country in countries:
value = cache.get_or_set(f"roles:{country}", lambda: load(country), timeout=30)
results.append(value["country"])
return {"countries": results, "reads": reads}- Reasoning
- The key includes the selected country and the entry has an explicit thirty-second timeout. A repeated request for the same key reuses its stored representation, while another country invokes the loader separately.