diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4fb31f3..6286c66 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ History ======= +0.92.1 (2023-12-24) +------------------- +* Fixed _get_matched_keys access for ReadOnlyCachedModel + 0.92.0 (2023-12-23) ------------------- * site_ctx implementation for FastAPI diff --git a/apphelpers/__init__.py b/apphelpers/__init__.py index d6c55bc..7dcdbd8 100644 --- a/apphelpers/__init__.py +++ b/apphelpers/__init__.py @@ -4,4 +4,4 @@ __author__ = """Scroll Tech""" __email__ = "engg@stck.me" -__version__ = "0.92.0" +__version__ = "0.92.1" diff --git a/apphelpers/utilities/caching.py b/apphelpers/utilities/caching.py index ac2f9f4..de30b00 100644 --- a/apphelpers/utilities/caching.py +++ b/apphelpers/utilities/caching.py @@ -21,31 +21,38 @@ class ReadOnlyCachedModel: key_fields: ClassVar[List[str]] @classmethod - def prefix_key(cls, data: dict) -> str: + def _prefix_key(cls, data: dict) -> str: key = cls.ns for _field in cls.key_fields: key += f":{data[_field]}" return key + @classmethod + def _get_matched_keys(cls, data: dict) -> List[str]: + pattern = cls.ns + for _field in cls.key_fields: + pattern += f':{data.get(_field, "*")}' + return cls.connection.keys(pattern) # type: ignore + @classmethod def get(cls, **data: Any) -> Optional[dict]: - key = cls.prefix_key(data) + key = cls._prefix_key(data) value: Any = cls.connection.get(key) return json.loads(value) if value else None @classmethod def exists(cls, **data: Any) -> bool: - key = cls.prefix_key(data) + key = cls._prefix_key(data) return cls.connection.exists(key) # type: ignore @classmethod def get_count(cls, **data: Any) -> int: - key: Any = cls.prefix_key(data) + key: Any = cls._prefix_key(data) count: Optional[Any] = cls.connection.get(key) return int(count) if count else 0 @classmethod - def count_matched_keys(cls, **data) -> int: + def count_matched_keys(cls, **data: Any) -> int: keys = cls._get_matched_keys(data) return len(keys) @@ -57,54 +64,47 @@ class ReadWriteCachedModel(ReadOnlyCachedModel): timeout: ClassVar[Optional[int]] = None - @classmethod - def _get_matched_keys(cls, data: dict) -> List[str]: - pattern = cls.ns - for _field in cls.key_fields: - pattern += f':{data.get(_field, "*")}' - return cls.connection.keys(pattern) # type: ignore - @classmethod def create(cls, **data: Any): - key = cls.prefix_key(data) + key = cls._prefix_key(data) cls.connection.set(key, json.dumps(data)) if cls.timeout: cls.connection.expire(key, cls.timeout) @classmethod def create_lookup(cls, **data: Any): - key = cls.prefix_key(data) + key = cls._prefix_key(data) cls.connection.set(key, 1) if cls.timeout: cls.connection.expire(key, cls.timeout) @classmethod def create_counter(cls, starting=1, **data: Any): - key = cls.prefix_key(data) + key = cls._prefix_key(data) cls.connection.set(key, starting) if cls.timeout: cls.connection.expire(key, cls.timeout) @classmethod def update(cls, **data): - # key = cls.prefix_key(data) + # key = cls._prefix_key(data) # NOTE: keepttl works only with Redis 6.0+ # cls.connection.set(key, json.dumps(data), keepttl=True) cls.create(**data) @classmethod def increment(cls, amount=1, **data): - key = cls.prefix_key(data) + key = cls._prefix_key(data) cls.connection.incr(key, amount) @classmethod def decrement(cls, amount=1, **data): - key = cls.prefix_key(data) + key = cls._prefix_key(data) cls.connection.decr(key, amount) @classmethod def delete(cls, **data): - key = cls.prefix_key(data) + key = cls._prefix_key(data) cls.connection.delete(key) @classmethod diff --git a/setup.cfg b/setup.cfg index c2eadfe..21d882f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.92.0 +current_version = 0.92.1 commit = True tag = True diff --git a/setup.py b/setup.py index e7212e8..3c31777 100644 --- a/setup.py +++ b/setup.py @@ -42,6 +42,6 @@ test_suite="tests", tests_require=test_requirements, url="https://github.com/scrolltech/apphelpers", - version="0.92.0", + version="0.92.1", zip_safe=False, )