Merge pull request #437 from chrismeyersfsu/fix-feature_cache

cache license validation
This commit is contained in:
Chris Meyers
2017-09-20 14:00:44 -05:00
committed by GitHub
3 changed files with 15 additions and 11 deletions

View File

@@ -2,13 +2,17 @@
# All Rights Reserved. # All Rights Reserved.
# Django # Django
from django.core.cache import cache
from django.core.signals import setting_changed
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
# Django REST Framework # Django REST Framework
from rest_framework.exceptions import APIException from rest_framework.exceptions import APIException
# Tower # Tower
from awx.main.utils.common import get_licenser, memoize from awx.main.utils.common import get_licenser
from awx.main.utils import memoize
__all__ = ['LicenseForbids', 'get_license', 'get_licensed_features', __all__ = ['LicenseForbids', 'get_license', 'get_licensed_features',
'feature_enabled', 'feature_exists'] 'feature_enabled', 'feature_exists']
@@ -19,10 +23,18 @@ class LicenseForbids(APIException):
default_detail = _('Your Tower license does not allow that.') default_detail = _('Your Tower license does not allow that.')
@memoize(cache_key='_validated_license_data')
def _get_validated_license_data(): def _get_validated_license_data():
return get_licenser().validate() return get_licenser().validate()
@receiver(setting_changed)
def _on_setting_changed(sender, **kwargs):
# Clear cached result above when license changes.
if kwargs.get('setting', None) == 'LICENSE':
cache.delete('_validated_license_data')
def get_license(show_key=False): def get_license(show_key=False):
"""Return a dictionary representing the active license on this Tower instance.""" """Return a dictionary representing the active license on this Tower instance."""
license_data = _get_validated_license_data() license_data = _get_validated_license_data()
@@ -40,7 +52,6 @@ def get_licensed_features():
return features return features
@memoize(cache_name='ephemeral')
def feature_enabled(name): def feature_enabled(name):
"""Return True if the requested feature is enabled, False otherwise.""" """Return True if the requested feature is enabled, False otherwise."""
validated_license_data = _get_validated_license_data() validated_license_data = _get_validated_license_data()

View File

@@ -107,14 +107,13 @@ class RequireDebugTrueOrTest(logging.Filter):
return settings.DEBUG or 'test' in sys.argv return settings.DEBUG or 'test' in sys.argv
def memoize(ttl=60, cache_key=None, cache_name='default'): def memoize(ttl=60, cache_key=None):
''' '''
Decorator to wrap a function and cache its result. Decorator to wrap a function and cache its result.
''' '''
from django.core.cache import caches from django.core.cache import cache
def _memoizer(f, *args, **kwargs): def _memoizer(f, *args, **kwargs):
cache = caches[cache_name]
key = cache_key or slugify('%s %r %r' % (f.__name__, args, kwargs)) key = cache_key or slugify('%s %r %r' % (f.__name__, args, kwargs))
value = cache.get(key) value = cache.get(key)
if value is None: if value is None:

View File

@@ -481,9 +481,6 @@ if is_testing():
'default': { 'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}, },
'ephemeral': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
} }
else: else:
CACHES = { CACHES = {
@@ -491,9 +488,6 @@ else:
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'memcached:11211', 'LOCATION': 'memcached:11211',
}, },
'ephemeral': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
} }
# Social Auth configuration. # Social Auth configuration.