mirror of
https://gitee.com/dify_ai/dify.git
synced 2024-12-04 20:28:12 +08:00
d069c668f8
Co-authored-by: StyleZhang <jasonapring2015@outlook.com> Co-authored-by: Garfield Dai <dai.hai@foxmail.com> Co-authored-by: chenhe <guchenhe@gmail.com> Co-authored-by: jyong <jyong@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yeuoly <admin@srmxy.cn>
23 lines
675 B
Python
23 lines
675 B
Python
from collections import OrderedDict
|
|
from typing import Any
|
|
|
|
|
|
class LRUCache:
|
|
def __init__(self, capacity: int):
|
|
self.cache = OrderedDict()
|
|
self.capacity = capacity
|
|
|
|
def get(self, key: Any) -> Any:
|
|
if key not in self.cache:
|
|
return None
|
|
else:
|
|
self.cache.move_to_end(key) # move the key to the end of the OrderedDict
|
|
return self.cache[key]
|
|
|
|
def put(self, key: Any, value: Any) -> None:
|
|
if key in self.cache:
|
|
self.cache.move_to_end(key)
|
|
self.cache[key] = value
|
|
if len(self.cache) > self.capacity:
|
|
self.cache.popitem(last=False) # pop the first item
|