[Django]関数ごとmemcacheに入れるための便利関数

こんな感じ。

def cache_func(key, timeout, func, *args, **kwargs):
    # get the result from memcache
    result = memcache.get(key)
    if result:
        return result
    # get the result from orginal function
    result = func(*args, **kwargs)
    # set the result to memcache
    memcache.set(key, result, timeout)
    return result

使い方はこんな感じ。

def get_hogehoge(id, count):
    return "%(id)s_%(count)s"
cache_timelines = lambda id, count: cache_func('cachekey_%(id)s_%(count)s' % dict(id=id, count=count), 60 * 10, get_hogehoge, id, count)


Popularity: 9% [?]

  • Digg
  • Google Bookmarks
  • Google Reader
  • Facebook
  • Delicious
  • FriendFeed
  • Evernote
  • Twitter
  • Share/Bookmark
  1. No comments yet.

  1. No trackbacks yet.