Skip to content

Commit 5f6f422

Browse files
authored
Merge pull request joke2k#122 from sanoma/feature/redis_locations
Feature: support for multiple redis locations
2 parents 38ce6f3 + 955aa7b commit 5f6f422

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ nosetests.xml
3535
.pydevproject
3636
.idea
3737
.projectile
38-
.ropeproject
38+
.ropeproject
39+
40+
# pyenv/pyenv-virtualenv
41+
.python-version

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ In order to use unsafe characters you have to encode with ``urllib.parse.encode`
227227
228228
See https://perishablepress.com/stop-using-unsafe-characters-in-urls/ for reference.
229229

230+
Multiple redis cache locations
231+
------------------------------
232+
233+
For redis cache, `multiple master/slave or shard locations <http://niwinz.github.io/django-redis/latest/#_pluggable_clients>`_ can be configured as follows:
234+
235+
.. code-block::
236+
237+
CACHE_URL='rediscache://master:6379,slave1:6379,slave2:6379/1'
238+
230239
Email settings
231240
--------------
232241

@@ -267,6 +276,11 @@ Django-environ is licensed under the MIT License - see the `LICENSE_FILE`_ file
267276
Changelog
268277
=========
269278

279+
`pending`
280+
---------
281+
282+
- Support for django-redis multiple locations (master/slave, shards)
283+
270284
`0.4.2 - 13-April-2017 <https://github.com/joke2k/django-environ/compare/v0.4.1...v0.4.2>`__
271285
--------------------------------------------------------------------------------------------
272286

environ/environ.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ def cache_url_config(cls, url, backend=None):
456456
scheme = url.scheme.replace('cache', '')
457457
else:
458458
scheme = 'unix'
459-
460-
config['LOCATION'] = scheme + '://' + url.netloc + url.path
459+
locations = [scheme + '://' + loc + url.path for loc in url.netloc.split(',')]
460+
config['LOCATION'] = locations[0] if len(locations) == 1 else locations
461461

462462
if url.query:
463463
config_options = {}

environ/test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ def test_redis_with_password_parsing(self):
477477
self.assertEqual(REDIS_DRIVER, url['BACKEND'])
478478
self.assertEqual(url['LOCATION'], 'redis://:redispass@127.0.0.1:6379/0')
479479

480+
def test_redis_multi_location_parsing(self):
481+
url = 'rediscache://host1:6379,host2:6379,host3:9999/1'
482+
url = Env.cache_url_config(url)
483+
484+
self.assertEqual(url['BACKEND'], REDIS_DRIVER)
485+
self.assertEqual(url['LOCATION'], [
486+
'redis://host1:6379/1',
487+
'redis://host2:6379/1',
488+
'redis://host3:9999/1',
489+
])
490+
480491
def test_redis_socket_url(self):
481492
url = 'redis://:redispass@/path/to/socket.sock?db=0'
482493
url = Env.cache_url_config(url)
@@ -485,7 +496,7 @@ def test_redis_socket_url(self):
485496
self.assertEqual(url['OPTIONS'], {
486497
'DB': 0
487498
})
488-
499+
489500
def test_options_parsing(self):
490501
url = 'filecache:///var/tmp/django_cache?timeout=60&max_entries=1000&cull_frequency=0'
491502
url = Env.cache_url_config(url)

0 commit comments

Comments
 (0)