mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Use OrderedDict in ExpiringCache
This commit is contained in:
parent
897f8752da
commit
6d00213e80
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
from synapse.util.caches import register_cache
|
from synapse.util.caches import register_cache
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ class ExpiringCache(object):
|
|||||||
|
|
||||||
self._reset_expiry_on_get = reset_expiry_on_get
|
self._reset_expiry_on_get = reset_expiry_on_get
|
||||||
|
|
||||||
self._cache = {}
|
self._cache = OrderedDict()
|
||||||
|
|
||||||
self.metrics = register_cache(cache_name, self)
|
self.metrics = register_cache(cache_name, self)
|
||||||
|
|
||||||
@ -70,15 +71,8 @@ class ExpiringCache(object):
|
|||||||
self._cache[key] = _CacheEntry(now, value)
|
self._cache[key] = _CacheEntry(now, value)
|
||||||
|
|
||||||
# Evict if there are now too many items
|
# Evict if there are now too many items
|
||||||
if self._max_len and len(self) > self._max_len:
|
while self._max_len and len(self) > self._max_len:
|
||||||
sorted_entries = sorted(
|
self._cache.popitem(last=False)
|
||||||
self._cache.keys(),
|
|
||||||
key=lambda item: item[1].time,
|
|
||||||
)
|
|
||||||
|
|
||||||
while len(self) > self._max_len and sorted_entries:
|
|
||||||
key = sorted_entries.pop()
|
|
||||||
self._cache.pop(key)
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user