| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | from collections import OrderedDict | 
					
						
							|  |  |  | from typing import Any | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LRUCache: | 
					
						
							|  |  |  |     def __init__(self, capacity: int): | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         self.cache: OrderedDict[Any, Any] = OrderedDict() | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         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 |