1
1
# 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
5
4
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
7
10
8
11
lock : Lock = Lock ()
12
+ group_cache_tls = local () # Thread-local storage (TLS)
9
13
10
14
11
15
class GroupCache :
@@ -15,16 +19,30 @@ class GroupCache:
15
19
16
20
caches : Dict [int , "GroupCache" ] = {}
17
21
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
+
18
29
@classmethod
19
- def get_cache (cls ) -> " GroupCache" :
30
+ def get_cache (cls , client : Optional [ AtlanClient ] = None ) -> GroupCache :
20
31
from pyatlan .client .atlan import AtlanClient
21
32
22
33
with lock :
23
- client = AtlanClient .get_default_client ()
34
+ client = client or AtlanClient .get_default_client ()
24
35
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 ]
28
46
29
47
@classmethod
30
48
def get_id_for_name (cls , name : str ) -> Optional [str ]:
@@ -65,27 +83,21 @@ def validate_aliases(cls, aliases: Iterable[str]):
65
83
"""
66
84
return cls .get_cache ()._validate_aliases (aliases )
67
85
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
-
75
86
def _refresh_cache (self ) -> None :
76
87
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
89
101
90
102
def _get_id_for_name (self , name : str ) -> Optional [str ]:
91
103
"""
0 commit comments