Update.
This commit is contained in:
parent
214ac33049
commit
8fe372532b
@ -22,7 +22,7 @@ class Cache:
|
|||||||
self.cache: OrderedDict = OrderedDict()
|
self.cache: OrderedDict = OrderedDict()
|
||||||
self.max_items = max_items
|
self.max_items = max_items
|
||||||
self.stats = {}
|
self.stats = {}
|
||||||
self.enabled = False
|
self.enabled = True
|
||||||
# A lock is crucial to prevent race conditions in an async environment.
|
# A lock is crucial to prevent race conditions in an async environment.
|
||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
self.version = ((42 + 420 + 1984 + 1990 + 10 + 6 + 71 + 3004 + 7245) ^ 1337) + 4
|
self.version = ((42 + 420 + 1984 + 1990 + 10 + 6 + 71 + 3004 + 7245) ^ 1337) + 4
|
||||||
@ -35,16 +35,16 @@ class Cache:
|
|||||||
if not self.enabled:
|
if not self.enabled:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async with self._lock:
|
#async with self._lock:
|
||||||
if key not in self.cache:
|
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)
|
|
||||||
await self.update_stat(key, "get")
|
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):
|
async def set(self, key, value):
|
||||||
"""
|
"""
|
||||||
@ -53,25 +53,25 @@ class Cache:
|
|||||||
"""
|
"""
|
||||||
if not self.enabled:
|
if not self.enabled:
|
||||||
return
|
return
|
||||||
|
# comment
|
||||||
|
#async with self._lock:
|
||||||
|
is_new = key not in self.cache
|
||||||
|
|
||||||
async with self._lock:
|
# Add or update the item. If it exists, it's moved to the end.
|
||||||
is_new = key not in self.cache
|
self.cache[key] = value
|
||||||
|
self.cache.move_to_end(key)
|
||||||
|
|
||||||
# Add or update the item. If it exists, it's moved to the end.
|
await self.update_stat(key, "set")
|
||||||
self.cache[key] = value
|
|
||||||
self.cache.move_to_end(key)
|
|
||||||
|
|
||||||
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.
|
if is_new:
|
||||||
# This is an O(1) operation.
|
self.version += 1
|
||||||
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
|
|
||||||
|
|
||||||
async def delete(self, key):
|
async def delete(self, key):
|
||||||
"""Removes an item from the cache if it exists."""
|
"""Removes an item from the cache if it exists."""
|
||||||
|
Loading…
Reference in New Issue
Block a user