mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 04:20:17 +02:00
Swap the Redis Cache client
This commit is contained in:
parent
8351bd2fa3
commit
297da44a5a
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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. #
|
||||
#######################################
|
||||
|
@ -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 '
|
||||
|
@ -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()
|
||||
|
@ -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')
|
||||
|
||||
|
2
setup.py
2
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',
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user