diff --git a/powersimdata/utility/helpers.py b/powersimdata/utility/helpers.py index 9aaec9228..a56534cb9 100644 --- a/powersimdata/utility/helpers.py +++ b/powersimdata/utility/helpers.py @@ -63,7 +63,7 @@ def put(self, key, obj): :param tuple key: a tuple used to lookup the cached value :param Any obj: the object to cache """ - self._cache[key] = obj + self._cache[key] = copy.deepcopy(obj) def get(self, key): """Retrieve the value associated with key if it exists. diff --git a/powersimdata/utility/tests/test_helpers.py b/powersimdata/utility/tests/test_helpers.py index 7b75438c1..d6cea1552 100644 --- a/powersimdata/utility/tests/test_helpers.py +++ b/powersimdata/utility/tests/test_helpers.py @@ -68,6 +68,17 @@ def test_mem_cache_get_returns_copy(): assert id(cache.get(key)) != id(obj) +def test_mem_cache_put_version_never_changes(): + cache = MemoryCache() + key = cache_key("foo", 4) + obj = {"key1": "value1"} + cache.put(key, obj) + obj["key2"] = "value2" + assert "key1" in cache.get(key) + assert "key2" not in cache.get(key) + assert "key2" in obj + + def test_copy_command(): expected = r"\cp -p source dest" command = CommandBuilder.copy("source", "dest")