Skip to content

Commit

Permalink
Update cache settings to match OOHDIR's
Browse files Browse the repository at this point in the history
For #522
  • Loading branch information
philgyford committed Nov 29, 2022
1 parent 6bca74f commit 3d94d39
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
14 changes: 6 additions & 8 deletions .env.dist
Expand Up @@ -45,14 +45,12 @@ HINES_LOG_LEVEL="INFO"
# Is the site using https? Should be True for Production:
HINES_USE_HTTPS="False"

# Set this to "redis", "dummy" or "memory":
HINES_CACHE="dummy"

# If HINES_CACHE is "redis":
# Will be used as the cache URL if set:
REDIS_TLS_URL=""
# Will be used as the cache URL if REDIS_TLS_URL is not set:
REDIS_URL=""
# Should be one of "dummy" (default), "memory" or "redis":
HINES_CACHE_TYPE="dummy"

# Required if HINES_CACHE_TYPE is "redis", ignored otherwise:
# Use a host of 'localhost' in production, 'redis' with dev Docker.
REDIS_URL="redis://localhost:6379/0"

# Set this to use Sentry:
SENTRY_DSN=""
Expand Down
61 changes: 28 additions & 33 deletions hines/config/settings.py
Expand Up @@ -71,6 +71,7 @@
]

MIDDLEWARE = [
"django.middleware.cache.UpdateCacheMiddleware",
"django.middleware.security.SecurityMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -80,8 +81,8 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"mentions.middleware.WebmentionHeadMiddleware",
# Can go at the end of the list:
"django.contrib.redirects.middleware.RedirectFallbackMiddleware",
"django.middleware.cache.FetchFromCacheMiddleware",
]

ROOT_URLCONF = "hines.config.urls"
Expand Down Expand Up @@ -109,22 +110,6 @@
DATABASES = {"default": dj_database_url.config(conn_max_age=500)}


# Custom setting to enable the site-wide caching.
# (We must also have set up the CACHES setting, if making this True.)
USE_PER_SITE_CACHE = False

if USE_PER_SITE_CACHE:
# Must be first:
MIDDLEWARE = ["django.middleware.cache.UpdateCacheMiddleware"] + MIDDLEWARE
# Must be last:
MIDDLEWARE += [
"django.middleware.cache.FetchFromCacheMiddleware",
]
CACHE_MIDDLEWARE_ALIAS = "default"
CACHE_MIDDLEWARE_SECONDS = 600
CACHE_MIDDLEWARE_KEY_PREFIX = ""


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -260,32 +245,42 @@
}


HINES_CACHE = os.getenv("HINES_CACHE", default="memory")
# Both of these are also used in HINES.apps.up.views.index:
HINES_CACHE_TYPE = os.getenv("HINES_CACHE_TYPE", default="dummy")
REDIS_URL = os.getenv("REDIS_URL", "")

# The DEFAULT_CACHE_TYPE setting is used in the /up/ URL to decide whether
# to check the state of the Redis cache or not

if HINES_CACHE_TYPE == "memory":
# Use in-memory caching
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "HINES",
}
}

if HINES_CACHE == "redis":
# Use the TLS URL if set, otherwise, use the non-TLS one:
REDIS_URL = os.getenv("REDIS_TLS_URL", default=os.getenv("REDIS_URL", default=""))
if REDIS_URL != "":
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_URL,
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
}
elif HINES_CACHE_TYPE == "redis" and REDIS_URL:
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": os.getenv("REDIS_URL"),
}
if os.getenv("REDIS_TLS_URL", default=""):
CACHES["default"]["OPTIONS"]["CONNECTION_POOL_KWARGS"] = {
"ssl_cert_reqs": None
}
}

elif HINES_CACHE == "dummy":
else:
# Use dummy cache (ie, no caching)
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.dummy.DummyCache",
}
}


# Seconds before expiring a cached item. None for never expiring.
CACHES["default"]["TIMEOUT"] = 300

TEST_RUNNER = "hines.core.test_runner.HinesTestRunner"


Expand Down

0 comments on commit 3d94d39

Please sign in to comment.