-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: drop
cachetools
dependency in favor of simple local implement…
…ation
- Loading branch information
Showing
7 changed files
with
67 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from collections import OrderedDict | ||
|
||
|
||
class LRUCache(dict): | ||
def __init__(self, maxsize): | ||
super().__init__() | ||
self._order = OrderedDict() | ||
self.maxsize = maxsize | ||
|
||
def clear(self): | ||
super().clear() | ||
self._order.clear() | ||
|
||
def __getitem__(self, key): | ||
value = super().__getitem__(key) | ||
self._update(key) | ||
return value | ||
|
||
def __setitem__(self, key, value): | ||
maxsize = self.maxsize | ||
if maxsize <= 0: | ||
return | ||
if key not in self: | ||
while len(self) >= maxsize: | ||
self.popitem() | ||
super().__setitem__(key, value) | ||
self._update(key) | ||
|
||
def __delitem__(self, key): | ||
super().__delitem__(key) | ||
del self._order[key] | ||
|
||
def popitem(self): | ||
"""Remove and return the least recently used key-value pair.""" | ||
key, _ = self._order.popitem(last=False) | ||
return key, super().pop(key) | ||
|
||
def _update(self, key): | ||
try: | ||
self._order.move_to_end(key) | ||
except KeyError: | ||
self._order[key] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from google.auth._cache import LRUCache | ||
|
||
|
||
def test_lru_cache(): | ||
lru_cache = LRUCache(2) | ||
lru_cache["a"] = 1 | ||
lru_cache["b"] = 2 | ||
assert lru_cache["a"] == 1 | ||
lru_cache["c"] = 3 | ||
assert "b" not in lru_cache | ||
assert lru_cache["a"] == 1 | ||
assert lru_cache["c"] == 3 | ||
lru_cache["d"] = 4 | ||
assert "a" not in lru_cache | ||
assert lru_cache["c"] == 3 | ||
assert lru_cache["d"] == 4 | ||
|
||
|
||
def test_zero_size_lru_cache(): | ||
lru_cache = LRUCache(0) | ||
lru_cache["a"] = 1 | ||
assert "a" not in lru_cache |