Skip to content

Commit

Permalink
Refactor error handling in CustomDjangoCache to handle multiple error…
Browse files Browse the repository at this point in the history
… types (#11808)
  • Loading branch information
thesujai authored Feb 23, 2024
1 parent 0f802b0 commit 017af53
Showing 1 changed file with 109 additions and 82 deletions.
191 changes: 109 additions & 82 deletions kolibri/deployment/default/custom_django_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ class CustomDjangoCache(DjangoCache):
https://github.com/grantjenks/python-diskcache/blob/v4.1.0/diskcache/djangocache.py
"""

ERRORS_TO_HANDLE = (sqlite3.OperationalError, AssertionError)

def try_execute(self, method_name, error_return_value, *args, **kwargs):
"""
Safely executes a method with error handling.
:param method_name --> (str): name of method to execute
:param error_return_value --> (any): value to return if error occur
:param *args: positional arguments for method
:param *kwargs: keyword arguments for method
:return: The return value of the executed method if successful,
otherwise returns error_return_value
"""
try:
method = getattr(super(CustomDjangoCache, self), method_name)
if method is None:
raise ValueError(
"{method_name} is not a valid method".format(
method_name=method_name
)
)
return method(*args, **kwargs)
except self.ERRORS_TO_HANDLE:
return error_return_value

def add(
self,
key,
Expand All @@ -23,12 +47,17 @@ def add(
tag=None,
retry=True,
):
try:
return super(CustomDjangoCache, self).add(
key, value, timeout, version, read, tag, retry
)
except sqlite3.OperationalError:
return False
return self.try_execute(
"add",
False,
key=key,
value=value,
timeout=timeout,
version=version,
read=read,
tag=tag,
retry=retry,
)

def has_key(self, key, version=None):
"""Returns True if the key is in the cache and has not expired.
Expand All @@ -38,12 +67,7 @@ def has_key(self, key, version=None):
:return: True if key is found
"""
try:
return super(CustomDjangoCache, self).has_key( # noqa: W601
key, version=version
)
except sqlite3.OperationalError:
return False
return self.try_execute("has_key", False, key=key, version=version)

def get(
self,
Expand All @@ -55,12 +79,17 @@ def get(
tag=False,
retry=False,
):
try:
return super(CustomDjangoCache, self).get(
key, default, version, read, expire_time, tag, retry
)
except sqlite3.OperationalError:
return None
return self.try_execute(
"get",
None,
key=key,
default=default,
version=version,
read=read,
expire_time=expire_time,
tag=tag,
retry=retry,
)

def set(
self,
Expand All @@ -72,95 +101,93 @@ def set(
tag=None,
retry=True,
):
try:
return super(CustomDjangoCache, self).set(
key, value, timeout, version, read, tag, retry
)
except sqlite3.OperationalError:
return False
return self.try_execute(
"set",
False,
key=key,
value=value,
timeout=timeout,
version=version,
read=read,
tag=tag,
retry=retry,
)

def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None, retry=True):
try:
return super(CustomDjangoCache, self).touch(key, timeout, version, retry)
except sqlite3.OperationalError:
return False
return self.try_execute(
"touch",
False,
key=key,
timeout=timeout,
version=version,
retry=retry,
)

def pop(
self, key, default=None, version=None, expire_time=False, tag=False, retry=True
):
try:
return super(CustomDjangoCache, self).pop(
key, default, version, expire_time, tag, retry
)
except sqlite3.OperationalError:
return None
return self.try_execute(
"pop",
None,
key=key,
default=default,
version=version,
expire_time=expire_time,
tag=tag,
retry=retry,
)

def delete(self, key, version=None, retry=True):
try:
super(CustomDjangoCache, self).delete(key, version, retry)
except sqlite3.OperationalError:
pass
self.try_execute(
"delete",
None,
key=key,
version=version,
retry=retry,
)

def incr(self, key, delta=1, version=None, default=None, retry=True):
try:
return super(CustomDjangoCache, self).incr(
key, delta, version, default, retry
)
except sqlite3.OperationalError:
return None
return self.try_execute(
"incr",
None,
key=key,
delta=delta,
version=version,
default=default,
retry=retry,
)

def decr(self, key, delta=1, version=None, default=None, retry=True):
try:
return super(CustomDjangoCache, self).decr(
key, delta, version, default, retry
)
except sqlite3.OperationalError:
return None
return self.try_execute(
"decr",
None,
key=key,
delta=delta,
version=version,
default=default,
retry=retry,
)

def expire(self, retry=False):
try:
return super(CustomDjangoCache, self).expire(retry)
except sqlite3.OperationalError:
return 0
return self.try_execute("expire", 0, retry=retry)

def stats(self, enable=True, reset=False):
try:
return super(CustomDjangoCache, self).stats(enable, reset)
except sqlite3.OperationalError:
return 0, 0
return self.try_execute("stats", (0, 0), enable=enable, reset=reset)

def create_tag_index(self):
try:
super(CustomDjangoCache, self).create_tag_index()
except sqlite3.OperationalError:
pass
return self.try_execute("create_tag_index", None)

def drop_tag_index(self):
try:
super(CustomDjangoCache, self).drop_tag_index()
except sqlite3.OperationalError:
pass
return self.try_execute("drop_tag_index", None)

def evict(self, tag):
try:
return super(CustomDjangoCache, self).evict(tag)
except sqlite3.OperationalError:
return 0
return self.try_execute("evict", 0, tag=tag)

def cull(self):
try:
return super(CustomDjangoCache, self).cull()
except sqlite3.OperationalError:
return 0
return self.try_execute("cull", 0)

def clear(self):
try:
return super(CustomDjangoCache, self).clear()
except sqlite3.OperationalError:
return 0
return self.try_execute("clear", 0)

def close(self, **kwargs):
try:
super(CustomDjangoCache, self).close(**kwargs)
except sqlite3.OperationalError:
pass
return self.try_execute("close", None, **kwargs)

0 comments on commit 017af53

Please sign in to comment.