python gae-memcache-decorator.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python gae-memcache-decorator.py相关的知识,希望对你有一定的参考价值。

import functools
import logging
from google.appengine.api import memcache
 
def cached(time=1200):
  """
  Decorator that caches the result of a method for the specified time in seconds.
  
  Use it as:
    
    @cached(time=1200)
    def functionToCache(arguments):
      ...
    
  """
  def decorator(function):
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
      key = '%s%s%s' % (function.__name__, str(args), str(kwargs))
      value = memcache.get(key)
      logging.debug('Cache lookup for %s, found? %s', key, value != None)
      if not value:
        value = function(*args, **kwargs)
        memcache.set(key, value, time=time)
      return value
    return wrapper
  return decorator

以上是关于python gae-memcache-decorator.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python