-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor Translator and LocaleStr (#86)
* WIP * WIP * Modify commands * Refactor LevelStr * Some refactor * Add back key to app commands locale str * Fix search command invalid description * Add aiocache * Fix enum issues
- Loading branch information
Showing
106 changed files
with
1,476 additions
and
2,197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from collections import OrderedDict, defaultdict | ||
from typing import Any | ||
|
||
import aiocache | ||
|
||
|
||
class LFUCache(aiocache.SimpleMemoryCache): | ||
def __init__(self, maxsize: int = 1024, **kwargs: Any) -> None: | ||
super().__init__(**kwargs) | ||
self._maxsize = maxsize | ||
self._frequency: defaultdict[Any, int] = defaultdict(int) | ||
self._freq_list: OrderedDict[int, list[Any]] = OrderedDict() | ||
|
||
def _update_freq(self, key: Any) -> None: | ||
freq = self._frequency[key] | ||
self._frequency[key] += 1 | ||
|
||
if freq in self._freq_list: | ||
self._freq_list[freq].remove(key) | ||
if not self._freq_list[freq]: | ||
del self._freq_list[freq] | ||
|
||
if self._frequency[key] not in self._freq_list: | ||
self._freq_list[self._frequency[key]] = [] | ||
self._freq_list[self._frequency[key]].append(key) | ||
|
||
async def _evict(self) -> None: | ||
if len(self._cache) >= self._maxsize: | ||
min_freq = next(iter(self._freq_list)) | ||
key_to_evict = self._freq_list[min_freq].pop(0) | ||
if not self._freq_list[min_freq]: | ||
del self._freq_list[min_freq] | ||
await super().delete(key_to_evict) | ||
del self._frequency[key_to_evict] | ||
|
||
async def get(self, key: Any, default: Any = None) -> Any: | ||
if await super().exists(key): | ||
self._update_freq(key) | ||
return await super().get(key, default) | ||
|
||
async def set(self, key: Any, value: Any, ttl: int = 0) -> None: | ||
await self._evict() | ||
await super().set(key, value, ttl) | ||
self._update_freq(key) | ||
|
||
async def delete(self, key: Any) -> None: | ||
await super().delete(key) | ||
del self._frequency[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.