Upload hot_cache.py with huggingface_hub
Browse files- hot_cache.py +78 -0
hot_cache.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import OrderedDict
|
| 2 |
+
|
| 3 |
+
class LRUCache:
|
| 4 |
+
"""
|
| 5 |
+
Least Recently Used (LRU) Cache.
|
| 6 |
+
設計書で言及されているホットキャッシュのシンプルな実装です。
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
def __init__(self, max_size: int = 20):
|
| 10 |
+
"""
|
| 11 |
+
Args:
|
| 12 |
+
max_size (int): キャッシュの最大サイズ。
|
| 13 |
+
"""
|
| 14 |
+
if max_size <= 0:
|
| 15 |
+
raise ValueError("max_size must be a positive integer.")
|
| 16 |
+
self.max_size = max_size
|
| 17 |
+
self._cache = OrderedDict()
|
| 18 |
+
|
| 19 |
+
def __contains__(self, key):
|
| 20 |
+
"'key in cache' 構文をサポートします。"
|
| 21 |
+
return key in self._cache
|
| 22 |
+
|
| 23 |
+
def __getitem__(self, key):
|
| 24 |
+
"キーに対応する値を取得し、そのキーを最も最近使用されたものとしてマークします。"
|
| 25 |
+
if key not in self._cache:
|
| 26 |
+
raise KeyError(f"Key '{key}' not found in cache.")
|
| 27 |
+
|
| 28 |
+
# アイテムを最後に移動させて「最近使用した」ことを示す
|
| 29 |
+
self._cache.move_to_end(key)
|
| 30 |
+
return self._cache[key]
|
| 31 |
+
|
| 32 |
+
def __setitem__(self, key, value):
|
| 33 |
+
"キーと値のペアをキャッシュに追加します。"
|
| 34 |
+
if key in self._cache:
|
| 35 |
+
# 既存のキーの場合は、最近使用したことを示すために移動
|
| 36 |
+
self._cache.move_to_end(key)
|
| 37 |
+
|
| 38 |
+
self._cache[key] = value
|
| 39 |
+
|
| 40 |
+
# キャッシュサイズが上限を超えた場合、最も古く使用されていないアイテムを削除
|
| 41 |
+
if len(self._cache) > self.max_size:
|
| 42 |
+
self._cache.popitem(last=False)
|
| 43 |
+
|
| 44 |
+
def get(self, key, default=None):
|
| 45 |
+
"キーが存在しない場合に例外を送出しないバージョンの get です。"
|
| 46 |
+
if key in self._cache:
|
| 47 |
+
return self[key]
|
| 48 |
+
return default
|
| 49 |
+
|
| 50 |
+
@property
|
| 51 |
+
def size(self):
|
| 52 |
+
"現在のキャッシュサイズを返します。"
|
| 53 |
+
return len(self._cache)
|
| 54 |
+
|
| 55 |
+
# --- 使用例 ---
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
# サイズ3のキャッシュを作成
|
| 58 |
+
cache = LRUCache(max_size=3)
|
| 59 |
+
|
| 60 |
+
print("--- Cache Operations ---")
|
| 61 |
+
cache['coord_1'] = "Tile 1 Data"
|
| 62 |
+
cache['coord_2'] = "Tile 2 Data"
|
| 63 |
+
cache['coord_3'] = "Tile 3 Data"
|
| 64 |
+
print("Cache after adding 3 items:", cache._cache)
|
| 65 |
+
|
| 66 |
+
# coord_1にアクセス -> 最近使用されたアイテムになる
|
| 67 |
+
print("\nAccessing 'coord_1'...")
|
| 68 |
+
_ = cache['coord_1']
|
| 69 |
+
print("Cache state:", cache._cache)
|
| 70 |
+
|
| 71 |
+
# 新しいアイテムを追加 -> 最も古く使用されていない 'coord_2' が削除される
|
| 72 |
+
print("\nAdding 'coord_4', expecting 'coord_2' to be evicted...")
|
| 73 |
+
cache['coord_4'] = "Tile 4 Data"
|
| 74 |
+
print("Cache state:", cache._cache)
|
| 75 |
+
|
| 76 |
+
print("\nIs 'coord_2' in cache?", 'coord_2' in cache)
|
| 77 |
+
print("Is 'coord_3' in cache?", 'coord_3' in cache)
|
| 78 |
+
print("Current cache size:", cache.size)
|