mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-09-11 22:30:41 +03:00
* update chat_microservice * reformat logger_conf * add database * add service and repository logic * fix constants gpt base url * add models endpoints
32 lines
917 B
Python
32 lines
917 B
Python
from datetime import datetime, timedelta
|
|
from functools import lru_cache, wraps
|
|
from inspect import cleandoc
|
|
from typing import Any
|
|
|
|
|
|
def timed_cache(**timedelta_kwargs: Any) -> Any:
|
|
def _wrapper(func: Any) -> Any:
|
|
update_delta = timedelta(**timedelta_kwargs)
|
|
next_update = datetime.utcnow() + update_delta
|
|
# Apply @lru_cache to f with no cache size limit
|
|
cached_func = lru_cache(None)(func)
|
|
|
|
@wraps(func)
|
|
def _wrapped(*args: Any, **kwargs: Any) -> Any:
|
|
nonlocal next_update
|
|
now = datetime.utcnow()
|
|
if now >= next_update:
|
|
cached_func.cache_clear()
|
|
next_update = now + update_delta
|
|
return cached_func(*args, **kwargs)
|
|
|
|
return _wrapped
|
|
|
|
return _wrapper
|
|
|
|
|
|
def clean_doc(cls: Any) -> str | None:
|
|
if cls.__doc__ is None:
|
|
return None
|
|
return cleandoc(cls.__doc__)
|