Fix django-compress caching issues

django-compress doesn't work well with cache. If horizon is
deployed in a containarized environment with pre-built static
in images, django-compress tries to load old templates from cache.

This patch introduces following changes:

* update_angular_template_hash uses the correct Django cache backend now
* fixed cache cleanup for memcached backend
* added clean_angular_template_cache management command to clean cache
  manually without executing heavy 'compress' command.

Closes-Bug: #1874657
Change-Id: If1fd61fa2e4b36e22797b0f54ac067a2a937f369
This commit is contained in:
Ivan Kolodyazhny 2020-04-21 18:36:35 +03:00
parent 50837618bd
commit 0d290b9184
4 changed files with 70 additions and 16 deletions

33
horizon/cache.py Normal file
View File

@ -0,0 +1,33 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.conf import settings
from django.core.cache import caches
from django.core.cache.utils import make_template_fragment_key
def cleanup_angular_template_cache(theme):
# The compressor has modified the angular template cache preloads
# which are cached in the 'COMPRESS_CACHE_BACKEND' Django cache.
django_cache = caches[settings.COMPRESS_CACHE_BACKEND]
# generate the same key as used in _scripts.html when caching the
# preloads
key = make_template_fragment_key(
"angular",
['template_cache_preloads', theme]
)
# if template preloads have been cached, clear them
if django_cache.get(key):
# Set to None because memcached doesn't remove records immediately
django_cache.set(key, None)

View File

@ -15,11 +15,11 @@
from compressor.signals import post_compress
from django.contrib.staticfiles import finders
from django.core.cache import caches
from django.core.cache.utils import make_template_fragment_key
from django.dispatch import receiver
from django import template
from horizon import cache
register = template.Library()
@ -37,21 +37,8 @@ def update_angular_template_hash(sender, **kwargs):
compressed = context['compressed'] # the compressed content
compressed_name = compressed['name'] # name of the compressed content
if compressed_name == 'angular_template_cache_preloads':
# The compressor has modified the angular template cache preloads
# which are cached in the 'default' Django cache. Fetch that cache.
cache = caches['default']
# generate the same key as used in _scripts.html when caching the
# preloads
theme = context['THEME'] # current theme being compressed
key = make_template_fragment_key(
"angular",
['template_cache_preloads', theme]
)
# if template preloads have been cached, clear them
if cache.get(key):
cache.delete(key)
cache.cleanup_angular_template_cache(theme)
@register.filter(name='angular_escapes')

View File

@ -0,0 +1,24 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.conf import settings
from django.core.management.base import BaseCommand
from horizon import cache
class Command(BaseCommand):
help = 'Clears template cache for angularized views.'
def handle(self, *args, **options):
for theme in settings.AVAILABLE_THEMES:
cache.cleanup_angular_template_cache(theme[0])

View File

@ -0,0 +1,10 @@
---
features:
- |
Added an ``cleanup_angular_template_cache`` management command, that clears
template cache for angularized views. The command is available
as ``./manage.py cleanup_angular_template_cache``.
fixes:
- |
[`bug/1874657 <https://bugs.launchpad.net/horizon/1874657>`_] Horizon
container upgrade works without additional ``./manage.py compress`` call.