-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.py
More file actions
26 lines (21 loc) · 665 Bytes
/
cache.py
File metadata and controls
26 lines (21 loc) · 665 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
LRU Cache implementation for BuildAutomata Memory System
Copyright 2025 Jurden Bruce
"""
from collections import OrderedDict
class LRUCache(OrderedDict):
"""Simple LRU cache with max size"""
def __init__(self, maxsize=1000):
self.maxsize = maxsize
super().__init__()
def __setitem__(self, key, value):
if key in self:
self.move_to_end(key)
super().__setitem__(key, value)
if len(self) > self.maxsize:
oldest = next(iter(self))
del self[oldest]
def __getitem__(self, key):
value = super().__getitem__(key)
self.move_to_end(key)
return value