python 3 functools.lru缓存的c实现

fastcache的Python项目详细描述


python 3 functools.lru_缓存的c实现。提供10-30倍的加速 超过标准库。通过标准库中的LRU缓存测试套件。

提供两个最近最少使用的缓存函数装饰器:

clru_cache - built-in (faster)
>>> from fastcache import clru_cache, __version__
>>> __version__
'1.1.0'
>>> @clru_cache(maxsize=325, typed=False)
... def fib(n):
...     """Terrible Fibonacci number generator."""
...     return n if n < 2 else fib(n-1) + fib(n-2)
...
>>> fib(300)
222232244629420445529739893461909967206666939096499764990979600
>>> fib.cache_info()
CacheInfo(hits=298, misses=301, maxsize=325, currsize=301)
>>> print(fib.__doc__)
Terrible Fibonacci number generator.
>>> fib.cache_clear()
>>> fib.cache_info()
CacheInfo(hits=0, misses=0, maxsize=325, currsize=0)
>>> fib.__wrapped__(300)
222232244629420445529739893461909967206666939096499764990979600
>>> type(fib)
>>> <class 'fastcache.clru_cache'>
lru_cache - python wrapper around clru_cache
>>> from fastcache import lru_cache
>>> @lru_cache(maxsize=128, typed=False)
... def f(a, b):
...     pass
...
>>> type(f)
>>> <class 'function'>

(c)lru_cache(maxsize=128, typed=False, state=None, unhashable=’error’)

Least-recently-used cache decorator.

If maxsize is set to None, the LRU features are disabled and the cache can grow without bound.

If typed is True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results.

If state is a list or dict, the items will be incorporated into the argument hash.

The result of calling the cached function with unhashable (mutable) arguments depends on the value of unhashable:

If unhashable is ‘error’, a TypeError will be raised.

If unhashable is ‘warning’, a UserWarning will be raised, and the wrapped function will be called with the supplied arguments. A miss will be recorded in the cache statistics.

If unhashable is ‘ignore’, the wrapped function will be called with the supplied arguments. A miss will will be recorded in the cache statistics.

查看名为tuple的缓存统计信息(命中、未命中、maxsize、currsize) 使用f.cache_info()。使用f.cache_clear()清除缓存和统计信息。 使用f.\uu wrapped访问底层函数。

见:http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java爬虫获取外部网站搜索结果   java Bluestack未连接到eclipse   java如何从ConstraintViolationException Hibernamte获取数据库字段名   HttpResponse HttpResponse=httpClient引发java运行时错误。执行(httpPost);   Jama中矩阵的java点积和叉积   java有什么方法可以唯一地识别可扩展设备吗?   java我需要用*来写我的名字,但我不断遇到一个错误,我对编码很陌生   java变量是在内部类中访问的。需要被宣布为最终决定。但我不想宣布最终结果   java如何缩短base64图像字符串,Android?   JavaSpringMVC:计划方法不自动触发   图形学习Java 2D API的好资源是什么?   如何在java中对方法进行排队   java JavaFX多行   java Selenium无法在[链接]上找到基于CSS元素的密码字段元素http://www.cartasi.it/gtwpages/index.jsp   Java中的equals()和hashCode()契约   软删除情况下的java Hibernate二级缓存   java为什么这段代码要两次调用这些方法?