Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Fix bug 1104119 - Cache refill instead of invalidation for document z…
Browse files Browse the repository at this point in the history
…one remaps.

This resets the cached document zone remaps when saving documents instead just deleting them when saving the zone.
  • Loading branch information
jezdez committed Feb 10, 2015
1 parent 225608f commit 944c624
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
2 changes: 0 additions & 2 deletions kuma/wiki/constants.py
Expand Up @@ -192,8 +192,6 @@
]
SLUG_CLEANSING_REGEX = '^\/?(([A-z-]+)?\/?docs\/)?'

URL_REMAPS_CACHE_KEY_TMPL = 'DocumentZoneUrlRemaps:%s'

# TODO: Put this under the control of Constance / Waffle?
# Flags used to signify revisions in need of review
REVIEW_FLAG_TAGS = (
Expand Down
30 changes: 18 additions & 12 deletions kuma/wiki/managers.py
Expand Up @@ -9,7 +9,7 @@
from kuma.core.cache import memcache

from .constants import (ALLOWED_TAGS, ALLOWED_ATTRIBUTES, ALLOWED_STYLES,
TEMPLATE_TITLE_PREFIX, URL_REMAPS_CACHE_KEY_TMPL)
TEMPLATE_TITLE_PREFIX)
from .content import parse as parse_content
from .queries import TransformQuerySet

Expand Down Expand Up @@ -230,20 +230,26 @@ def get_query_set(self):
class DocumentZoneManager(models.Manager):
"""Manager for DocumentZone objects"""

def build_url_remaps(self, locale):
qs = (self.filter(document__locale=locale,
url_root__isnull=False)
.exclude(url_root=''))
return [{
'original_path': '/docs/%s' % zone.document.slug,
'new_path': '/%s' % zone.url_root
} for zone in qs]

def reset_url_remaps(self, locale, cache_key=None):
if cache_key is None:
cache_key = self.model.cache_key(locale)
remaps = self.build_url_remaps(locale)
memcache.set(cache_key, remaps)

def get_url_remaps(self, locale):
cache_key = URL_REMAPS_CACHE_KEY_TMPL % locale
cache_key = self.model.cache_key(locale)
remaps = memcache.get(cache_key)

if not remaps:
qs = (self.filter(document__locale=locale,
url_root__isnull=False)
.exclude(url_root=''))
remaps = [{
'original_path': '/docs/%s' % zone.document.slug,
'new_path': '/%s' % zone.url_root
} for zone in qs]
memcache.set(cache_key, remaps)

remaps = self.reset_url_remaps(locale, cache_key)
return remaps


Expand Down
10 changes: 7 additions & 3 deletions kuma/wiki/models.py
Expand Up @@ -44,7 +44,7 @@
from . import kumascript
from .constants import (DEKI_FILE_URL, DOCUMENT_LAST_MODIFIED_CACHE_KEY_TMPL,
KUMA_FILE_URL, REDIRECT_CONTENT, REDIRECT_HTML,
TEMPLATE_TITLE_PREFIX, URL_REMAPS_CACHE_KEY_TMPL)
TEMPLATE_TITLE_PREFIX)
from .content import parse as parse_content
from .content import (extract_code_sample, extract_css_classnames,
extract_html_attributes, extract_kumascript_macro_names,
Expand Down Expand Up @@ -1532,11 +1532,15 @@ def __unicode__(self):
return u'DocumentZone %s (%s)' % (self.document.get_absolute_url(),
self.document.title)

@staticmethod
def cache_key(locale):
return 'wiki:zones:remaps:%s' % locale

def save(self, *args, **kwargs):
super(DocumentZone, self).save(*args, **kwargs)

# Invalidate URL remap cache for this zone
memcache.delete(URL_REMAPS_CACHE_KEY_TMPL % self.document.locale)
# Reset URL cache for the locale of this zone's attached document
DocumentZone.objects.reset_url_remaps(self.document.locale)


class ReviewTag(TagBase):
Expand Down
22 changes: 16 additions & 6 deletions kuma/wiki/tasks.py
Expand Up @@ -24,7 +24,7 @@
from .events import context_dict
from .exceptions import PageMoveError, StaleDocumentsRenderingInProgress
from .helpers import absolutify
from .models import Document, Revision, RevisionIP
from .models import Document, Revision, RevisionIP, DocumentZone
from .search import WikiDocumentType
from .signals import render_done

Expand Down Expand Up @@ -112,11 +112,7 @@ def build_json_data_for_document_task(pk, stale):

@receiver(render_done)
def build_json_data_handler(sender, instance, **kwargs):
try:
build_json_data_for_document_task.delay(instance.pk, stale=False)
except:
logging.error('JSON metadata build task failed',
exc_info=True)
build_json_data_for_document_task.delay(instance.pk, stale=False)


@task
Expand Down Expand Up @@ -352,3 +348,17 @@ def unindex_documents(ids, index_pk):
index = Index.objects.get(pk=index_pk)

cls.bulk_delete(ids, es=es, index=index.prefixed_name)


@task
def invalidate_zone_cache(pk):
document = Document.objects.get(pk=pk)
# if the document is a document zone
if document.zones.exists():
# reset the cached list of zones of the document's locale
DocumentZone.objects.reset_url_remaps(document.locale)


@receiver(render_done)
def invalidate_zone_cache_handler(sender, instance, **kwargs):
invalidate_zone_cache.delay(instance.pk)

0 comments on commit 944c624

Please sign in to comment.