-
Notifications
You must be signed in to change notification settings - Fork 0
Сached get_base_directories #889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
c70a8a2
6e41433
97a9824
ded6d9d
097e5f9
1ded88e
1f1e034
4ddfab9
36caf00
5803347
86b2778
ebe000c
229b885
3b31ef0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||||||||||
| AttributeValueValidator, | ||||||||||||||
| ) | ||||||||||||||
| from ldap_protocol.ldap_schema.entity_type_dao import EntityTypeDAO | ||||||||||||||
| from ldap_protocol.utils.async_cache import base_directories_cache | ||||||||||||||
| from ldap_protocol.utils.helpers import create_object_sid, generate_domain_sid | ||||||||||||||
| from ldap_protocol.utils.queries import get_domain_object_class | ||||||||||||||
| from password_utils import PasswordUtils | ||||||||||||||
|
|
@@ -113,6 +114,7 @@ async def setup_enviroment( | |||||||||||||
| domain=domain, | ||||||||||||||
| parent=domain, | ||||||||||||||
| ) | ||||||||||||||
| base_directories_cache.clear() | ||||||||||||||
|
|
||||||||||||||
| except Exception: | ||||||||||||||
| import traceback | ||||||||||||||
|
|
@@ -132,13 +134,13 @@ async def create_dir( | |||||||||||||
| is_system=is_system, | ||||||||||||||
| object_class=data["object_class"], | ||||||||||||||
| name=data["name"], | ||||||||||||||
| parent=parent, | ||||||||||||||
| ) | ||||||||||||||
| dir_.groups = [] | ||||||||||||||
| dir_.create_path(parent, dir_.get_dn_prefix()) | ||||||||||||||
|
|
||||||||||||||
| self._session.add(dir_) | ||||||||||||||
| await self._session.flush() | ||||||||||||||
| dir_.parent_id = parent.id if parent else None | ||||||||||||||
|
Comment on lines
140
to
+143
|
||||||||||||||
| self._session.add(dir_) | |
| await self._session.flush() | |
| dir_.parent_id = parent.id if parent else None | |
| dir_.parent_id = parent.id if parent else None | |
| self._session.add(dir_) | |
| await self._session.flush() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему то в таком случае parent_id не сохраняется
TheMihMih marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Async cache implementation.""" | ||
|
|
||
| import time | ||
| from functools import wraps | ||
| from typing import Callable, Generic, TypeVar | ||
|
|
||
| from entities import Directory | ||
|
|
||
| T = TypeVar("T") | ||
| DEFAULT_CACHE_TIME = 5 * 60 # 5 minutes | ||
|
|
||
|
|
||
| class AsyncTTLCache(Generic[T]): | ||
| def __init__(self, ttl: int | None = DEFAULT_CACHE_TIME) -> None: | ||
| self._ttl = ttl | ||
| self._value: T | None = None | ||
| self._expires_at: float | None = None | ||
|
|
||
| def clear(self) -> None: | ||
| self._value = None | ||
| self._expires_at = None | ||
|
|
||
| def __call__(self, func: Callable) -> Callable: | ||
| @wraps(func) | ||
| async def wrapper(*args: tuple, **kwargs: dict) -> T: | ||
| if self._value is not None: | ||
| if not self._expires_at or self._expires_at > time.monotonic(): | ||
| return self._value | ||
| self.clear() | ||
|
|
||
| result = await func(*args, **kwargs) | ||
|
|
||
| self._value = result | ||
| self._expires_at = ( | ||
| time.monotonic() + self._ttl if self._ttl else None | ||
| ) | ||
|
|
||
| return result | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| base_directories_cache = AsyncTTLCache[list[Directory]]() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| queryable_attr as qa, | ||
| ) | ||
|
|
||
| from .async_cache import base_directories_cache | ||
| from .const import EMAIL_RE, GRANT_DN_STRING | ||
| from .helpers import ( | ||
| create_integer_hash, | ||
|
|
@@ -35,13 +36,25 @@ | |
| ) | ||
|
|
||
|
|
||
| @base_directories_cache | ||
| async def get_base_directories(session: AsyncSession) -> list[Directory]: | ||
| """Get base domain directories.""" | ||
| result = await session.execute( | ||
| select(Directory) | ||
| .filter(qa(Directory.parent_id).is_(None)), | ||
| ) # fmt: skip | ||
| return list(result.scalars().all()) | ||
| res = [] | ||
| for dir_ in result.scalars(): | ||
| new_dir = Directory( | ||
| **{ | ||
| k: v | ||
| for k, v in dir_.__dict__.items() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно сделать так: Только columns сформировать один раз выше цикла что думаешь ?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ладно, там с id всё равно хрень |
||
| if not k.startswith("_") and k != "id" | ||
| }, | ||
| ) | ||
| new_dir.id = dir_.id | ||
| res.append(new_dir) | ||
| return res | ||
|
|
||
|
|
||
| async def get_user(session: AsyncSession, name: str) -> User | None: | ||
|
|
@@ -362,7 +375,7 @@ async def create_group( | |
| dir_ = Directory( | ||
| object_class="", | ||
| name=name, | ||
| parent=parent, | ||
| parent_id=parent.id, | ||
| ) | ||
| session.add(dir_) | ||
| await session.flush() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.