mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 18:27:59 -05:00
[V3/permissions] Performance improvements (#1885)
* basic caching layer * bit more work, now with an upper size to the cache * cache fix * smarter cache invalidation * One more cache case * Put in a bare skeleton of something else still needed * more logic handling improvements * more work, still not finished * mass-resolve is done in theory, but needs testing * small bugfixin + comments * add note about before/after hooks * LRU-dict fix * when making comments about optimizations, provide historical context * fmt pass
This commit is contained in:
53
redbot/core/utils/caching.py
Normal file
53
redbot/core/utils/caching.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import collections
|
||||
|
||||
|
||||
class LRUDict:
|
||||
"""
|
||||
dict with LRU-eviction and max-size
|
||||
|
||||
This is intended for caching, it may not behave how you want otherwise
|
||||
|
||||
This uses collections.OrderedDict under the hood, but does not directly expose
|
||||
all of it's methods (intentional)
|
||||
"""
|
||||
|
||||
def __init__(self, *keyval_pairs, size):
|
||||
self.size = size
|
||||
self._dict = collections.OrderedDict(*keyval_pairs)
|
||||
|
||||
def __contains__(self, key):
|
||||
if key in self._dict:
|
||||
self._dict.move_to_end(key, last=True)
|
||||
return True
|
||||
return False
|
||||
|
||||
def __getitem__(self, key):
|
||||
ret = self._dict.__getitem__(key)
|
||||
self._dict.move_to_end(key, last=True)
|
||||
return ret
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if key in self._dict:
|
||||
self._dict.move_to_end(key, last=True)
|
||||
self._dict[key] = value
|
||||
if len(self._dict) > self.size:
|
||||
self._dict.popitem(last=False)
|
||||
|
||||
def __delitem__(self, key):
|
||||
return self._dict.__delitem__(key)
|
||||
|
||||
def clear(self):
|
||||
return self._dict.clear()
|
||||
|
||||
def pop(self, key):
|
||||
return self._dict.pop(key)
|
||||
|
||||
# all of the below access all of the items, and therefore shouldnt modify the ordering for eviction
|
||||
def keys(self):
|
||||
return self._dict.keys()
|
||||
|
||||
def items(self):
|
||||
return self._dict.items()
|
||||
|
||||
def values(self):
|
||||
return self._dict.values()
|
||||
Reference in New Issue
Block a user