src/cm/utils/cache.py
author Simon Descarpentries <sid@sopinspace.com>
Fri, 20 Sep 2013 16:28:40 +0200
changeset 536 0c625ce49a81
parent 349 8d1ce1bda109
permissions -rw-r--r--
- updating jQuery (1.3.2 -> 1.10.2), jQuery UI, jQuery modules - [text_view_frame.html] Add some JS driven CSS tricks for iPad - [text_view_frame.html] move throbber's hiding-call in nested text_view_comments iframe onload signal (so it works with jQuery 1.10.2 and the hiding timing is good) - [text_view_frame.html][base.html] factorize req_yui.html et req_jquery.html - [main_tabs][text_tabs][f_client-min.js] rewrite jQuery UI Tabs API calls select in active - [base.css] CSS fix grey frame appearence for all tabs - hg remove bgiframe -> not loaded in templates anymore - hg remove header_custom.html -> the file has been empty since 4 years - hg remove controls.html -> the file has been empty since 4 years

from django.core.cache import cache
from hashlib import sha1
from django.conf import settings

# adapted [to django] from http://code.activestate.com/recipes/325205/
def dj_memoize(f):
    def g(*args, **kwargs):
        key = sha1( str((settings.SITE_URL, f.__name__, f, tuple(args), frozenset(kwargs.items())) )).hexdigest()
        val = cache.get(key)
        if not val:
            val = f(*args, **kwargs)
            cache.set(key,val)
        return val
    return g



# adapted from http://code.activestate.com/recipes/496879/
# decorator with LRU policy

# changed : 
#  - default limit is 100
#  - store sha1 of key (shorter) 
import cPickle, hashlib

def memoize(function, limit=100):
    if isinstance(function, int):
        def memoize_wrapper(f):
            return memoize(f, function)

        return memoize_wrapper

    dict = {}
    list = []
    def memoize_wrapper(*args, **kwargs):
        key = hashlib.sha1(cPickle.dumps((args, kwargs))).digest()
        try:
            list.append(list.pop(list.index(key)))
        except ValueError:
            dict[key] = function(*args, **kwargs)
            list.append(key)
            if limit is not None and len(list) > limit:
                del dict[list.pop(0)]

        return dict[key]

    memoize_wrapper._memoize_dict = dict
    memoize_wrapper._memoize_list = list
    memoize_wrapper._memoize_limit = limit
    memoize_wrapper._memoize_origfunc = function
    memoize_wrapper.func_name = function.func_name
    return memoize_wrapper