Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Optionally measure size of cache by sum of length of values #1815
Conversation
erikjohnston
added some commits
Jan 13, 2017
erikjohnston
assigned
NegativeMjark
Jan 16, 2017
erikjohnston
added some commits
Jan 16, 2017
| @@ -58,6 +58,18 @@ def __init__(self, max_size, keylen=1, cache_type=dict): | ||
| lock = threading.Lock() | ||
| + def cache_len(): | ||
| + if size_callback is not None: | ||
| + return sum(size_callback(node.value) for node in cache.itervalues()) |
NegativeMjark
Jan 16, 2017
Contributor
This is probably sub-optimal since it iterates the entire cache. You will probably need to store the current size somewhere and update it as things are added or removed.
NegativeMjark
Jan 16, 2017
Contributor
If you want to be awful you can move the if statement outside the function def and define the function twice...
| ) | ||
| def __len__(self): | ||
| - return len(self._cache) | ||
| + if self.iterable: | ||
| + return sum(len(value.value) for value in self._cache.itervalues()) |
NegativeMjark
Jan 16, 2017
Contributor
This is probably sub-optimal since it iterates the entire cache. You will probably need to store the current size somewhere and update it as things are added or removed.
erikjohnston
added some commits
Jan 16, 2017
| @synchronized | ||
| def cache_set_default(key, value): | ||
| node = cache.get(key, None) | ||
| if node is not None: | ||
| + evict() # As the new node may be bigger than the old node. |
NegativeMjark
Jan 17, 2017
Contributor
This doesn't seem necessary if we aren't modifying the cache.
| @@ -128,7 +128,7 @@ def test_set(self): | ||
| m = Mock() | ||
| cache = LruCache(1) | ||
| - cache.set("key", "value", m) | ||
| + cache.set("key", "value", [m]) |
erikjohnston commentedJan 16, 2017
No description provided.