1
1
# SPDX-License-Identifier: Apache-2.0
2
- # Copyright 2023 Atlan Pte. Ltd.
3
- from threading import Lock
4
- from typing import Dict , Optional
2
+ # Copyright 2025 Atlan Pte. Ltd.
3
+ from __future__ import annotations
4
+
5
+ from threading import Lock , local
6
+ from typing import TYPE_CHECKING , Dict , Optional
5
7
6
- from pyatlan .client .typedef import TypeDefClient
7
8
from pyatlan .errors import ErrorCode
8
9
from pyatlan .model .enums import AtlanTypeCategory
9
10
from pyatlan .model .typedef import EnumDef
10
11
12
+ if TYPE_CHECKING :
13
+ from pyatlan .client .atlan import AtlanClient
14
+
11
15
lock : Lock = Lock ()
16
+ enum_cache_tls = local () # Thread-local storage (TLS)
12
17
13
18
14
19
class EnumCache :
15
20
"""
16
21
Lazily-loaded cache for accessing details of an enumeration.
17
22
"""
18
23
19
- caches : Dict [int , "EnumCache" ] = {}
24
+ def __init__ (self , client : AtlanClient ):
25
+ self .client : AtlanClient = client
26
+ self .cache_by_name : Dict [str , EnumDef ] = {}
27
+ self .lock : Lock = Lock ()
20
28
21
29
@classmethod
22
- def get_cache (cls ) -> " EnumCache" :
30
+ def get_cache (cls , client : Optional [ AtlanClient ] = None ) -> EnumCache :
23
31
from pyatlan .client .atlan import AtlanClient
24
32
25
33
with lock :
26
- client = AtlanClient .get_default_client ()
34
+ client = client or AtlanClient .get_default_client ()
27
35
cache_key = client .cache_key
28
- if cache_key not in cls .caches :
29
- cls .caches [cache_key ] = EnumCache (typedef_client = client .typedef )
30
- return cls .caches [cache_key ]
36
+
37
+ if not hasattr (enum_cache_tls , "caches" ):
38
+ enum_cache_tls .caches = {}
39
+
40
+ if cache_key not in enum_cache_tls .caches :
41
+ cache_instance = EnumCache (client = client )
42
+ cache_instance ._refresh_cache () # Refresh on new cache instance
43
+ enum_cache_tls .caches [cache_key ] = cache_instance
44
+
45
+ return enum_cache_tls .caches [cache_key ]
31
46
32
47
@classmethod
33
48
def refresh_cache (cls ) -> None :
@@ -49,17 +64,12 @@ def get_by_name(cls, name: str) -> EnumDef:
49
64
raise ErrorCode .ENUM_NOT_FOUND .exception_with_parameters (name )
50
65
return enum
51
66
52
- def __init__ (self , typedef_client : TypeDefClient ):
53
- self .typedef_client : TypeDefClient = typedef_client
54
- self .cache_by_name : Dict [str , EnumDef ] = {}
55
- self .lock : Lock = Lock ()
56
-
57
67
def _refresh_cache (self ) -> None :
58
68
"""
59
69
Refreshes the cache of enumerations by requesting the full set of enumerations from Atlan.
60
70
"""
61
71
with self .lock :
62
- response = self .typedef_client .get (type_category = AtlanTypeCategory .ENUM )
72
+ response = self .client . typedef .get (type_category = AtlanTypeCategory .ENUM )
63
73
if not response or not response .enum_defs :
64
74
raise ErrorCode .EXPIRED_API_TOKEN .exception_with_parameters ()
65
75
self .cache_by_name = {}
0 commit comments