Skip to content

Commit 9df8cc5

Browse files
committed
[change] Bound SourceTagCache to AtlanClient, moved class var to TLS
1 parent a97ed4c commit 9df8cc5

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

pyatlan/cache/connection_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
if TYPE_CHECKING:
1717
from pyatlan.client.atlan import AtlanClient
18-
LOGGER = logging.getLogger(__name__)
1918

2019
lock = threading.Lock()
2120
connection_cache_tls = local() # Thread-local storage (TLS)
21+
LOGGER = logging.getLogger(__name__)
2222

2323

2424
class ConnectionCache(AbstractAssetCache):

pyatlan/cache/source_tag_cache.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
# SPDX-License-Identifier: Apache-2.0
2-
# Copyright 2024 Atlan Pte. Ltd.
2+
# Copyright 2025 Atlan Pte. Ltd.
33
from __future__ import annotations
44

55
import logging
66
import threading
7-
from typing import Dict, Union
7+
from threading import local
8+
from typing import TYPE_CHECKING, Optional, Union
89

910
from pyatlan.cache.abstract_asset_cache import AbstractAssetCache, AbstractAssetName
1011
from pyatlan.cache.connection_cache import ConnectionCache, ConnectionName
11-
from pyatlan.client.atlan import AtlanClient
1212
from pyatlan.errors import AtlanError
1313
from pyatlan.model.assets import Asset, Tag
1414
from pyatlan.model.fluent_search import FluentSearch
1515
from pyatlan.model.search import Term
1616

17-
LOGGER = logging.getLogger(__name__)
17+
if TYPE_CHECKING:
18+
from pyatlan.client.atlan import AtlanClient
1819

1920
lock = threading.Lock()
21+
source_tag_cache_tls = local() # Thread-local storage (TLS)
22+
LOGGER = logging.getLogger(__name__)
2023

2124

2225
class SourceTagCache(AbstractAssetCache):
@@ -34,21 +37,26 @@ class SourceTagCache(AbstractAssetCache):
3437

3538
_SEARCH_FIELDS = [Asset.NAME]
3639
SEARCH_ATTRIBUTES = [field.atlan_field_name for field in _SEARCH_FIELDS]
37-
caches: Dict[int, SourceTagCache] = dict()
3840

3941
def __init__(self, client: AtlanClient):
4042
super().__init__(client)
4143

4244
@classmethod
43-
def get_cache(cls) -> SourceTagCache:
45+
def get_cache(cls, client: Optional[AtlanClient] = None) -> SourceTagCache:
4446
from pyatlan.client.atlan import AtlanClient
4547

4648
with lock:
47-
default_client = AtlanClient.get_default_client()
48-
cache_key = default_client.cache_key
49-
if cache_key not in cls.caches:
50-
cls.caches[cache_key] = SourceTagCache(client=default_client)
51-
return cls.caches[cache_key]
49+
client = client or AtlanClient.get_default_client()
50+
cache_key = client.cache_key
51+
52+
if not hasattr(source_tag_cache_tls, "caches"):
53+
source_tag_cache_tls.caches = {}
54+
55+
if cache_key not in source_tag_cache_tls.caches:
56+
cache_instance = SourceTagCache(client=client)
57+
source_tag_cache_tls.caches[cache_key] = cache_instance
58+
59+
return source_tag_cache_tls.caches[cache_key]
5260

5361
@classmethod
5462
def get_by_guid(cls, guid: str, allow_refresh: bool = True) -> Tag:

pyatlan/client/atlan.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from pyatlan.cache.enum_cache import EnumCache
4848
from pyatlan.cache.group_cache import GroupCache
4949
from pyatlan.cache.role_cache import RoleCache
50+
from pyatlan.cache.source_tag_cache import SourceTagCache
5051
from pyatlan.cache.user_cache import UserCache
5152
from pyatlan.client.admin import AdminClient
5253
from pyatlan.client.asset import A, AssetClient, IndexSearchResults, LineageListResults
@@ -184,6 +185,7 @@ class AtlanClient(BaseSettings):
184185
_user_cache: Optional[UserCache] = PrivateAttr(default=None)
185186
_custom_metadata_cache: Optional[CustomMetadataCache] = PrivateAttr(default=None)
186187
_connection_cache: Optional[ConnectionCache] = PrivateAttr(default=None)
188+
_source_tag_cache: Optional[SourceTagCache] = PrivateAttr(default=None)
187189

188190
class Config:
189191
env_prefix = "atlan_"
@@ -375,6 +377,12 @@ def connection_cache(self) -> ConnectionCache:
375377
self._connection_cache = ConnectionCache.get_cache(client=self)
376378
return self._connection_cache
377379

380+
@property
381+
def source_tag_cache(self) -> SourceTagCache:
382+
if self._source_tag_cache is None:
383+
self._source_tag_cache = SourceTagCache.get_cache(client=self)
384+
return self._source_tag_cache
385+
378386
def update_headers(self, header: Dict[str, str]):
379387
self._session.headers.update(header)
380388

0 commit comments

Comments
 (0)