mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 12:30:15 +02:00
Swap the Redis Cache client
This commit is contained in:
parent
8351bd2fa3
commit
297da44a5a
@ -172,11 +172,8 @@ MESSAGE_TAGS = {
|
|||||||
|
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": "redis_cache.RedisCache",
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
"LOCATION": "localhost:6379",
|
"LOCATION": "redis://127.0.0.1:6379/1" # change the 1 here to change the database used
|
||||||
"OPTIONS": {
|
|
||||||
"DB": 1,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +61,13 @@ EMAIL_HOST_PASSWORD = ''
|
|||||||
EMAIL_USE_TLS = True
|
EMAIL_USE_TLS = True
|
||||||
DEFAULT_FROM_EMAIL = ''
|
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. #
|
# Add any custom settings below here. #
|
||||||
#######################################
|
#######################################
|
||||||
|
@ -8,7 +8,7 @@ from uuid import uuid1
|
|||||||
from redis import Redis
|
from redis import Redis
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from django.core.cache import caches
|
from django_redis import get_redis_connection
|
||||||
|
|
||||||
from allianceauth import __title__ as AUTH_TITLE, __url__, __version__
|
from allianceauth import __title__ as AUTH_TITLE, __url__, __version__
|
||||||
|
|
||||||
@ -103,8 +103,7 @@ class DiscordClient:
|
|||||||
self._access_token = str(access_token)
|
self._access_token = str(access_token)
|
||||||
self._is_rate_limited = bool(is_rate_limited)
|
self._is_rate_limited = bool(is_rate_limited)
|
||||||
if not redis:
|
if not redis:
|
||||||
default_cache = caches['default']
|
self._redis = get_redis_connection("default")
|
||||||
self._redis = default_cache.get_master_client()
|
|
||||||
if not isinstance(self._redis, Redis):
|
if not isinstance(self._redis, Redis):
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
'This class requires a Redis client, but none was provided '
|
'This class requires a Redis client, but none was provided '
|
||||||
|
@ -85,28 +85,24 @@ class TestBasicsAndHelpers(TestCase):
|
|||||||
client = DiscordClient(TEST_BOT_TOKEN, mock_redis, is_rate_limited=True)
|
client = DiscordClient(TEST_BOT_TOKEN, mock_redis, is_rate_limited=True)
|
||||||
self.assertTrue(client.is_rate_limited)
|
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):
|
def test_use_default_redis_if_none_provided(self, mock_caches):
|
||||||
my_redis = MagicMock(spec=Redis)
|
my_redis = MagicMock(spec=Redis)
|
||||||
mock_default_cache = MagicMock(**{'get_master_client.return_value': my_redis})
|
mock_caches.return_value = my_redis
|
||||||
my_dict = {'default': mock_default_cache}
|
|
||||||
mock_caches.__getitem__.side_effect = my_dict.__getitem__
|
|
||||||
|
|
||||||
client = DiscordClient(TEST_BOT_TOKEN)
|
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)
|
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):
|
def test_raise_exception_if_default_cache_is_not_redis(self, mock_caches):
|
||||||
my_redis = MagicMock()
|
my_redis = MagicMock()
|
||||||
mock_default_cache = MagicMock(**{'get_master_client.return_value': my_redis})
|
mock_caches.return_value = my_redis
|
||||||
my_dict = {'default': mock_default_cache}
|
|
||||||
mock_caches.__getitem__.side_effect = my_dict.__getitem__
|
|
||||||
|
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
DiscordClient(TEST_BOT_TOKEN)
|
DiscordClient(TEST_BOT_TOKEN)
|
||||||
|
|
||||||
self.assertTrue(mock_default_cache.get_master_client.called)
|
self.assertTrue(mock_caches.called)
|
||||||
|
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
|
@ -14,7 +14,7 @@ from requests.exceptions import HTTPError
|
|||||||
import requests_mock
|
import requests_mock
|
||||||
|
|
||||||
from django.contrib.auth.models import Group, User
|
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.shortcuts import reverse
|
||||||
from django.test import TransactionTestCase, TestCase
|
from django.test import TransactionTestCase, TestCase
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
@ -87,8 +87,7 @@ remove_guild_member_request = DiscordRequest(
|
|||||||
|
|
||||||
|
|
||||||
def clear_cache():
|
def clear_cache():
|
||||||
default_cache = caches['default']
|
redis = get_redis_connection('default')
|
||||||
redis = default_cache.get_master_client()
|
|
||||||
redis.flushall()
|
redis.flushall()
|
||||||
logger.info('Cache flushed')
|
logger.info('Cache flushed')
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@ -26,7 +26,7 @@ install_requires = [
|
|||||||
'django-bootstrap-form',
|
'django-bootstrap-form',
|
||||||
'django-registration>=3.1',
|
'django-registration>=3.1',
|
||||||
'django-sortedm2m',
|
'django-sortedm2m',
|
||||||
'django-redis-cache>=3.0.0',
|
'django-redis>=5.2.0<6.0.0',
|
||||||
'django-celery-beat>=2.0.0',
|
'django-celery-beat>=2.0.0',
|
||||||
|
|
||||||
'openfire-restapi',
|
'openfire-restapi',
|
||||||
|
@ -33,11 +33,8 @@ ROOT_URLCONF = 'tests.urls'
|
|||||||
|
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": "redis_cache.RedisCache",
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
"LOCATION": "localhost:6379",
|
"LOCATION": "redis://127.0.0.1:6379/1"
|
||||||
"OPTIONS": {
|
|
||||||
"DB": 1,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,11 +14,8 @@ ROOT_URLCONF = 'tests.urls'
|
|||||||
|
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": "redis_cache.RedisCache",
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
"LOCATION": "localhost:6379",
|
"LOCATION": "redis://127.0.0.1:6379/1"
|
||||||
"OPTIONS": {
|
|
||||||
"DB": 1,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user