Improve index mirroring cache class to use the hash of the keys instead of the literal keys. Avoid warning about invalid key characters. Closes GitLab issue #518. Thanks to TheOneValen @ for the report.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-10-09 05:12:19 -04:00
parent 1e08653b88
commit f34bead7d2
4 changed files with 37 additions and 5 deletions

View File

@@ -1,8 +1,12 @@
3.1.6 (2018-10-XX)
==================
* Improve index mounting value clean up code to remove the spaces at the
* Improve index mirroring value clean up code to remove the spaces at the
starts and at the end of directories. Closes again GitLab issue #520
Thanks to TheOneValen @ for the report.
* Improve index mirroring cache class to use the hash of the keys
instead of the literal keys. Avoid warning about invalid key
characters. Closes GitLab issue #518. Thanks to TheOneValen @ for the
report.
3.1.5 (2018-10-08)
==================

View File

@@ -1,5 +1,7 @@
from __future__ import unicode_literals
import hashlib
from django.core.cache import caches
from .settings import (
@@ -8,17 +10,27 @@ from .settings import (
class IndexFilesystemCache(object):
@staticmethod
def get_key_hash(key):
return hashlib.sha256(key).hexdigest()
@staticmethod
def get_document_key(document):
return 'document_pk_{}'.format(document.pk)
return IndexFilesystemCache.get_key_hash(
key='document_pk_{}'.format(document.pk)
)
@staticmethod
def get_node_key(node):
return 'node_pk_{}'.format(node.pk)
return IndexFilesystemCache.get_key_hash(
key='node_pk_{}'.format(node.pk)
)
@staticmethod
def get_path_key(path):
return 'path_{}'.format(path)
return IndexFilesystemCache.get_key_hash(
key='path_{}'.format(path)
)
def __init__(self, name='default'):
self.cache = caches[name]

View File

@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals
TEST_CACHE_KEY_BAD_CHARACTERS = ' \r\n!@#$%^&*()+_{}|:"<>?-=[];\',./'
TEST_DOCUMENT_PK = 99
TEST_NODE_EXPRESSION = 'level_1'
TEST_NODE_EXPRESSION_MULTILINE = 'first\r\nsecond\r\nthird'

View File

@@ -1,10 +1,14 @@
from __future__ import absolute_import, unicode_literals
import warnings
from common.tests import BaseTestCase
from ..caches import IndexFilesystemCache
from .literals import TEST_DOCUMENT_PK, TEST_NODE_PK, TEST_PATH
from .literals import (
TEST_CACHE_KEY_BAD_CHARACTERS, TEST_DOCUMENT_PK, TEST_NODE_PK, TEST_PATH
)
class MockDocument(object):
@@ -47,3 +51,14 @@ class IndexFilesystemCacheTestCase(BaseTestCase):
self.cache.clear_node(node=self.node)
self.assertEquals(None, self.cache.get_path(path=TEST_PATH))
def test_valid_cache_key_characters(self):
with warnings.catch_warnings(record=True) as warning_list:
self.cache.cache.validate_key(TEST_CACHE_KEY_BAD_CHARACTERS)
self.assertTrue(len(warning_list) == 1)
with warnings.catch_warnings(record=True) as warning_list:
self.cache.cache.validate_key(
self.cache.get_key_hash(key=TEST_CACHE_KEY_BAD_CHARACTERS)
)
self.assertTrue(len(warning_list) == 0)