Skip to content

Commit

Permalink
Cache the lookup pepper in the HashingMetadataStore. (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre authored Dec 16, 2021
1 parent fa547f1 commit 347b9f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/475.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cache the lookup pepper in the `HashingMetadataStore`.
10 changes: 10 additions & 0 deletions sydent/db/hashing_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
class HashingMetadataStore:
def __init__(self, sydent: "Sydent") -> None:
self.sydent = sydent
self._cached_lookup_pepper: Optional[str] = None

def get_lookup_pepper(self) -> Optional[str]:
"""Return the value of the current lookup pepper from the db
:return: A pepper if it exists in the database, or None if one does
not exist
"""

if self._cached_lookup_pepper is not None:
return self._cached_lookup_pepper

cur = self.sydent.db.cursor()
res = cur.execute("select lookup_pepper from hashing_metadata")
# Annotation safety: lookup_pepper is marked as varchar(256) in the
Expand All @@ -52,6 +57,8 @@ def get_lookup_pepper(self) -> Optional[str]:
if isinstance(pepper, bytes):
pepper = pepper.decode("UTF-8")

self._cached_lookup_pepper = pepper

return pepper

def store_lookup_pepper(
Expand Down Expand Up @@ -84,6 +91,9 @@ def store_lookup_pepper(
# Commit the queued db transactions so that adding a new pepper and hashing is atomic
self.sydent.db.commit()

# Update the cached pepper (only once the transaction has committed successfully!)
self._cached_lookup_pepper = pepper

def _rehash_threepids(
self,
cur: Cursor,
Expand Down

0 comments on commit 347b9f5

Please sign in to comment.