Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_lru_cache_unlimited_explicit():
cache = LRUCache(capacity=None)
for i in range(100):
cache[i] = i
assert len(cache.lru) == 100
def test_lru_cache_set_multiple():
cache = LRUCache(capacity=3)
cache["a"] = 1
cache["a"] = 2
cache["a"] = 3
cache["a"] = 4
assert cache.lru == ["a"]
def test_lru_cache():
cache = LRUCache(capacity=3)
cache["a"] = 1
cache["b"] = 2
cache["c"] = 3
_ = cache["a"] # move to front in lru queue
cache["d"] = 4 # move oldest item out of lru queue
try:
_ = cache['f']
except KeyError:
pass
assert cache.lru == ["c", "a", "d"]
def test_lru_cache_iteration_works():
cache = LRUCache()
count = 0
for _ in cache:
assert False, 'there should be no elements in the cache'
assert count == 0
def test_lru_cache_clear():
cache = LRUCache(capacity=3)
cache["a"] = 1
cache["b"] = 2
cache.clear()
assert cache.lru == []
def test_lru_cache_delete():
cache = LRUCache(capacity=3)
cache["a"] = 1
cache["b"] = 2
del cache["a"]
try:
del cache['f']
except KeyError:
pass
assert cache.lru == ["b"]
def test_lru_cache_get():
cache = LRUCache(capacity=3)
cache["a"] = 1
cache["b"] = 1
cache["c"] = 1
cache.get("a")
cache["d"] = 4
assert cache.lru == ["c", "a", "d"]
def test_lru_cache_unlimited():
cache = LRUCache()
for i in range(100):
cache[i] = i
assert len(cache.lru) == 100
def __init__(self, name, db, cache_size=10):
"""
Get access to a table.
:param name: The name of the table.
:type name: str
:param db: The parent database.
:type db: tinydb.database.TinyDB
:param cache_size: Maximum size of query cache.
"""
self.name = name
self._db = db
self._query_cache = LRUCache(capacity=cache_size)
old_ids = self._read().keys()
if old_ids:
self._last_id = max(i for i in old_ids)
else:
self._last_id = 0