Skip to content

Commit

Permalink
add utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jul 26, 2024
1 parent 14390d1 commit 888ad16
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions onadata/libs/utils/cache_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,27 @@ def safe_cache_get(key, default=None):
# older Python versions
logger.exception(exc)
return default


def safe_cache_set(key, value, timeout=None):
"""
Safely set a value in the cache.
Args:
key (str): The cache key to set.
value (Any): The value to store in the cache.
timeout (int, optional): The cache timeout in seconds. If None,
the default cache timeout will be used.
Returns:
None
"""
try:
cache.set(key, value, timeout)
except ConnectionError as exc:
# Handle cache connection error
logger.exception(exc)
except socket.error as exc:
# Handle other potential connection errors, especially for
# older Python versions
logger.exception(exc)

0 comments on commit 888ad16

Please sign in to comment.