Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utkarsh/issue 186 #189

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions pv_site_api/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from datetime import datetime, timedelta, timezone
from functools import wraps

from threading import Lock
import psutil
import structlog
from pvsite_datamodel.read.user import get_user_by_email
Expand All @@ -17,33 +17,47 @@
cache_time_seconds = int(os.getenv("CACHE_TIME_SECONDS", CACHE_TIME_SECONDS))
delete_cache_time_seconds = int(os.getenv("DELETE_CACHE_TIME_SECONDS", DELETE_CACHE_TIME_SECONDS))

cache_lock = Lock()

def remove_old_cache(
last_updated: dict, response: dict, remove_cache_time_seconds: float = delete_cache_time_seconds
):
"""
Remove old cache entries from the cache
last_updated: dict,
response: dict,
remove_cache_time_seconds: float = delete_cache_time_seconds
):
"""
Remove old cache entries from the cache

:param last_updated: dict of last updatedtimes
:param response: dict of responses, same keys as last_updated
:param remove_cache_time_seconds: the amount of time, after which the cache should be removed
"""

:param last_updated: dict of last updatedtimes
:param response: dict of responses, same keys as last_updated
:param remove_cache_time_seconds: the amount of time, after which the cache should be removed
"""
now = datetime.now(tz=timezone.utc)
logger.info("Removing old cache entries")
keys_to_remove = []
for key, value in last_updated.items():
if now - timedelta(seconds=remove_cache_time_seconds) > value:
logger.debug(f"Removing {key} from cache, ({value})")
keys_to_remove.append(key)

for key in keys_to_remove:
last_updated.pop(key)
response.pop(key)

process = psutil.Process(os.getpid())
logger.debug(f"Memory is {process.memory_info().rss / 10 ** 6} MB")

return last_updated, response
now = datetime.now(tz=timezone.utc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this identented correctly?

logger.info("Removing old cache entries")

"""
To check if cache removal is needed
"""
if not any(now - timedelta(seconds=remove_cache_time_seconds) > value for value in last_updated.values()):
logger.debug("No old cache entries to remove, skipping.")
return last_updated, response

keys_to_remove = []

with cache_lock:
for key, value in last_updated.items():
if now - timedelta(seconds=remove_cache_time_seconds) > value:
logger.debug(f"Removing {key} from cache, ({value})")
keys_to_remove.append(key)

for key in keys_to_remove:
last_updated.pop(key, None)
response.pop(key, None)

process = psutil.Process(os.getpid())
logger.debug(f"Memory is {process.memory_info().rss / 10 ** 6} MB")

return last_updated, response


def cache_response(func):
Expand Down
Loading