Open mbeckersys opened 7 years ago
The LRU cache from the standard lib does the same:
>>> from functools import lru_cache
>>>
>>> @lru_cache(3)
... def test(a):
... return {'a': a}
...
>>> x = test(3)
>>> x
{'a': 3}
>>> x['a'] = 42
>>> x
{'a': 42}
>>> y = test(3)
>>> y
{'a': 42}
Hi,
When putting or getting mutable items to/from cache, the value should be a deep copy. Otherwise the cache contents can be modified implicitly, which produces a behavior that I find quiet unintuitive for a cache.
Here is an example:
The items on the cache should never change after
put
, unless we update them explicitly. This happens for all mutable types (dicts, lists, ...).I cannot think of a case where someone would want to save references in the cache, apart for performance reasons. I suggest to take a deep copy by default, and add an optional parameter to allow reverting to the current behavior if somebody really needs that (and knows what this is doing).