|
46 | 46 | ) |
47 | 47 |
|
48 | 48 | from . import _base as base |
| 49 | +from ._base import _set_properties_cache |
49 | 50 | from . import documents |
50 | 51 | from .documents import ConnectionPolicy, DatabaseAccount |
51 | 52 | from ._constants import _Constants as Constants |
@@ -144,7 +145,7 @@ def __init__( |
144 | 145 |
|
145 | 146 | self.connection_policy = connection_policy or ConnectionPolicy() |
146 | 147 | self.partition_resolvers: Dict[str, RangePartitionResolver] = {} |
147 | | - self.partition_key_definition_cache: Dict[str, Any] = {} |
| 148 | + self.__container_properties_cache: Dict[str, Dict[str, Any]] = {} |
148 | 149 | self.default_headers: Dict[str, Any] = { |
149 | 150 | http_constants.HttpHeaders.CacheControl: "no-cache", |
150 | 151 | http_constants.HttpHeaders.Version: http_constants.Versions.CurrentVersion, |
@@ -231,6 +232,26 @@ def __init__( |
231 | 232 | self.session: Optional[_session.Session] = None |
232 | 233 | self._set_client_consistency_level(database_account, consistency_level) |
233 | 234 |
|
| 235 | + @property |
| 236 | + def _container_properties_cache(self) -> Dict[str, Dict[str, Any]]: |
| 237 | + """Gets the container properties cache from the client. |
| 238 | + :returns: the container properties cache for the client. |
| 239 | + :rtype: Dict[str, Dict[str, Any]]""" |
| 240 | + return self.__container_properties_cache |
| 241 | + |
| 242 | + def _set_container_properties_cache(self, container_link: str, properties: Optional[Dict[str, Any]]) -> None: |
| 243 | + """Sets the container properties cache for the specified container. |
| 244 | +
|
| 245 | + This will only update the properties cache for a specified container. |
| 246 | + :param container_link: The container link will be used as the key to cache the container properties. |
| 247 | + :type container_link: str |
| 248 | + :param properties: These are the container properties to cache. |
| 249 | + :type properties: Optional[Dict[str, Any]]""" |
| 250 | + if properties: |
| 251 | + self.__container_properties_cache[container_link] = properties |
| 252 | + else: |
| 253 | + self.__container_properties_cache[container_link] = {} |
| 254 | + |
234 | 255 | def _set_client_consistency_level( |
235 | 256 | self, |
236 | 257 | database_account: DatabaseAccount, |
@@ -1262,7 +1283,6 @@ def CreateItem( |
1262 | 1283 |
|
1263 | 1284 | if base.IsItemContainerLink(database_or_container_link): |
1264 | 1285 | options = self._AddPartitionKey(database_or_container_link, document, options) |
1265 | | - |
1266 | 1286 | return self.Create(document, path, "docs", collection_id, None, options, **kwargs) |
1267 | 1287 |
|
1268 | 1288 | def UpsertItem( |
@@ -3280,13 +3300,14 @@ def _UpdateSessionIfRequired( |
3280 | 3300 | self.session.update_session(response_result, response_headers) |
3281 | 3301 |
|
3282 | 3302 | def _get_partition_key_definition(self, collection_link: str) -> Optional[Dict[str, Any]]: |
3283 | | - partition_key_definition = None |
| 3303 | + partition_key_definition: Optional[Dict[str, Any]] |
3284 | 3304 | # If the document collection link is present in the cache, then use the cached partitionkey definition |
3285 | | - if collection_link in self.partition_key_definition_cache: |
3286 | | - partition_key_definition = self.partition_key_definition_cache.get(collection_link) |
| 3305 | + if collection_link in self.__container_properties_cache: |
| 3306 | + cached_container: Dict[str, Any] = self.__container_properties_cache.get(collection_link, {}) |
| 3307 | + partition_key_definition = cached_container.get("partitionKey") |
3287 | 3308 | # Else read the collection from backend and add it to the cache |
3288 | 3309 | else: |
3289 | | - collection = self.ReadContainer(collection_link) |
3290 | | - partition_key_definition = collection.get("partitionKey") |
3291 | | - self.partition_key_definition_cache[collection_link] = partition_key_definition |
| 3310 | + container = self.ReadContainer(collection_link) |
| 3311 | + partition_key_definition = container.get("partitionKey") |
| 3312 | + self.__container_properties_cache[collection_link] = _set_properties_cache(container) |
3292 | 3313 | return partition_key_definition |
0 commit comments