22
33import logging
44from asyncio import Lock
5+ from functools import cached_property
56from logging import getLogger
67from typing import TYPE_CHECKING , ClassVar , Literal , overload
78
@@ -139,7 +140,6 @@ def __init__(
139140 self ._storage_type = storage_type
140141 self ._alias = alias
141142 self ._configuration = configuration
142- self ._additional_cache_key = hash_api_base_url_and_token (configuration )
143143
144144 async def __aenter__ (self ) -> AliasResolver :
145145 """Context manager to prevent race condition in alias creation."""
@@ -183,15 +183,7 @@ async def _get_alias_map(cls, configuration: Configuration) -> dict[str, str]:
183183 default_kvs_client = await cls ._get_default_kvs_client (configuration )
184184
185185 record = await default_kvs_client .get_record (cls ._ALIAS_MAPPING_KEY )
186-
187- # get_record can return {key: ..., value: ..., content_type: ...}
188- if isinstance (record , dict ):
189- if 'value' in record and isinstance (record ['value' ], dict ):
190- cls ._alias_map = record ['value' ]
191- else :
192- cls ._alias_map = record
193- else :
194- cls ._alias_map = dict [str , str ]()
186+ cls ._alias_map = record .get ('value' , {}) if record else {}
195187
196188 return cls ._alias_map
197189
@@ -201,6 +193,18 @@ async def resolve_id(self) -> str | None:
201193 Returns:
202194 Storage id if it exists, None otherwise.
203195 """
196+ # First try to find the alias in the configuration mapping to avoid any API calls.
197+ # This mapping is maintained by the Apify platform and does not have to be maintained in the default KVS.
198+ if self ._configuration .actor_storages and self ._alias != 'default' :
199+ storage_maps = {
200+ 'Dataset' : self ._configuration .actor_storages ['datasets' ],
201+ 'KeyValueStore' : self ._configuration .actor_storages ['key_value_stores' ],
202+ 'RequestQueue' : self ._configuration .actor_storages ['request_queues' ],
203+ }
204+ if storage_id := storage_maps .get (self ._storage_type , {}).get (self ._alias ):
205+ return storage_id
206+
207+ # Fallback to the mapping saved in the default KVS
204208 return (await self ._get_alias_map (self ._configuration )).get (self ._storage_key , None )
205209
206210 async def store_mapping (self , storage_id : str ) -> None :
@@ -220,30 +224,22 @@ async def store_mapping(self, storage_id: str) -> None:
220224
221225 try :
222226 record = await default_kvs_client .get_record (self ._ALIAS_MAPPING_KEY )
223-
224- # get_record can return {key: ..., value: ..., content_type: ...}
225- if isinstance (record , dict ) and 'value' in record :
226- record = record ['value' ]
227-
228- # Update or create the record with the new alias mapping
229- if isinstance (record , dict ):
230- record [self ._storage_key ] = storage_id
231- else :
232- record = {self ._storage_key : storage_id }
227+ value = record .get ('value' , {}) if record else {}
228+ value [self ._storage_key ] = storage_id
233229
234230 # Store the mapping back in the KVS.
235- await default_kvs_client .set_record (self ._ALIAS_MAPPING_KEY , record )
231+ await default_kvs_client .set_record (key = self ._ALIAS_MAPPING_KEY , value = value )
236232 except Exception as exc :
237233 logger .warning (f'Error storing alias mapping for { self ._alias } : { exc } ' )
238234
239- @property
235+ @cached_property
240236 def _storage_key (self ) -> str :
241237 """Get a unique storage key used for storing the alias in the mapping."""
242238 return self ._ALIAS_STORAGE_KEY_SEPARATOR .join (
243239 [
244240 self ._storage_type ,
245241 self ._alias ,
246- self ._additional_cache_key ,
242+ hash_api_base_url_and_token ( self ._configuration ) ,
247243 ]
248244 )
249245
0 commit comments