@@ -197,16 +197,6 @@ def _ttl_action(self):
197
197
198
198
return TtlActions .EXPIRED
199
199
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
-
210
200
def _get_max_version (self ):
211
201
# type: () -> int
212
202
"""Ask the provider store for the most recent version of this material.
@@ -235,7 +225,7 @@ def _get_provider(self, version):
235
225
raise AttributeError ('No encryption materials available' )
236
226
237
227
def _get_most_recent_version (self , allow_local ):
238
- # type: (bool) -> Tuple[int, CryptographicMaterialsProvider]
228
+ # type: (bool) -> CryptographicMaterialsProvider
239
229
"""Get the most recent version of the provider.
240
230
241
231
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):
244
234
245
235
:param bool allow_local: Should we allow returning the local version if we cannot obtain the lock?
246
236
:returns: version and corresponding cryptographic materials provider
247
- :rtype: tuple containing int and CryptographicMaterialsProvider
237
+ :rtype: CryptographicMaterialsProvider
248
238
"""
249
239
acquired = self ._lock .acquire (blocking = not allow_local )
250
240
@@ -257,13 +247,20 @@ def _get_most_recent_version(self, allow_local):
257
247
258
248
try :
259
249
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 )
261
254
actual_version = self ._provider_store .version_from_material_description (provider ._material_description )
262
255
# 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 )
263
260
finally :
264
261
self ._lock .release ()
265
262
266
- return actual_version , provider
263
+ return provider
267
264
268
265
def encryption_materials (self , encryption_context ):
269
266
# type: (EncryptionContext) -> CryptographicMaterials
@@ -288,10 +285,7 @@ def encryption_materials(self, encryption_context):
288
285
# Block until we can acquire the lock.
289
286
allow_local = False
290
287
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 )
295
289
296
290
return provider .encryption_materials (encryption_context )
297
291
0 commit comments