-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create cache_utils.py, move stuff there
This will eventually support the creation of a `content_metadata` module, and we'll yank stuff out of the `subsidy_access_policy` module into this new module.
- Loading branch information
1 parent
9ad845b
commit cc341c0
Showing
8 changed files
with
53 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Utils for interacting with cache interfaces. | ||
""" | ||
import hashlib | ||
|
||
from django.conf import settings | ||
from edx_django_utils.cache import RequestCache | ||
|
||
from enterprise_access import __version__ as code_version | ||
|
||
CACHE_KEY_SEP = ':' | ||
DEFAULT_NAMESPACE = 'enterprise-access-default' | ||
|
||
|
||
def versioned_cache_key(*args): | ||
""" | ||
Utility to produce a versioned cache key, which includes | ||
an optional settings variable and the current code version, | ||
so that we can perform key-based cache invalidation. | ||
""" | ||
components = [str(arg) for arg in args] | ||
components.append(code_version) | ||
if stamp_from_settings := getattr(settings, 'CACHE_KEY_VERSION_STAMP', None): | ||
components.append(stamp_from_settings) | ||
decoded_cache_key = CACHE_KEY_SEP.join(components) | ||
return hashlib.sha512(decoded_cache_key.encode()).hexdigest() | ||
|
||
|
||
def request_cache(namespace=DEFAULT_NAMESPACE): | ||
""" | ||
Helper that returns a namespaced RequestCache instance. | ||
""" | ||
return RequestCache(namespace=namespace) |