mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-29 02:24:35 -05:00
* Enable PEP604 Ruff rules * Fix harcoded signature in test * Hack CLVMStreamable test with note to fast follow
36 lines
886 B
Python
36 lines
886 B
Python
# Package: utils
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections import OrderedDict
|
|
from typing import Generic, TypeVar
|
|
|
|
K = TypeVar("K")
|
|
V = TypeVar("V")
|
|
|
|
|
|
class LRUCache(Generic[K, V]):
|
|
def __init__(self, capacity: int):
|
|
self.cache: OrderedDict[K, V] = OrderedDict()
|
|
self.capacity = capacity
|
|
|
|
def get(self, key: K) -> V | None:
|
|
if key not in self.cache:
|
|
return None
|
|
else:
|
|
self.cache.move_to_end(key)
|
|
return self.cache[key]
|
|
|
|
def put(self, key: K, value: V) -> None:
|
|
if self.capacity > 0:
|
|
self.cache[key] = value
|
|
self.cache.move_to_end(key)
|
|
if len(self.cache) > self.capacity:
|
|
self.cache.popitem(last=False)
|
|
|
|
def remove(self, key: K) -> None:
|
|
self.cache.pop(key)
|
|
|
|
def get_capacity(self) -> int:
|
|
return self.capacity
|