Skip to content

Commit 575d729

Browse files
committed
[change] Bound GroupCache to AtlanClient, moved class var to TLS
1 parent 8199787 commit 575d729

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

pyatlan/cache/group_cache.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# SPDX-License-Identifier: Apache-2.0
2-
# Copyright 2022 Atlan Pte. Ltd.
3-
from threading import Lock
4-
from typing import Dict, Iterable, Optional
2+
# Copyright 2025 Atlan Pte. Ltd.
3+
from __future__ import annotations
54

6-
from pyatlan.client.group import GroupClient
5+
from threading import Lock, local
6+
from typing import TYPE_CHECKING, Dict, Iterable, Optional
7+
8+
if TYPE_CHECKING:
9+
from pyatlan.client.atlan import AtlanClient
710

811
lock: Lock = Lock()
12+
group_cache_tls = local() # Thread-local storage (TLS)
913

1014

1115
class GroupCache:
@@ -15,16 +19,30 @@ class GroupCache:
1519

1620
caches: Dict[int, "GroupCache"] = {}
1721

22+
def __init__(self, client: AtlanClient):
23+
self.client: AtlanClient = client
24+
self.map_id_to_name: Dict[str, str] = {}
25+
self.map_name_to_id: Dict[str, str] = {}
26+
self.map_alias_to_id: Dict[str, str] = {}
27+
self.lock: Lock = Lock()
28+
1829
@classmethod
19-
def get_cache(cls) -> "GroupCache":
30+
def get_cache(cls, client: Optional[AtlanClient] = None) -> GroupCache:
2031
from pyatlan.client.atlan import AtlanClient
2132

2233
with lock:
23-
client = AtlanClient.get_default_client()
34+
client = client or AtlanClient.get_default_client()
2435
cache_key = client.cache_key
25-
if cache_key not in cls.caches:
26-
cls.caches[cache_key] = GroupCache(group_client=client.group)
27-
return cls.caches[cache_key]
36+
37+
if not hasattr(group_cache_tls, "caches"):
38+
group_cache_tls.caches = {}
39+
40+
if cache_key not in group_cache_tls.caches:
41+
cache_instance = GroupCache(client=client)
42+
cache_instance._refresh_cache() # Refresh on new cache instance
43+
group_cache_tls.caches[cache_key] = cache_instance
44+
45+
return group_cache_tls.caches[cache_key]
2846

2947
@classmethod
3048
def get_id_for_name(cls, name: str) -> Optional[str]:
@@ -65,27 +83,21 @@ def validate_aliases(cls, aliases: Iterable[str]):
6583
"""
6684
return cls.get_cache()._validate_aliases(aliases)
6785

68-
def __init__(self, group_client: GroupClient):
69-
self.group_client: GroupClient = group_client
70-
self.map_id_to_name: Dict[str, str] = {}
71-
self.map_name_to_id: Dict[str, str] = {}
72-
self.map_alias_to_id: Dict[str, str] = {}
73-
self.lock: Lock = Lock()
74-
7586
def _refresh_cache(self) -> None:
7687
with self.lock:
77-
groups = self.group_client.get_all()
78-
if groups is not None:
79-
self.map_id_to_name = {}
80-
self.map_name_to_id = {}
81-
self.map_alias_to_id = {}
82-
for group in groups:
83-
group_id = str(group.id)
84-
group_name = str(group.name)
85-
group_alias = str(group.alias)
86-
self.map_id_to_name[group_id] = group_name
87-
self.map_name_to_id[group_name] = group_id
88-
self.map_alias_to_id[group_alias] = group_id
88+
groups = self.client.group.get_all()
89+
if not groups:
90+
return
91+
self.map_id_to_name = {}
92+
self.map_name_to_id = {}
93+
self.map_alias_to_id = {}
94+
for group in groups:
95+
group_id = str(group.id)
96+
group_name = str(group.name)
97+
group_alias = str(group.alias)
98+
self.map_id_to_name[group_id] = group_name
99+
self.map_name_to_id[group_name] = group_id
100+
self.map_alias_to_id[group_alias] = group_id
89101

90102
def _get_id_for_name(self, name: str) -> Optional[str]:
91103
"""

pyatlan/client/atlan.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pyatlan.cache.atlan_tag_cache import AtlanTagCache
4545
from pyatlan.cache.custom_metadata_cache import CustomMetadataCache
4646
from pyatlan.cache.enum_cache import EnumCache
47+
from pyatlan.cache.group_cache import GroupCache
4748
from pyatlan.client.admin import AdminClient
4849
from pyatlan.client.asset import A, AssetClient, IndexSearchResults, LineageListResults
4950
from pyatlan.client.audit import AuditClient
@@ -175,6 +176,7 @@ class AtlanClient(BaseSettings):
175176
_open_lineage_client: Optional[OpenLineageClient] = PrivateAttr(default=None)
176177
_atlan_tag_cache: Optional[AtlanTagCache] = PrivateAttr(default=None)
177178
_enum_cache: Optional[EnumCache] = PrivateAttr(default=None)
179+
_group_cache: Optional[GroupCache] = PrivateAttr(default=None)
178180
_custom_metadata_cache: Optional[CustomMetadataCache] = PrivateAttr(default=None)
179181

180182
class Config:
@@ -337,6 +339,12 @@ def enum_cache(self) -> EnumCache:
337339
self._enum_cache = EnumCache.get_cache(client=self)
338340
return self._enum_cache
339341

342+
@property
343+
def group_cache(self) -> GroupCache:
344+
if self._group_cache is None:
345+
self._group_cache = GroupCache.get_cache(client=self)
346+
return self._group_cache
347+
340348
@property
341349
def custom_metadata_cache(self) -> CustomMetadataCache:
342350
if self._custom_metadata_cache is None:

0 commit comments

Comments
 (0)