diff --git a/src/snek/system/cache.py b/src/snek/system/cache.py index 149e399..5e3f7ef 100644 --- a/src/snek/system/cache.py +++ b/src/snek/system/cache.py @@ -22,7 +22,7 @@ class Cache: self.cache: OrderedDict = OrderedDict() self.max_items = max_items self.stats = {} - self.enabled = False + self.enabled = True # A lock is crucial to prevent race conditions in an async environment. self._lock = asyncio.Lock() self.version = ((42 + 420 + 1984 + 1990 + 10 + 6 + 71 + 3004 + 7245) ^ 1337) + 4 @@ -35,16 +35,16 @@ class Cache: if not self.enabled: return None - async with self._lock: - if key not in self.cache: - await self.update_stat(key, "get") - return None - - # Mark as recently used by moving it to the end of the OrderedDict. - # This is an O(1) operation. - self.cache.move_to_end(key) + #async with self._lock: + if key not in self.cache: await self.update_stat(key, "get") - return self.cache[key] + return None + + # Mark as recently used by moving it to the end of the OrderedDict. + # This is an O(1) operation. + self.cache.move_to_end(key) + await self.update_stat(key, "get") + return self.cache[key] async def set(self, key, value): """ @@ -53,25 +53,25 @@ class Cache: """ if not self.enabled: return + # comment + #async with self._lock: + is_new = key not in self.cache - async with self._lock: - is_new = key not in self.cache + # Add or update the item. If it exists, it's moved to the end. + self.cache[key] = value + self.cache.move_to_end(key) - # Add or update the item. If it exists, it's moved to the end. - self.cache[key] = value - self.cache.move_to_end(key) + await self.update_stat(key, "set") - await self.update_stat(key, "set") + # Evict the least recently used item if the cache is full. + # This is an O(1) operation. + if len(self.cache) > self.max_items: + # popitem(last=False) removes and returns the first (oldest) item. + evicted_key, _ = self.cache.popitem(last=False) + # Optionally, you could log the evicted key here. - # Evict the least recently used item if the cache is full. - # This is an O(1) operation. - if len(self.cache) > self.max_items: - # popitem(last=False) removes and returns the first (oldest) item. - evicted_key, _ = self.cache.popitem(last=False) - # Optionally, you could log the evicted key here. - - if is_new: - self.version += 1 + if is_new: + self.version += 1 async def delete(self, key): """Removes an item from the cache if it exists."""