Skip to content

Commit

Permalink
Fixes memoize with db hash key inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cevans87 committed Dec 12, 2019
1 parent 6a2fa27 commit e2bd385
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions atools/_memoize_decorator.py
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass, field
from datetime import timedelta
from functools import partial, wraps
from hashlib import sha256
import inspect
from pathlib import Path
from sqlite3 import connect, Connection
Expand Down Expand Up @@ -65,7 +66,7 @@ def __post_init__(self) -> None:
if self.db is not None:
self.db.execute(dedent(f'''
CREATE TABLE IF NOT EXISTS `{self.table_name}` (
k integer PRIMARY KEY,
k TEXT PRIMARY KEY,
t FLOAT,
e FLOAT,
v TEXT NOT NULL
Expand Down Expand Up @@ -152,7 +153,7 @@ def expire_one_memo(self) -> None:
elif self.size is not None and self.size < len(self.memos):
(k, _) = self.memos.popitem(last=False)
if (self.db is not None) and (k is not None):
self.db.execute(f"DELETE FROM `{self.table_name}` WHERE k = {k}")
self.db.execute(f"DELETE FROM `{self.table_name}` WHERE k = '{k}'")
self.db.commit()

def finalize_memo(self, memo: _Memo, key: int) -> Any:
Expand Down Expand Up @@ -206,7 +207,7 @@ async def decorator(*args, **kwargs) -> Any:
key[i] = await v
key = tuple(key)

key = hash(key)
key = sha256(str(key).encode()).hexdigest()

memo: _AsyncMemo = self.get_memo(key)

Expand Down Expand Up @@ -244,7 +245,7 @@ def decorator(*args, **kwargs):
else:
key = self.get_key(*args, **kwargs)

key = hash(key)
key = sha256(str(key).encode()).hexdigest()

with self._sync_lock:
memo: _SyncMemo = self.get_memo(key)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name='atools',
version='0.9.1',
version='0.9.2',
packages=find_packages(),
python_requires='>=3.6',
url='https://github.com/cevans87/atools',
Expand Down

0 comments on commit e2bd385

Please sign in to comment.