-
Notifications
You must be signed in to change notification settings - Fork 0
Add redis cache object sid 1412 #983
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5b6530
Enhance: integrate ObjectSidCacheRedisClient and ObjectSidAllowedObje…
cdcce7e
refactor: remove logging statements from ObjectSidAllowedObjectClasse…
029b428
refactor: streamline objectSid handling in AddRequest by removing con…
611479c
refactor: simplify _decode method in ObjectSidAllowedObjectClassesCac…
bc8a14f
refactor: add compute method for retrieving available object classes …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
64 changes: 64 additions & 0 deletions
64
app/ldap_protocol/rid_manager/objectsid_allowed_object_classes_cache.py
This file contains hidden or 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,64 @@ | ||
| """Redis cache for objectSid-related metadata. | ||
|
|
||
| Copyright (c) 2026 MultiFactor | ||
| License: https://github.com/MultiDirectoryLab/MultiDirectory/blob/main/LICENSE | ||
| """ | ||
|
|
||
| import json | ||
| from typing import Awaitable, Callable | ||
|
|
||
| from ldap_protocol.rid_manager.types import ObjectSidCacheRedisClient | ||
|
|
||
|
|
||
| class ObjectSidAllowedObjectClassesCache: | ||
| """Cache for ObjectClass names that allow `objectSid`.""" | ||
|
|
||
| _CACHE_KEY = "ldap:objectsid:allowed_object_classes" | ||
| _LOCK_KEY = "lock:ldap:objectsid:allowed_object_classes" | ||
| _LOCK_BLOCKING_TIMEOUT_SECONDS = 5 | ||
| _LOCK_LEASE_TIMEOUT_SECONDS = 30 | ||
|
|
||
| def __init__(self, redis: ObjectSidCacheRedisClient) -> None: | ||
| self._redis = redis | ||
|
|
||
| def _decode(self, raw: bytes | str) -> set[str] | None: | ||
| if isinstance(raw, bytes | bytearray): | ||
| raw = raw.decode("utf-8", errors="replace") | ||
| try: | ||
| decoded = json.loads(raw) | ||
| except json.JSONDecodeError: | ||
| return None | ||
|
|
||
| return {str(v).lower() for v in decoded} | ||
|
|
||
| async def get(self) -> set[str] | None: | ||
| raw = await self._redis.get(self._CACHE_KEY) | ||
| if not raw: | ||
| return None | ||
| return self._decode(raw) | ||
|
|
||
| async def store(self, value: set[str]) -> None: | ||
| normalized = sorted({v.lower() for v in value}) | ||
| await self._redis.set(self._CACHE_KEY, json.dumps(normalized)) | ||
|
|
||
| async def clear(self) -> None: | ||
| await self._redis.delete(self._CACHE_KEY, self._LOCK_KEY) | ||
|
|
||
| async def get_or_compute(self, compute: Callable[[], Awaitable[set[str]]]) -> set[str]: | ||
| """Read from redis, or compute once under a lock.""" | ||
| if cached := await self.get(): | ||
| return cached | ||
|
|
||
| lock = self._redis.lock( | ||
| name=self._LOCK_KEY, | ||
| blocking_timeout=self._LOCK_BLOCKING_TIMEOUT_SECONDS, | ||
| timeout=self._LOCK_LEASE_TIMEOUT_SECONDS, | ||
| ) | ||
| async with lock: | ||
| if cached2 := await self.get(): | ||
| return cached2 | ||
|
|
||
| value = await compute() | ||
| normalized = {v.lower() for v in value} | ||
| await self.store(normalized) | ||
| return normalized |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
давай
async def computeунесем внутрьObjectSidAllowedObjectClassesCacheдумаю тут нет смысла держать эту функцию
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.
обсудили голосом