Skip to content

Commit

Permalink
Fixed _get_matched_keys for ReadOnlyCachedModel
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravr committed Dec 23, 2023
1 parent a951338 commit 31bbb3e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apphelpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__author__ = """Scroll Tech"""
__email__ = "[email protected]"
__version__ = "0.92.0"
__version__ = "0.92.1"
38 changes: 19 additions & 19 deletions apphelpers/utilities/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.92.0
current_version = 0.92.1
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 31bbb3e

Please sign in to comment.