diff --git a/allianceauth/project_template/project_name/settings/base.py b/allianceauth/project_template/project_name/settings/base.py index af53215f..755ca4d1 100644 --- a/allianceauth/project_template/project_name/settings/base.py +++ b/allianceauth/project_template/project_name/settings/base.py @@ -172,11 +172,8 @@ MESSAGE_TAGS = { CACHES = { "default": { - "BACKEND": "redis_cache.RedisCache", - "LOCATION": "localhost:6379", - "OPTIONS": { - "DB": 1, - } + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://127.0.0.1:6379/1" # change the 1 here to change the database used } } diff --git a/allianceauth/project_template/project_name/settings/local.py b/allianceauth/project_template/project_name/settings/local.py index cc900956..2004bdc1 100644 --- a/allianceauth/project_template/project_name/settings/local.py +++ b/allianceauth/project_template/project_name/settings/local.py @@ -61,6 +61,13 @@ EMAIL_HOST_PASSWORD = '' EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = '' +# Cache compression can help on bigger auths where ram starts to become an issue. +# Uncomment the following 3 lines to enable. + +#CACHES["default"]["OPTIONS"] = { +# "COMPRESSOR": "django_redis.compressors.lzma.LzmaCompressor", +#} + ####################################### # Add any custom settings below here. # ####################################### diff --git a/allianceauth/services/modules/discord/discord_client/client.py b/allianceauth/services/modules/discord/discord_client/client.py index bbe62720..94c2d8c0 100644 --- a/allianceauth/services/modules/discord/discord_client/client.py +++ b/allianceauth/services/modules/discord/discord_client/client.py @@ -8,7 +8,7 @@ from uuid import uuid1 from redis import Redis import requests -from django.core.cache import caches +from django_redis import get_redis_connection from allianceauth import __title__ as AUTH_TITLE, __url__, __version__ @@ -103,8 +103,7 @@ class DiscordClient: self._access_token = str(access_token) self._is_rate_limited = bool(is_rate_limited) if not redis: - default_cache = caches['default'] - self._redis = default_cache.get_master_client() + self._redis = get_redis_connection("default") if not isinstance(self._redis, Redis): raise RuntimeError( 'This class requires a Redis client, but none was provided ' diff --git a/allianceauth/services/modules/discord/discord_client/tests/test_client.py b/allianceauth/services/modules/discord/discord_client/tests/test_client.py index acc57fac..8d2eece6 100644 --- a/allianceauth/services/modules/discord/discord_client/tests/test_client.py +++ b/allianceauth/services/modules/discord/discord_client/tests/test_client.py @@ -85,28 +85,24 @@ class TestBasicsAndHelpers(TestCase): client = DiscordClient(TEST_BOT_TOKEN, mock_redis, is_rate_limited=True) self.assertTrue(client.is_rate_limited) - @patch(MODULE_PATH + '.caches') + @patch(MODULE_PATH + '.get_redis_connection') def test_use_default_redis_if_none_provided(self, mock_caches): my_redis = MagicMock(spec=Redis) - mock_default_cache = MagicMock(**{'get_master_client.return_value': my_redis}) - my_dict = {'default': mock_default_cache} - mock_caches.__getitem__.side_effect = my_dict.__getitem__ + mock_caches.return_value = my_redis client = DiscordClient(TEST_BOT_TOKEN) - self.assertTrue(mock_default_cache.get_master_client.called) + self.assertTrue(mock_caches.called) self.assertEqual(client._redis, my_redis) - @patch(MODULE_PATH + '.caches') + @patch(MODULE_PATH + '.get_redis_connection') def test_raise_exception_if_default_cache_is_not_redis(self, mock_caches): my_redis = MagicMock() - mock_default_cache = MagicMock(**{'get_master_client.return_value': my_redis}) - my_dict = {'default': mock_default_cache} - mock_caches.__getitem__.side_effect = my_dict.__getitem__ + mock_caches.return_value = my_redis with self.assertRaises(RuntimeError): DiscordClient(TEST_BOT_TOKEN) - self.assertTrue(mock_default_cache.get_master_client.called) + self.assertTrue(mock_caches.called) @requests_mock.Mocker() diff --git a/allianceauth/services/modules/discord/tests/test_integration.py b/allianceauth/services/modules/discord/tests/test_integration.py index e69f6a4d..9bbbd0b0 100644 --- a/allianceauth/services/modules/discord/tests/test_integration.py +++ b/allianceauth/services/modules/discord/tests/test_integration.py @@ -14,7 +14,7 @@ from requests.exceptions import HTTPError import requests_mock from django.contrib.auth.models import Group, User -from django.core.cache import caches +from django_redis import get_redis_connection from django.shortcuts import reverse from django.test import TransactionTestCase, TestCase from django.test.utils import override_settings @@ -87,8 +87,7 @@ remove_guild_member_request = DiscordRequest( def clear_cache(): - default_cache = caches['default'] - redis = default_cache.get_master_client() + redis = get_redis_connection('default') redis.flushall() logger.info('Cache flushed') diff --git a/setup.py b/setup.py index 7665e992..9f247c17 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ install_requires = [ 'django-bootstrap-form', 'django-registration>=3.1', 'django-sortedm2m', - 'django-redis-cache>=3.0.0', + 'django-redis>=5.2.0<6.0.0', 'django-celery-beat>=2.0.0', 'openfire-restapi', diff --git a/tests/settings_all.py b/tests/settings_all.py index 4e7b631d..c2fec175 100644 --- a/tests/settings_all.py +++ b/tests/settings_all.py @@ -33,11 +33,8 @@ ROOT_URLCONF = 'tests.urls' CACHES = { "default": { - "BACKEND": "redis_cache.RedisCache", - "LOCATION": "localhost:6379", - "OPTIONS": { - "DB": 1, - } + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://127.0.0.1:6379/1" } } diff --git a/tests/settings_core.py b/tests/settings_core.py index fd97a388..515f5ba2 100644 --- a/tests/settings_core.py +++ b/tests/settings_core.py @@ -14,11 +14,8 @@ ROOT_URLCONF = 'tests.urls' CACHES = { "default": { - "BACKEND": "redis_cache.RedisCache", - "LOCATION": "localhost:6379", - "OPTIONS": { - "DB": 1, - } + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://127.0.0.1:6379/1" } }