Skip to content

Commit

Permalink
perf: update cache timeout settings
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Nov 25, 2024
1 parent 444a9d5 commit eed339e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
33 changes: 24 additions & 9 deletions enterprise_access/apps/bffs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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.
Expand All @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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


Expand Down
1 change: 0 additions & 1 deletion enterprise_access/apps/bffs/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions enterprise_access/apps/content_metadata/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``,
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions enterprise_access/apps/subsidy_access_policy/customer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions enterprise_access/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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', '')
Expand All @@ -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 = ''
Expand Down

0 comments on commit eed339e

Please sign in to comment.