zoukankan      html  css  js  c++  java
  • python的cache修饰器

    简单的memory cache。可以用来内存缓存任意函数方法。

    #!/usr/bin/python
     
    import functools
    from threading import RLock
    import logging
     
     
    LOGGER = logging.getLogger(__name__)
    
    class CacheNullValue(object):
        pass
     
    _Null = CacheNullValue()
     
    class Cache(object):
     
        def __init__(self, cache_limit=1000):
            self._cache = {}
            self._queue = []
            self.cache_limit = cache_limit
            self._lock = RLock()
     
        def __getitem__(self, name):
            with self._lock:
                return self._cache.get(name, _Null)
        get = __getitem__
     
        def __setitem__(self, name, value):
            with self._lock:
                if len(self._queue) >= self.cache_limit:
                    del self._cache[self._queue.pop(0)]
                self._queue.append(name)
                self._cache[name] = value
        set = __setitem__
     
     
    def cache(cache_limit):
        """A function decorator for method cache store
        """
        
        def cached(f):
            @functools.wraps(f)
            def _cached(*args, **kwargs):
                cid = repr((args, kwargs))
                data = _cached.cache.get(cid)
                if data != _Null:
                    LOGGER.debug('hit cached `' + _cached.__name__ + '` for ' + cid)
                    return data
                data = _cached.f(*args, **kwargs)
                _cached.cache.set(cid, data)
                return data
     
            _cached.f = f
            _cached.cache = Cache(cache_limit)
     
            return _cached
     
        return cached
     
     
    def setup(cache_limit):
        """ A global thread safe key-value store
        """
        global _cache_object
        _cache_object = Cache(cache_limit)
     
     
    def set(cid, data):
     
        _cache_object.set(cid, data)
     
     
    def get(cid):
        return _cache_object.get(cid)
     
     
    if __name__ == '__main__':
     
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(levelname)-8s %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S', filemode='a+')
     
        class T(object):
     
            @cache(100)
            def c(self, t):
                return t
     
        @cache(100)
        def cc(t):
            return t
     
        t = T()
        print t.c('hello')
        print t.c('hello')
        print t.c('h')
        print t.c('hello')
        print cc('he')
        print cc('he')
  • 相关阅读:
    python之路_初始mysql数据库
    python之路_并发编程之IO模型
    python之路_并发编程之协程
    python之路_并发编程之多线程2
    python之路_并发编程之多线程1
    python之路_并发编程之多进程3
    python之路_并发编程之多进程2
    python之路_并发编程之多进程1
    python之路_基于udp协议编程
    python之路_基于tcp协议的粘包现象
  • 原文地址:https://www.cnblogs.com/nagi/p/4191461.html
Copyright © 2011-2022 走看看