- Remove legacy `self.get` migration from strings
- Add "forced exp_time" by using force and exp time if "current cache time" > forced exp_time replace it with current and force refresh it.
pull/1/head
hikariatama 2022-08-08 21:10:19 +00:00
parent 6f43fbc994
commit 5f5ac8859e
2 changed files with 11 additions and 24 deletions

View File

@ -949,7 +949,7 @@ class Modules:
instance.allclients = self.allclients
instance.allmodules = self
instance.hikka = True
instance.get = partial(self._mod_get, _module=instance)
instance.get = partial(self._mod_get, _modname=instance.__class__.__name__)
instance.set = partial(self._mod_set, _modname=instance.__class__.__name__)
instance.set = partial(self._mod_pointer, _modname=instance.__class__.__name__)
instance.get_prefix = partial(self._db.get, "hikka.main", "command_prefix", ".")
@ -989,27 +989,8 @@ class Modules:
self,
key: str,
default: Optional[Hashable] = None,
_module: Module = None,
_modname: str = None,
) -> Hashable:
mod, legacy = _module.__class__.__name__, _module.strings["name"]
if self._db.get(legacy, key, Placeholder) is not Placeholder:
for iterkey, value in self._db[legacy].items():
if iterkey == "__config__":
# Config already uses classname as key
# No need to migrate
continue
if isinstance(value, dict) and isinstance(
self._db.get(mod, iterkey), dict
):
self._db[mod][iterkey].update(value)
else:
self._db.set(mod, iterkey, value)
logger.debug(f"Migrated {legacy} -> {mod}")
del self._db[legacy]
return self._db.get(mod, key, default)
def _mod_set(self, key: str, value: Hashable, _modname: str = None) -> bool:

View File

@ -65,6 +65,7 @@ class CacheRecordPerms:
self._hashable_entity = copy.deepcopy(hashable_entity)
self._hashable_user = copy.deepcopy(hashable_user)
self._exp = round(time.time() + exp)
self.ts = time.time()
def expired(self):
return self._exp < time.time()
@ -165,14 +166,14 @@ def install_perms_caching(client: TelegramClient):
async def new(
entity: EntityLike,
user: Optional[EntityLike] = None,
exp: int = 5 * 60,
exp: int = None,
force: bool = False,
):
# Will be used to determine, which client caused logging messages
# parsed via inspect.stack()
_hikka_client_id_logging_tag = copy.copy(client.tg_id) # skipcq
if force:
if force and not exp:
return await old(entity, user)
entity = await client.get_entity(entity)
@ -214,6 +215,11 @@ def install_perms_caching(client: TelegramClient):
hashable_entity
and hashable_user
and hashable_user in client._hikka_perms_cache.get(hashable_entity, {})
and (
not exp
or client._hikka_perms_cache[hashable_entity][hashable_user].ts + exp
> time.time()
)
):
logger.debug(f"Using cached perms {hashable_entity} ({hashable_user})")
return copy.deepcopy(
@ -227,7 +233,7 @@ def install_perms_caching(client: TelegramClient):
hashable_entity,
hashable_user,
resolved_perms,
exp,
exp or 5 * 60,
)
client._hikka_perms_cache.setdefault(hashable_entity, {})[
hashable_user