Skip to content

Commit e66344b

Browse files
svsgooglecopybara-github
authored andcommitted
Allow LRUSizeTrackingCache to have a max size of 0.
Allowing 1 byte, but disallowing 0 bytes is somewhat silly. PiperOrigin-RevId: 871814019 Change-Id: I6ebbea20d449bc0c77dc20e8e75615d06b26f76e
1 parent c9b0e4d commit e66344b

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

py/koladata/ext/persisted_data/lru_size_tracking_cache.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def __init__(
7878
that the cache should hold. The cache will evict the least recently used
7979
entries to ensure that it never exceeds this limit.
8080
"""
81-
if max_total_bytes_of_entries_in_cache <= 0:
81+
if max_total_bytes_of_entries_in_cache < 0:
8282
raise ValueError(
83-
'max_total_bytes_of_entries_in_cache must be positive, but is'
83+
'max_total_bytes_of_entries_in_cache must be non-negative, but is'
8484
f' {max_total_bytes_of_entries_in_cache}'
8585
)
8686

@@ -155,9 +155,9 @@ def set_max_total_bytes_of_entries_in_cache(
155155
self, max_total_bytes_of_entries_in_cache: int
156156
) -> None:
157157
"""Sets the limit on the total size of all entries in the cache."""
158-
if max_total_bytes_of_entries_in_cache <= 0:
158+
if max_total_bytes_of_entries_in_cache < 0:
159159
raise ValueError(
160-
'max_total_bytes_of_entries_in_cache must be positive, but is'
160+
'max_total_bytes_of_entries_in_cache must be non-negative, but is'
161161
f' {max_total_bytes_of_entries_in_cache}'
162162
)
163163
with self._rlock:

py/koladata/ext/persisted_data/lru_size_tracking_cache_test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,29 @@ def test_basic_functionality(self):
114114
self.assertIsNone(cache.get('d'))
115115
self.assertIsNone(cache.get('e'))
116116

117-
# Setting the max size to a non-positive value raises an error.
118-
with self.assertRaises(ValueError):
119-
cache.set_max_total_bytes_of_entries_in_cache(0)
117+
# Setting the max size to a negative value raises an error.
120118
with self.assertRaises(ValueError):
121119
cache.set_max_total_bytes_of_entries_in_cache(-1)
122120
# The max size is not changed.
123121
self.assertEqual(cache.get_max_total_bytes_of_entries_in_cache(), 10)
124122

125-
# Creating a new cache with a non-positive max size raises an error.
126-
with self.assertRaises(ValueError):
127-
lru_size_tracking_cache.LruSizeTrackingCache(
128-
max_total_bytes_of_entries_in_cache=0,
129-
)
123+
# Using zero is fine and evicts all entries, thereby disabling caching.
124+
cache.set_max_total_bytes_of_entries_in_cache(0)
125+
self.assertEqual(cache.get_max_total_bytes_of_entries_in_cache(), 0)
126+
127+
# Creating a new cache with a negative max size raises an error.
130128
with self.assertRaises(ValueError):
131129
lru_size_tracking_cache.LruSizeTrackingCache(
132130
max_total_bytes_of_entries_in_cache=-1,
133131
)
134132

133+
# We can create a cache with zero max size. Such a cache is not very useful
134+
# unless we plan to increase the max size later.
135+
cache = lru_size_tracking_cache.LruSizeTrackingCache(
136+
max_total_bytes_of_entries_in_cache=0,
137+
)
138+
self.assertEqual(cache.get_max_total_bytes_of_entries_in_cache(), 0)
139+
135140

136141
if __name__ == '__main__':
137142
absltest.main()

0 commit comments

Comments
 (0)