Skip to content

Commit

Permalink
Merge pull request #121 from uw-it-aca/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jlaney authored Oct 7, 2020
2 parents 8cbe802 + 35081d8 commit d44bdd6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
31 changes: 20 additions & 11 deletions project/base_settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,29 @@
}
}

MEMCACHED_SERVERS = []
MEMCACHED_SERVER_COUNT = int(os.getenv('MEMCACHED_SERVER_COUNT', 0))
if (os.getenv('SESSION_BACKEND', '') == 'MEMCACHED'
and MEMCACHED_SERVER_COUNT > 0):
if MEMCACHED_SERVER_COUNT > 0:
MEMCACHED_SERVER_SPEC = os.getenv('MEMCACHED_SERVER_SPEC')
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': [MEMCACHED_SERVER_SPEC.format(n)
for n in range(
MEMCACHED_SERVER_COUNT)]

MEMCACHED_SERVERS = [MEMCACHED_SERVER_SPEC.format(n) for n in range(MEMCACHED_SERVER_COUNT)]
MEMCACHED_USE_POOLING = os.getenv('MEMCACHED_USE_POOLING', True)
MEMCACHED_MAX_POOL_SIZE = int(os.getenv('MEMCACHED_MAX_POOL_SIZE', 5))
MEMCACHED_CONNECT_TIMEOUT = int(os.getenv('MEMCACHED_CONNECT_TIMEOUT', 2))
MEMCACHED_TIMEOUT = int(os.getenv('MEMCACHED_TIMEOUT', 2))
MEMCACHED_NOREPLY = os.getenv('MEMCACHED_NOREPLY', True)

if os.getenv('SESSION_BACKEND', '') == 'MEMCACHED':
CACHES = {
'default': {
'BACKEND': 'memcached_clients.django.PymemcacheCache',
'LOCATION': MEMCACHED_SERVERS,
'OPTIONS': {
'use_pooling': MEMCACHED_USE_POOLING,
'max_pool_size': MEMCACHED_MAX_POOL_SIZE,
}
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
}
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
Expand Down
29 changes: 29 additions & 0 deletions tests/test_settings/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,35 @@ def test_db_postgres_with_env_values(self):
with SettingLoader('project.base_settings', DB='postgres', **database_env_values) as base_settings:
self.assertDictEqual(base_settings.DATABASES, postgres_db)

class TestMemcached(TestCase):
def test_servers(self):
memcached = {
'MEMCACHED_SERVER_COUNT': '2',
'MEMCACHED_SERVER_SPEC': 'mock_memcached_{}:11211'
}
with SettingLoader('project.base_settings', **memcached) as base_settings:
self.assertEqual(base_settings.MEMCACHED_SERVER_COUNT, 2)
self.assertListEqual(
base_settings.MEMCACHED_SERVERS,
['mock_memcached_0:11211', 'mock_memcached_1:11211'])

def test_cache_backend(self):
memcached = {
'MEMCACHED_SERVER_COUNT': '1',
'MEMCACHED_SERVER_SPEC': 'mock_memcached_{}:11211',
'MEMCACHED_USE_POOLING': 'True',
'MEMCACHED_MAX_POOL_SIZE': '9',
'SESSION_BACKEND': 'MEMCACHED',
}
with SettingLoader('project.base_settings', **memcached) as base_settings:
self.assertEqual(base_settings.CACHES['default']['LOCATION'],
['mock_memcached_0:11211'])
self.assertDictEqual(base_settings.CACHES['default']['OPTIONS'], {
'use_pooling': 'True', 'max_pool_size': 9})
self.assertEqual(base_settings.SESSION_ENGINE,
'django.contrib.sessions.backends.cache')


class TestStaticfilesFinders(TestCase):
def test_requried_finders(self):
with SettingLoader('project.base_settings') as base_settings:
Expand Down

0 comments on commit d44bdd6

Please sign in to comment.