From bc358b89dee73d2a1b8e046f5b79141472e20e83 Mon Sep 17 00:00:00 2001 From: Waheed Ahmed Date: Thu, 31 May 2018 01:22:17 +0500 Subject: [PATCH] Add program UUID in cache key instead passing as resource id. LEARNER-5373 --- .../core/djangoapps/credentials/tests/test_utils.py | 8 +++++--- openedx/core/djangoapps/credentials/utils.py | 11 ++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/openedx/core/djangoapps/credentials/tests/test_utils.py b/openedx/core/djangoapps/credentials/tests/test_utils.py index b23120d1df..ef3b18ed92 100644 --- a/openedx/core/djangoapps/credentials/tests/test_utils.py +++ b/openedx/core/djangoapps/credentials/tests/test_utils.py @@ -28,7 +28,7 @@ class TestGetCredentials(CredentialsApiConfigMixin, CacheIsolationTestCase): ClientFactory(name=CredentialsApiConfig.OAUTH2_CLIENT_NAME, client_type=CONFIDENTIAL) - self.create_credentials_config() + self.credentials_config = self.create_credentials_config(cache_ttl=1) self.user = UserFactory() def test_get_many(self, mock_get_edx_api_data): @@ -45,8 +45,9 @@ class TestGetCredentials(CredentialsApiConfigMixin, CacheIsolationTestCase): 'username': self.user.username, 'status': 'awarded', } + cache_key = '{}.{}'.format(self.credentials_config.CACHE_KEY, self.user.username) self.assertEqual(kwargs['querystring'], querystring) - self.assertIsNone(kwargs['resource_id']) + self.assertEqual(kwargs['cache_key'], cache_key) self.assertEqual(actual, expected) @@ -66,7 +67,8 @@ class TestGetCredentials(CredentialsApiConfigMixin, CacheIsolationTestCase): 'status': 'awarded', 'program_uuid': program_uuid, } + cache_key = '{}.{}.{}'.format(self.credentials_config.CACHE_KEY, self.user.username, program_uuid) self.assertEqual(kwargs['querystring'], querystring) - self.assertEqual(kwargs['resource_id'], program_uuid) + self.assertEqual(kwargs['cache_key'], cache_key) self.assertEqual(actual, expected) diff --git a/openedx/core/djangoapps/credentials/utils.py b/openedx/core/djangoapps/credentials/utils.py index 8a86fd4001..94673add31 100644 --- a/openedx/core/djangoapps/credentials/utils.py +++ b/openedx/core/djangoapps/credentials/utils.py @@ -55,14 +55,11 @@ def get_credentials(user, program_uuid=None): # Bypass caching for staff users, who may be generating credentials and # want to see them displayed immediately. use_cache = credential_configuration.is_cache_enabled and not user.is_staff - cache_key = credential_configuration.CACHE_KEY + '.' + user.username if use_cache else None + cache_key = '{}.{}'.format(credential_configuration.CACHE_KEY, user.username) if use_cache else None + if cache_key and program_uuid: + cache_key = '{}.{}'.format(cache_key, program_uuid) api = get_credentials_api_client(user) return get_edx_api_data( - credential_configuration, - 'credentials', - api=api, - resource_id=program_uuid, - querystring=querystring, - cache_key=cache_key + credential_configuration, 'credentials', api=api, querystring=querystring, cache_key=cache_key )