Skip to content

Commit

Permalink
Make config cache thread safe
Browse files Browse the repository at this point in the history
cachetools cache is not thread safe and there were frequent exceptions
logged indicating that cache updates during async calls were failing
with key errors similar to those described in:

tkem/cachetools#80

Add a lock per table instance synchronizes cache updates across threads
in.
  • Loading branch information
mmcfarland committed Oct 24, 2022
1 parent 9613098 commit 61b4bf3
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pccommon/pccommon/tables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from threading import Lock
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(
self._service_client: Optional[TableServiceClient] = None
self._table_client: Optional[TableClient] = None
self._cache: Cache = TTLCache(maxsize=1024, ttl=ttl or DEFAULT_TTL)
self._cache_lock: Lock = Lock()

def _ensure_table_client(self) -> None:
if not self._table_client:
Expand Down Expand Up @@ -187,7 +189,7 @@ def update(self, partition_key: str, row_key: str, entity: M) -> None:
}
)

@cachedmethod(cache=lambda self: self._cache)
@cachedmethod(cache=lambda self: self._cache, lock=lambda self: self._cache_lock)
def get(self, partition_key: str, row_key: str) -> Optional[M]:
with self as table_client:
try:
Expand Down

0 comments on commit 61b4bf3

Please sign in to comment.