chia-blockchain/chia/util/lru_cache.py
Yostra a929ec889e
Use lru cache (#2433)
* use lru cache

* use new func

* cache block after write is done

* dont iterate as orederd dict
2021-04-26 23:21:58 -07:00

25 lines
675 B
Python

from collections import OrderedDict
from typing import Any, Optional
class LRUCache:
def __init__(self, capacity: int):
self.cache: OrderedDict = OrderedDict()
self.capacity = capacity
def get(self, key: Any) -> Optional[Any]:
if key not in self.cache:
return None
else:
self.cache.move_to_end(key)
return self.cache[key]
def put(self, key: Any, value: Any) -> None:
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: Any) -> None:
self.cache.pop(key)