Skip to content

Commit

Permalink
fix: update cache keys to be user-specific
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Nov 26, 2024
1 parent eed339e commit e7975c7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion enterprise_access/apps/api/v1/tests/test_bff_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def setUp(self):
)
@ddt.unpack
@mock_dashboard_dependencies
def test_dashboard_empty_state(
def test_dashboard_empty_state_with_permissions(
self,
mock_get_enterprise_customers_for_user,
mock_get_subscription_licenses_for_learner,
Expand Down
42 changes: 26 additions & 16 deletions enterprise_access/apps/bffs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ def enterprise_customer_cache_key(enterprise_customer_slug, enterprise_customer_
return versioned_cache_key('enterprise_customer', enterprise_customer_slug, enterprise_customer_uuid)


def subscription_licenses_cache_key(enterprise_customer_uuid):
return versioned_cache_key('get_subscription_licenses_for_learner', enterprise_customer_uuid)
def subscription_licenses_cache_key(enterprise_customer_uuid, lms_user_id):
return versioned_cache_key('get_subscription_licenses_for_learner', enterprise_customer_uuid, lms_user_id)


def default_enterprise_enrollment_intentions_cache_key(enterprise_customer_uuid):
return versioned_cache_key('get_default_enterprise_enrollment_intentions', enterprise_customer_uuid)
def default_enterprise_enrollment_intentions_learner_status_cache_key(enterprise_customer_uuid, lms_user_id):
return versioned_cache_key(
'get_default_enterprise_enrollment_intentions_learner_status',
enterprise_customer_uuid,
lms_user_id
)


def enterprise_course_enrollments_cache_key(enterprise_customer_uuid):
return versioned_cache_key('get_enterprise_course_enrollments', enterprise_customer_uuid)
def enterprise_course_enrollments_cache_key(enterprise_customer_uuid, lms_user_id):
return versioned_cache_key('get_enterprise_course_enrollments', enterprise_customer_uuid, lms_user_id)


def get_and_cache_enterprise_customer_users(request, timeout=settings.ENTERPRISE_USER_RECORD_CACHE_TIMEOUT, **kwargs):
Expand Down Expand Up @@ -97,7 +101,7 @@ def get_and_cache_subscription_licenses_for_learner(
"""
Retrieves and caches subscription licenses for a learner.
"""
cache_key = subscription_licenses_cache_key(enterprise_customer_uuid)
cache_key = subscription_licenses_cache_key(enterprise_customer_uuid, request.user.id)
cached_response = TieredCache.get_cached_response(cache_key)
if cached_response.is_found:
logger.info(
Expand All @@ -114,15 +118,18 @@ def get_and_cache_subscription_licenses_for_learner(
return response_payload


def get_and_cache_default_enterprise_enrollment_intentions(
def get_and_cache_default_enterprise_enrollment_intentions_learner_status(
request,
enterprise_customer_uuid,
timeout=settings.DEFAULT_ENTERPRISE_ENROLLMENT_INTENTIONS_CACHE_TIMEOUT,
):
"""
Retrieves and caches default enterprise enrollment intentions for a learner.
"""
cache_key = default_enterprise_enrollment_intentions_cache_key(enterprise_customer_uuid)
cache_key = default_enterprise_enrollment_intentions_learner_status_cache_key(
enterprise_customer_uuid,
request.user.id,
)
cached_response = TieredCache.get_cached_response(cache_key)
if cached_response.is_found:
logger.info(
Expand All @@ -148,7 +155,7 @@ def get_and_cache_enterprise_course_enrollments(
"""
Retrieves and caches enterprise course enrollments for a learner.
"""
cache_key = enterprise_course_enrollments_cache_key(enterprise_customer_uuid)
cache_key = enterprise_course_enrollments_cache_key(enterprise_customer_uuid, request.user.id)
cached_response = TieredCache.get_cached_response(cache_key)
if cached_response.is_found:
logger.info(
Expand All @@ -165,27 +172,30 @@ def get_and_cache_enterprise_course_enrollments(
return response_payload


def invalidate_default_enterprise_enrollment_intentions_cache(enterprise_customer_uuid):
def invalidate_default_enterprise_enrollment_intentions_learner_status_cache(enterprise_customer_uuid, lms_user_id):
"""
Invalidates the default enterprise enrollment intentions cache for a learner.
"""
cache_key = default_enterprise_enrollment_intentions_cache_key(enterprise_customer_uuid)
cache_key = default_enterprise_enrollment_intentions_learner_status_cache_key(
enterprise_customer_uuid,
lms_user_id,
)
TieredCache.delete_all_tiers(cache_key)


def invalidate_enterprise_course_enrollments_cache(enterprise_customer_uuid):
def invalidate_enterprise_course_enrollments_cache(enterprise_customer_uuid, lms_user_id):
"""
Invalidates the enterprise course enrollments cache for a learner.
"""
cache_key = enterprise_course_enrollments_cache_key(enterprise_customer_uuid)
cache_key = enterprise_course_enrollments_cache_key(enterprise_customer_uuid, lms_user_id)
TieredCache.delete_all_tiers(cache_key)


def invalidate_subscription_licenses_cache(enterprise_customer_uuid):
def invalidate_subscription_licenses_cache(enterprise_customer_uuid, lms_user_id):
"""
Invalidates the subscription licenses cache for a learner.
"""
cache_key = subscription_licenses_cache_key(enterprise_customer_uuid)
cache_key = subscription_licenses_cache_key(enterprise_customer_uuid, lms_user_id)
TieredCache.delete_all_tiers(cache_key)


Expand Down
27 changes: 16 additions & 11 deletions enterprise_access/apps/bffs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from enterprise_access.apps.api_client.license_manager_client import LicenseManagerUserApiClient
from enterprise_access.apps.bffs.api import (
get_and_cache_default_enterprise_enrollment_intentions,
get_and_cache_default_enterprise_enrollment_intentions_learner_status,
get_and_cache_enterprise_course_enrollments,
get_and_cache_subscription_licenses_for_learner,
invalidate_default_enterprise_enrollment_intentions_cache,
invalidate_default_enterprise_enrollment_intentions_learner_status_cache,
invalidate_enterprise_course_enrollments_cache,
invalidate_subscription_licenses_cache
)
Expand Down Expand Up @@ -300,7 +300,8 @@ def check_and_activate_assigned_license(self):
# Invalidate the subscription licenses cache as the cached data changed
# with the now-activated license.
invalidate_subscription_licenses_cache(
enterprise_customer_uuid=self.context.enterprise_customer_uuid
enterprise_customer_uuid=self.context.enterprise_customer_uuid,
lms_user_id=self.context.lms_user_id,
)
except Exception as e: # pylint: disable=broad-exception-caught
logger.exception(f"Error activating license {subscription_license.get('uuid')}")
Expand Down Expand Up @@ -388,7 +389,8 @@ def check_and_auto_apply_license(self):
)
# Invalidate the subscription licenses cache as the cached data changed with the auto-applied license.
invalidate_subscription_licenses_cache(
enterprise_customer_uuid=self.context.enterprise_customer_uuid
enterprise_customer_uuid=self.context.enterprise_customer_uuid,
lms_user_id=self.context.lms_user_id,
)
# Update the context with the auto-applied license data
transformed_auto_applied_licenses = self.transform_subscription_licenses([auto_applied_license])
Expand All @@ -410,10 +412,11 @@ def load_default_enterprise_enrollment_intentions(self):
Load default enterprise course enrollments (stubbed)
"""
try:
default_enterprise_enrollment_intentions = get_and_cache_default_enterprise_enrollment_intentions(
request=self.context.request,
enterprise_customer_uuid=self.context.enterprise_customer_uuid,
)
default_enterprise_enrollment_intentions =\
get_and_cache_default_enterprise_enrollment_intentions_learner_status(
request=self.context.request,
enterprise_customer_uuid=self.context.enterprise_customer_uuid,
)
self.context.data['default_enterprise_enrollment_intentions'] = default_enterprise_enrollment_intentions
except Exception as e: # pylint: disable=broad-exception-caught
logger.exception("Error loading default enterprise courses")
Expand Down Expand Up @@ -462,11 +465,13 @@ def enroll_in_redeemable_default_enterprise_enrollment_intentions(self):

# Invalidate the default enterprise enrollment intentions and enterprise course enrollments cache
# as the previously redeemable enrollment intentions have been processed/enrolled.
invalidate_default_enterprise_enrollment_intentions_cache(
enterprise_customer_uuid=self.context.enterprise_customer_uuid
invalidate_default_enterprise_enrollment_intentions_learner_status_cache(
enterprise_customer_uuid=self.context.enterprise_customer_uuid,
lms_user_id=self.context.lms_user_id,
)
invalidate_enterprise_course_enrollments_cache(
enterprise_customer_uuid=self.context.enterprise_customer_uuid
enterprise_customer_uuid=self.context.enterprise_customer_uuid,
lms_user_id=self.context.lms_user_id,
)


Expand Down

0 comments on commit e7975c7

Please sign in to comment.