-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added --no-recommends flag to apt-get install (#766) * initial commit * add cache configuration logic * remove prints * Add cache configuration tests * update settings with cache config * add new cache env fixtures * add cache config generation helper * remove unused imports * copy updates * add comment and type hint * removed redis replication group id var * add print statement * handle member var with json loads * update test fixture to use list * update domain str length --------- Co-authored-by: Murdo <[email protected]>
- Loading branch information
1 parent
1cfb6b7
commit 3b64836
Showing
10 changed files
with
244 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
import os | ||
from typing import Any | ||
|
||
|
||
def generate_cache_configuration() -> dict[str, Any]: | ||
""" | ||
Generates appropriate cache configuration for the given environment | ||
""" | ||
cache = {} | ||
cache["BACKEND"] = "django.core.cache.backends.locmem.LocMemCache" | ||
|
||
# Utilising Redis in Non local development environments | ||
if ( | ||
os.environ.get("REDIS_AUTH_TOKEN") | ||
and os.environ.get("REDIS_PRIMARY_ENDPOINT_ADDRESS") | ||
and os.environ.get("REDIS_MEMBER_CLUSTERS") | ||
): | ||
REDIS_DB_VALUE: int = 0 | ||
cache["BACKEND"] = "django.core.cache.backends.redis.RedisCache" | ||
|
||
location: list[str] = [] | ||
location.append( | ||
f"rediss://:{os.environ.get('REDIS_AUTH_TOKEN')}@{os.environ.get('REDIS_PRIMARY_ENDPOINT_ADDRESS')}/{REDIS_DB_VALUE}" # noqa: E501 | ||
) | ||
|
||
domain: str = ".".join( | ||
os.environ.get("REDIS_PRIMARY_ENDPOINT_ADDRESS", "").split(".")[1:] | ||
) # 'iwfvzo.euw2.cache.amazonaws.com' | ||
|
||
hosts: list[str] = json.loads( | ||
os.environ.get("REDIS_MEMBER_CLUSTERS", "") | ||
) # ["cp-f05ff2dca7d81952-001","cp-f05ff2dca7d81952-002"] | ||
|
||
for host in hosts: | ||
location.append( | ||
f"rediss://:{os.environ.get('REDIS_AUTH_TOKEN')}@{host}.{domain}/{REDIS_DB_VALUE}" # noqa: E501 | ||
) | ||
|
||
cache["LOCATION"] = location | ||
|
||
return {"default": cache} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,20 @@ | ||
from core.helpers import generate_cache_configuration | ||
|
||
|
||
def test_redis_cache_for_non_local_development(set_redis_cache_env): | ||
cache = generate_cache_configuration() | ||
location = cache["default"]["LOCATION"] | ||
assert len(location) == 3 | ||
assert ( | ||
location[0] | ||
== "rediss://:[email protected]/0" | ||
) | ||
assert cache["default"]["BACKEND"] == "django.core.cache.backends.redis.RedisCache" | ||
|
||
|
||
def test_memcache_for_local_development(unset_redis_cache_env): | ||
cache = generate_cache_configuration() | ||
assert "LOCATION" not in cache["default"] | ||
assert ( | ||
cache["default"]["BACKEND"] == "django.core.cache.backends.locmem.LocMemCache" | ||
) |
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