From eed339ee86dc9a2c3057d114b9a5fe0f800b23cd Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Mon, 25 Nov 2024 12:17:44 -0500 Subject: [PATCH] perf: update cache timeout settings --- enterprise_access/apps/bffs/api.py | 33 ++++++++++++++----- enterprise_access/apps/bffs/context.py | 1 - .../apps/content_metadata/api.py | 10 +++--- .../subsidy_access_policy/customer_api.py | 8 +++-- enterprise_access/settings/base.py | 16 ++++++--- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/enterprise_access/apps/bffs/api.py b/enterprise_access/apps/bffs/api.py index aa4b9195..d9a68ed5 100644 --- a/enterprise_access/apps/bffs/api.py +++ b/enterprise_access/apps/bffs/api.py @@ -37,7 +37,7 @@ def enterprise_course_enrollments_cache_key(enterprise_customer_uuid): return versioned_cache_key('get_enterprise_course_enrollments', enterprise_customer_uuid) -def get_and_cache_enterprise_customer_users(request, **kwargs): +def get_and_cache_enterprise_customer_users(request, timeout=settings.ENTERPRISE_USER_RECORD_CACHE_TIMEOUT, **kwargs): """ Retrieves and caches enterprise learner data. """ @@ -55,13 +55,14 @@ def get_and_cache_enterprise_customer_users(request, **kwargs): username=username, **kwargs, ) - TieredCache.set_all_tiers(cache_key, response_payload, settings.LMS_CLIENT_TIMEOUT) + TieredCache.set_all_tiers(cache_key, response_payload, timeout) return response_payload def get_and_cache_enterprise_customer( enterprise_customer_slug=None, enterprise_customer_uuid=None, + timeout=settings.ENTERPRISE_USER_RECORD_CACHE_TIMEOUT, ): """ Retrieves and caches enterprise customer data. @@ -83,11 +84,16 @@ def get_and_cache_enterprise_customer( enterprise_customer_uuid=enterprise_customer_uuid, enterprise_customer_slug=enterprise_customer_slug, ) - TieredCache.set_all_tiers(cache_key, response_payload, settings.LMS_CLIENT_TIMEOUT) + TieredCache.set_all_tiers(cache_key, response_payload, timeout) return response_payload -def get_and_cache_subscription_licenses_for_learner(request, enterprise_customer_uuid, **kwargs): +def get_and_cache_subscription_licenses_for_learner( + request, + enterprise_customer_uuid, + timeout=settings.SUBSCRIPTION_LICENSES_LEARNER_CACHE_TIMEOUT, + **kwargs +): """ Retrieves and caches subscription licenses for a learner. """ @@ -104,11 +110,15 @@ def get_and_cache_subscription_licenses_for_learner(request, enterprise_customer enterprise_customer_uuid=enterprise_customer_uuid, **kwargs, ) - TieredCache.set_all_tiers(cache_key, response_payload, settings.LICENSE_MANAGER_CLIENT_TIMEOUT) + TieredCache.set_all_tiers(cache_key, response_payload, timeout) return response_payload -def get_and_cache_default_enterprise_enrollment_intentions(request, enterprise_customer_uuid): +def get_and_cache_default_enterprise_enrollment_intentions( + request, + enterprise_customer_uuid, + timeout=settings.DEFAULT_ENTERPRISE_ENROLLMENT_INTENTIONS_CACHE_TIMEOUT, +): """ Retrieves and caches default enterprise enrollment intentions for a learner. """ @@ -125,11 +135,16 @@ def get_and_cache_default_enterprise_enrollment_intentions(request, enterprise_c response_payload = client.get_default_enterprise_enrollment_intentions_learner_status( enterprise_customer_uuid=enterprise_customer_uuid, ) - TieredCache.set_all_tiers(cache_key, response_payload, settings.LMS_CLIENT_TIMEOUT) + TieredCache.set_all_tiers(cache_key, response_payload, timeout) return response_payload -def get_and_cache_enterprise_course_enrollments(request, enterprise_customer_uuid, **kwargs): +def get_and_cache_enterprise_course_enrollments( + request, + enterprise_customer_uuid, + timeout=settings.ENTERPRISE_COURSE_ENROLLMENTS_CACHE_TIMEOUT, + **kwargs +): """ Retrieves and caches enterprise course enrollments for a learner. """ @@ -146,7 +161,7 @@ def get_and_cache_enterprise_course_enrollments(request, enterprise_customer_uui enterprise_customer_uuid=enterprise_customer_uuid, **kwargs, ) - TieredCache.set_all_tiers(cache_key, response_payload, settings.LMS_CLIENT_TIMEOUT) + TieredCache.set_all_tiers(cache_key, response_payload, timeout) return response_payload diff --git a/enterprise_access/apps/bffs/context.py b/enterprise_access/apps/bffs/context.py index 79922e7c..4dedfaa3 100644 --- a/enterprise_access/apps/bffs/context.py +++ b/enterprise_access/apps/bffs/context.py @@ -5,7 +5,6 @@ from rest_framework import status -from enterprise_access.apps.api_client.lms_client import LmsApiClient from enterprise_access.apps.bffs import serializers from enterprise_access.apps.bffs.api import ( get_and_cache_enterprise_customer_users, diff --git a/enterprise_access/apps/content_metadata/api.py b/enterprise_access/apps/content_metadata/api.py index f4ee7d2f..ea9a5513 100644 --- a/enterprise_access/apps/content_metadata/api.py +++ b/enterprise_access/apps/content_metadata/api.py @@ -15,10 +15,12 @@ logger = logging.getLogger(__name__) -DEFAULT_CACHE_TIMEOUT = getattr(settings, 'CONTENT_METADATA_CACHE_TIMEOUT', 60 * 5) - -def get_and_cache_catalog_content_metadata(enterprise_catalog_uuid, content_keys, timeout=None): +def get_and_cache_catalog_content_metadata( + enterprise_catalog_uuid, + content_keys, + timeout=settings.CONTENT_METADATA_CACHE_TIMEOUT, +): """ Returns the metadata corresponding to the requested ``content_keys`` within the provided ``enterprise_catalog_uuid``, @@ -70,7 +72,7 @@ def get_and_cache_catalog_content_metadata(enterprise_catalog_uuid, content_keys cache_key = cache_keys_by_content_key.get(fetched_record.get('key')) content_metadata_to_cache[cache_key] = fetched_record - cache.set_many(content_metadata_to_cache, timeout or DEFAULT_CACHE_TIMEOUT) + cache.set_many(content_metadata_to_cache, timeout) # Add to our results list everything we just had to fetch metadata_results_list.extend(fetched_metadata) diff --git a/enterprise_access/apps/subsidy_access_policy/customer_api.py b/enterprise_access/apps/subsidy_access_policy/customer_api.py index 0949b52b..bc752844 100644 --- a/enterprise_access/apps/subsidy_access_policy/customer_api.py +++ b/enterprise_access/apps/subsidy_access_policy/customer_api.py @@ -13,10 +13,12 @@ logger = logging.getLogger(__name__) -DEFAULT_CACHE_TIMEOUT = settings.ENTERPRISE_USER_RECORD_CACHE_TIMEOUT - -def get_and_cache_enterprise_learner_record(enterprise_customer_uuid, learner_id, timeout=DEFAULT_CACHE_TIMEOUT): +def get_and_cache_enterprise_learner_record( + enterprise_customer_uuid, + learner_id, + timeout=settings.ENTERPRISE_USER_RECORD_CACHE_TIMEOUT, +): """ Fetches the enterprise learner record from the Lms client if it exists. Uses the `learner_id` and `enterprise_customer_uuid` to determine if diff --git a/enterprise_access/settings/base.py b/enterprise_access/settings/base.py index 175efc02..ea84b161 100644 --- a/enterprise_access/settings/base.py +++ b/enterprise_access/settings/base.py @@ -467,6 +467,7 @@ def root(*path_fragments): ENTERPRISE_SUBSIDY_URL = '' ENTERPRISE_ACCESS_URL = '' +# API Client timeouts LICENSE_MANAGER_CLIENT_TIMEOUT = os.environ.get('LICENSE_MANAGER_CLIENT_TIMEOUT', 45) LMS_CLIENT_TIMEOUT = os.environ.get('LMS_CLIENT_TIMEOUT', 45) ECOMMERCE_CLIENT_TIMEOUT = os.environ.get('ECOMMERCE_CLIENT_TIMEOUT', 45) @@ -487,6 +488,7 @@ def root(*path_fragments): BRAZE_ASSIGNMENT_CANCELLED_NOTIFICATION_CAMPAIGN = '' BRAZE_ASSIGNMENT_AUTOMATIC_CANCELLATION_NOTIFICATION_CAMPAIGN = '' +# Braze configuration BRAZE_API_URL = '' BRAZE_API_KEY = os.environ.get('BRAZE_API_KEY', '') BRAZE_APP_ID = os.environ.get('BRAZE_APP_ID', '') @@ -505,11 +507,15 @@ def root(*path_fragments): SIMPLE_HISTORY_DATE_INDEX = False # Cache timeouts -SUBSIDY_RECORD_CACHE_TIMEOUT = 60 * 5 -ENTERPRISE_USER_RECORD_CACHE_TIMEOUT = 60 * 5 -SUBSIDY_AGGREGATES_CACHE_TIMEOUT = 60 * 10 - -ALL_ENTERPRISE_GROUP_MEMBERS_CACHE_TIMEOUT = 60 * 5 +DEFAULT_CACHE_TIMEOUT = 60 * 5 # 5 minutes +CONTENT_METADATA_CACHE_TIMEOUT = 60 * 30 # 30 minutes +ENTERPRISE_USER_RECORD_CACHE_TIMEOUT = 60 * 10 # 10 minutes +SUBSIDY_AGGREGATES_CACHE_TIMEOUT = 60 * 10 # 10 minutes +SUBSCRIPTION_LICENSES_LEARNER_CACHE_TIMEOUT = 60 * 2 # 2 minutes +SUBSIDY_RECORD_CACHE_TIMEOUT = DEFAULT_CACHE_TIMEOUT +DEFAULT_ENTERPRISE_ENROLLMENT_INTENTIONS_CACHE_TIMEOUT = DEFAULT_CACHE_TIMEOUT +ENTERPRISE_COURSE_ENROLLMENTS_CACHE_TIMEOUT = DEFAULT_CACHE_TIMEOUT +ALL_ENTERPRISE_GROUP_MEMBERS_CACHE_TIMEOUT = DEFAULT_CACHE_TIMEOUT BRAZE_GROUP_EMAIL_FORCE_REMIND_ALL_PENDING_LEARNERS = False BRAZE_GROUPS_EMAIL_AUTO_REMINDER_DAY_5_CAMPAIGN = ''