diff --git a/r2/r2/lib/memoize.py b/r2/r2/lib/memoize.py index 422b8912a..9aa65a4b6 100644 --- a/r2/r2/lib/memoize.py +++ b/r2/r2/lib/memoize.py @@ -20,7 +20,7 @@ # CondeNet, Inc. All Rights Reserved. ################################################################################ from r2.config import cache -import sha +from r2.lib.filters import _force_utf8 class NoneResult(object): pass @@ -28,7 +28,7 @@ def memoize(iden, time = 0): def memoize_fn(fn): from r2.lib.memoize import NoneResult def new_fn(*a, **kw): - key = iden + str(a) + str(kw) + key = _make_key(iden, a, kw) #print 'CHECKING', key res = cache.get(key) if res is None: @@ -43,10 +43,28 @@ def memoize(iden, time = 0): return memoize_fn def clear_memo(iden, *a, **kw): - key = iden + str(a) + str(kw) + key = _make_key(iden, a, kw) #print 'CLEARING', key cache.delete(key) +def _make_key(iden, a, kw): + """ + Make the cache key. We have to descend into *a and **kw to make + sure that only regular strings are used in the key to keep 'foo' + and u'foo' in an args list from resulting in differing keys + """ + def _conv(s): + if isinstance(s, str): + return s + elif isinstance(s, unicode): + return _force_utf8(s) + else: + return str(s) + + return (_conv(iden) + + str([_conv(x) for x in a]) + + str(dict((_conv(x),_conv(y)) for (x,y) in kw))) + @memoize('test') def test(x, y): import time diff --git a/r2/r2/templates/subreddit.html b/r2/r2/templates/subreddit.html index 2bf711c4d..0276acc5a 100644 --- a/r2/r2/templates/subreddit.html +++ b/r2/r2/templates/subreddit.html @@ -22,6 +22,7 @@ <%! from r2.lib.strings import strings + from r2.lib.utils import timesince %> <%inherit file="printable.html"/> @@ -54,7 +55,8 @@ <%def name="tagline()"> - ${self.score(thing, thing.subscriber)} + ${self.score(thing, thing.subscriber)}, + ${_("a community for %(time)s") % dict(time=timesince(thing._date))} ##this function is used by subscriptionbox.html