Python实现简单的缓存和缓存decorator,pythondecorator,一个简单的缓存实现,将缓
Python实现简单的缓存和缓存decorator,pythondecorator,一个简单的缓存实现,将缓
一个简单的缓存实现,将缓存数据保存在单例的对象中。
使用方法:
# Initialize SimpleCache({'data':{'example':'example data'}}) # Getting instance c = SimpleCache.getInstance() c.set('re.reg_exp_compiled',re.compile(r'\W*')) reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*')) # -------------------------------------------------------------- c = SimpleCache.getInstance() reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*')) # -------------------------------------------------------------- @scache def func1(): return 'OK'
实现
__author__ = "Andrey Nikishaev"__copyright__ = "Copyright 2010, http://creotiv.in.ua"__license__ = "GPL"__version__ = "0.3"__maintainer__ = "Andrey Nikishaev"__email__ = "creotiv@gmail.com"__status__ = "Production""""Simple local cache.It saves local data in singleton dictionary with convenient interfaceExamples of use: # Initialize SimpleCache({'data':{'example':'example data'}}) # Getting instance c = SimpleCache.getInstance() c.set('re.reg_exp_compiled',re.compile(r'\W*')) reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*'))or c = SimpleCache.getInstance() reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*'))or @scache def func1(): return 'OK'"""class SimpleCache(dict): def __new__(cls,*args): if not hasattr(cls,'_instance'): cls._instance = dict.__new__(cls) else: raise Exception('SimpleCache already initialized') return cls._instance @classmethod def getInstance(cls): if not hasattr(cls,'_instance'): cls._instance = dict.__new__(cls) return cls._instance def get(self,name,default=None): """Multilevel get function. Code: Config().get('opt.opt_level2.key','default_value') """ if not name: return default levels = name.split('.') data = self for level in levels: try: data = data[level] except: return default return data def set(self,name,value): """Multilevel set function Code: Config().set('opt.opt_level2.key','default_value') """ levels = name.split('.') arr = self for name in levels[:-1]: if not arr.has_key(name): arr[name] = {} arr = arr[name] arr[levels[-1]] = value def getset(self,name,value): """Get cache, if not exists set it and return set value Code: Config().getset('opt.opt_level2.key','default_value') """ g = self.get(name) if not g: g = value self.set(name,g) return gdef scache(func): def wrapper(*args, **kwargs): cache = SimpleCache.getInstance() fn = "scache." + func.__module__ + func.__class__.__name__ + \ func.__name__ + str(args) + str(kwargs) val = cache.get(fn) if not val: res = func(*args, **kwargs) cache.set(fn,res) return res return val return wrapper
相关内容
- python解决八皇后算法,python皇后算法,python解决经典算法
- python下载Gmail邮箱中的所有邮件,pythongmail,#!/usr/bin/p
- python管理windows的环境变量,python环境变量,下面的代码
- python webpy中显示进程中的所有类型对象占用的内存大小
- python读写文件,和设置文件的字符编码比如utf-8,pyth
- python抓取图片示例,,[Python]代码#!
- Python sql server和postgresql的表结构转换,pythonpostgresql,[
- Python 计算已经过去多少个周末,python已经过去,计算已
- python通过MySQLdb访问mysql数据库,mysqldbmysql,需要安装My
- python使用PIL库提取图片的EXIF数据,并做重命名,,pyth
评论关闭