Skip to main content

C implementation of Python 3 functools.lru_cache

Project description

C implementation of Python 3 functools.lru_cache. Provides speedup of 10-30x over standard library. Passes test suite from standard library for lru_cache.

Provides 2 Least Recently Used caching function decorators:

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.

View the cache statistics named tuple (hits, misses, maxsize, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.__wrapped__.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastcache-1.1.0.tar.gz (20.2 kB view details)

Uploaded Source

File details

Details for the file fastcache-1.1.0.tar.gz.

File metadata

  • Download URL: fastcache-1.1.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.15

File hashes

Hashes for fastcache-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6de1b16e70335b7bde266707eb401a3aaec220fb66c5d13b02abf0eab8be782b
MD5 fff901f2f906d7a32098949fa26204e6
BLAKE2b-256 5fa3b280cba4b4abfe5f5bdc643e6c9d81bf3b9dc2148a11e5df06b6ba85a560

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page