Merge "add caching to _build_regex_range"

This commit is contained in:
Zuul
2018-09-17 00:02:28 +00:00
committed by Gerrit Code Review
2 changed files with 26 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ Common parameter types for validating request Body.
"""
import copy
import functools
import re
import unicodedata
@@ -24,6 +25,27 @@ import six
from nova.i18n import _
from nova.objects import tag
_REGEX_RANGE_CACHE = {}
def memorize(func):
@functools.wraps(func)
def memorizer(*args, **kwargs):
global _REGEX_RANGE_CACHE
key = "%s:%s:%s" % (func.__name__, hash(str(args)), hash(str(kwargs)))
value = _REGEX_RANGE_CACHE.get(key)
if value is None:
value = func(*args, **kwargs)
_REGEX_RANGE_CACHE[key] = value
return value
return memorizer
def _reset_cache():
global _REGEX_RANGE_CACHE
_REGEX_RANGE_CACHE = {}
def single_param(schema):
"""Macro function for use in JSONSchema to support query parameters that
@@ -83,6 +105,7 @@ def _get_all_chars():
# constraint fails and this causes issues for some unittests when
# PYTHONHASHSEED is set randomly.
@memorize
def _build_regex_range(ws=True, invert=False, exclude=None):
"""Build a range regex for a set of characters in utf8.

View File

@@ -95,7 +95,9 @@ class ValidationRegex(test.NoDBTestCase):
self.useFixture(fixtures.MonkeyPatch(
'nova.api.validation.parameter_types._get_all_chars',
_get_all_chars))
# note that since we use only the ascii range in the tests
# we have to clear the cache to recompute them.
parameter_types._reset_cache()
r = parameter_types._build_regex_range(ws=False)
self.assertEqual(r, re.escape('!') + '-' + re.escape('~'))