Skip to content

Commit 331925d

Browse files
committed
* add cache get attempt after getting the most recent version from the provider store
* move local version, last updated, and cache update into _get_most_recent_version to keep them inside the same lock acquisition
1 parent 98420e1 commit 331925d

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/dynamodb_encryption_sdk/material_providers/most_recent.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,6 @@ def _ttl_action(self):
197197

198198
return TtlActions.EXPIRED
199199

200-
def _set_most_recent_version(self, version):
201-
# type: (int) -> None
202-
"""Set the most recent version and update the last updated time.
203-
204-
:param int version: Version to set
205-
"""
206-
with self._lock:
207-
self._version = version
208-
self._last_updated = time.time()
209-
210200
def _get_max_version(self):
211201
# type: () -> int
212202
"""Ask the provider store for the most recent version of this material.
@@ -235,7 +225,7 @@ def _get_provider(self, version):
235225
raise AttributeError('No encryption materials available')
236226

237227
def _get_most_recent_version(self, allow_local):
238-
# type: (bool) -> Tuple[int, CryptographicMaterialsProvider]
228+
# type: (bool) -> CryptographicMaterialsProvider
239229
"""Get the most recent version of the provider.
240230
241231
If allowing local and we cannot obtain the lock, just return the most recent local
@@ -244,7 +234,7 @@ def _get_most_recent_version(self, allow_local):
244234
245235
:param bool allow_local: Should we allow returning the local version if we cannot obtain the lock?
246236
:returns: version and corresponding cryptographic materials provider
247-
:rtype: tuple containing int and CryptographicMaterialsProvider
237+
:rtype: CryptographicMaterialsProvider
248238
"""
249239
acquired = self._lock.acquire(blocking=not allow_local)
250240

@@ -257,13 +247,20 @@ def _get_most_recent_version(self, allow_local):
257247

258248
try:
259249
version = self._get_max_version()
260-
provider = self._get_provider(version)
250+
try:
251+
provider = self._cache.get(version)
252+
except KeyError:
253+
provider = self._get_provider(version)
261254
actual_version = self._provider_store.version_from_material_description(provider._material_description)
262255
# TODO: ^ should we promote material description from hidden?
256+
257+
self._version = version
258+
self._last_updated = time.time()
259+
self._cache.put(actual_version, provider)
263260
finally:
264261
self._lock.release()
265262

266-
return actual_version, provider
263+
return provider
267264

268265
def encryption_materials(self, encryption_context):
269266
# type: (EncryptionContext) -> CryptographicMaterials
@@ -288,10 +285,7 @@ def encryption_materials(self, encryption_context):
288285
# Block until we can acquire the lock.
289286
allow_local = False
290287

291-
actual_version, provider = self._get_most_recent_version(allow_local)
292-
293-
self._cache.put(actual_version, provider)
294-
self._set_most_recent_version(actual_version)
288+
provider = self._get_most_recent_version(allow_local)
295289

296290
return provider.encryption_materials(encryption_context)
297291

0 commit comments

Comments
 (0)