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

Commit

Permalink
Merge 4f3cb43 into d07d1f3
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Feb 4, 2015
2 parents d07d1f3 + 4f3cb43 commit 6bb85e5
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 163 deletions.
4 changes: 4 additions & 0 deletions kuma/core/cache.py
@@ -0,0 +1,4 @@
from django.core.cache import get_cache

# just a helper to not have to redefine that all over the place
memcache = get_cache('memcache')
8 changes: 4 additions & 4 deletions kuma/core/utils.py
Expand Up @@ -11,14 +11,15 @@
from polib import pofile

from django.conf import settings
from django.core.cache import get_cache
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.shortcuts import _get_queryset
from django.utils.encoding import force_unicode
from django.utils.http import urlencode

from taggit.utils import split_strip

from .cache import memcache


log = commonware.log.getLogger('kuma.core.utils')

Expand Down Expand Up @@ -138,15 +139,14 @@ def __init__(self, key, attempts=1, expires=60 * 60 * 3):
self.key = 'lock_%s' % key
self.attempts = attempts
self.expires = expires
self.cache = get_cache('memcache')
self.cache = memcache

def locked(self):
return bool(self.cache.get(self.key))

def acquire(self):
cache = get_cache('memcache')
for i in xrange(0, self.attempts):
stored = cache.add(self.key, 1, self.expires)
stored = self.cache.add(self.key, 1, self.expires)
if stored:
return True
if i != self.attempts - 1:
Expand Down
18 changes: 7 additions & 11 deletions kuma/demos/helpers.py
Expand Up @@ -7,19 +7,18 @@
import jinja2

from django.conf import settings
from django.core.cache import cache
from django.utils.tzinfo import LocalTimezone

import jingo
from jingo import register
from tower import ugettext as _
from tower import ugettext, ungettext
from tower import ungettext, ugettext as _
from taggit.models import TaggedItem
from threadedcomments.models import ThreadedComment
from threadedcomments.forms import ThreadedCommentForm
from threadedcomments.templatetags import threadedcommentstags
import threadedcomments.views

from kuma.core.cache import memcache
from kuma.core.urlresolvers import reverse
from .models import Submission
from . import DEMOS_CACHE_NS_KEY, TAG_DESCRIPTIONS, DEMO_LICENSES
Expand All @@ -45,25 +44,23 @@ def register_cached_inclusion_tag(template, key_fn=None,
Accepts a string or function to generate a cache key based on the incoming
parameters, along with an expiration time configurable as
INCLUDE_CACHE_EXPIRES or an explicit parameter"""

if key_fn is None:
key_fn = template

def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):

if type(key_fn) is str:
cache_key = key_fn
else:
cache_key = key_fn(*args, **kw)

out = cache.get(cache_key)
out = memcache.get(cache_key)
if out is None:
context = f(*args, **kw)
t = jingo.env.get_template(template).render(context)
out = jinja2.Markup(t)
cache.set(cache_key, out, expires)
memcache.set(cache_key, out, expires)
return out

return register.function(wrapper)
Expand Down Expand Up @@ -133,10 +130,10 @@ def submission_thumb(submission, extra_class=None, thumb_width="200",


def submission_listing_cache_key(*args, **kw):
ns_key = cache.get(DEMOS_CACHE_NS_KEY)
ns_key = memcache.get(DEMOS_CACHE_NS_KEY)
if ns_key is None:
ns_key = random.randint(1, 10000)
cache.set(DEMOS_CACHE_NS_KEY, ns_key)
memcache.set(DEMOS_CACHE_NS_KEY, ns_key)
full_path = args[0].get_full_path()
username = args[0].user.username
return 'demos_%s:%s' % (ns_key,
Expand Down Expand Up @@ -306,8 +303,7 @@ def date_diff(timestamp, to=None):
count = abs(round(delta.days / chunk, 0))
break

date_str = (ugettext('%(number)d %(type)s') %
{'number': count, 'type': name(count)})
date_str = (_('%(number)d %(type)s') % {'number': count, 'type': name(count)})

if delta.days > 0:
return "in " + date_str
Expand Down

0 comments on commit 6bb85e5

Please sign in to comment.