Keep callers from changing a cached list result
A cached function returns its saved object again. If that object is a list, one caller can change the result seen by everyone else. Keep immutable values inside the cache and create a caller-owned list outside the cached function.
Set up
Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save as owned_cache_results.py:
from functools import lru_cache
@lru_cache(maxsize=16)
def shared_parts(text):
return text.split(",")
first = shared_parts("red,blue")
first.append("changed")
print("Shared result:", shared_parts("red,blue"))
@lru_cache(maxsize=16)
def cached_parts(text):
return tuple(text.split(","))
def parts(text):
return list(cached_parts(text))
owned = parts("red,blue")
owned.append("changed")
later = parts("red,blue")
assert later == ["red", "blue"]
assert owned is not later
assert cached_parts.cache_info().hits == 1
print("Caller result:", owned)
print("Later result:", later)
print("Cache hits:", cached_parts.cache_info().hits)Run:
python3 -B owned_cache_results.pyObserved output:
Shared result: ['red', 'blue', 'changed']
Caller result: ['red', 'blue', 'changed']
Later result: ['red', 'blue']
Cache hits: 1Put the copy outside the decorator
The wrapper creates a new list on every call, including cache hits. If parts itself were cached, that list would be shared again. The tuple contains strings, so neither the tuple nor its elements can be changed in place.
This shallow copy is enough for these values. A tuple containing dictionaries would still share those dictionaries. For nested mutable data, choose immutable records throughout or make an appropriate independent copy at the boundary.
Account for cache lifetime
maxsize=16 limits entries, not total bytes, and there is no automatic time-based expiry. Arguments and results remain referenced until eviction or clearing. This function depends only on its input string; a function that reads changing files would also need an invalidation policy.
Splitting two words does not need a cache in normal code. No performance benefit is measured here. The lru_cache documentation explains result retention and warns against caching calls that must produce distinct mutable objects.
Clean up
Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.
rm -- owned_cache_results.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.