{ "info": { "author": "lonelyenvoy", "author_email": "petrinchor@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "# python-memoization\n\n[![Version][aucsvg]][auc] [![Supports Python][pythonsvg]][python] [![Build Status][travismaster]][travis]\n[![Coverage Status][coverallssvg]][coveralls] [![Repository][repositorysvg]][repository] [![Downloads][downloadssvg]][repository]\n[![PRs welcome][prsvg]][pr] [![License][licensesvg]][license]\n\n[aucsvg]: https://img.shields.io/badge/memoization-v0.1.4-brightgreen.svg\n[auc]: https://github.com/lonelyenvoy/python-memoization\n\n[pythonsvg]: https://img.shields.io/pypi/pyversions/memoization.svg\n[python]: https://www.python.org\n\n[travismaster]: https://travis-ci.org/lonelyenvoy/python-memoization.svg?branch=master\n[travis]: https://travis-ci.org/lonelyenvoy/python-memoization\n\n[coverallssvg]: https://coveralls.io/repos/github/lonelyenvoy/python-memoization/badge.svg?branch=master\n[coveralls]: https://coveralls.io/github/lonelyenvoy/python-memoization?branch=master\n\n[repositorysvg]: https://img.shields.io/pypi/v/memoization.svg\n[repository]: https://pypi.org/project/memoization\n\n[downloadssvg]: https://img.shields.io/pypi/dm/memoization.svg\n\n[prsvg]: https://img.shields.io/badge/PRs-welcome-blue.svg\n[pr]: https://github.com/lonelyenvoy/python-memoization#contributing\n\n[licensesvg]: https://img.shields.io/badge/License-MIT-blue.svg\n[license]: https://github.com/lonelyenvoy/python-memoization/blob/master/LICENSE\n\nA powerful caching library for Python, with TTL support and multiple algorithm options.\n\n\n## Why choose this library?\n\nPerhaps you know about [```functools.lru_cache```](https://docs.python.org/3/library/functools.html#functools.lru_cache)\nin Python 3, and you may be wondering why I am reinventing the wheel.\n\nWell, actually not. This lib is based on ```functools```. Please find below the comparison with ```lru_cache```.\n\n|Features|```functools.lru_cache```|```memoization```|\n|--------|-------------------|-----------|\n|Configurable max size|\u2714\ufe0f|\u2714\ufe0f|\n|Thread safety|\u2714\ufe0f|\u2714\ufe0f|\n|Flexible argument typing (typed & untyped)|\u2714\ufe0f|Always typed|\n|Cache statistics|\u2714\ufe0f|\u2714\ufe0f|\n|LRU (Least Recently Used) as caching algorithm|\u2714\ufe0f|\u2714\ufe0f|\n|LFU (Least Frequently Used) as caching algorithm|No support|\u2714\ufe0f|\n|FIFO (First In First Out) as caching algorithm|No support|\u2714\ufe0f|\n|TTL (Time-To-Live) support|No support|\u2714\ufe0f|\n|Support for unhashable arguments (dict, list, etc.)|No support|\u2714\ufe0f|\n|Partial cache clearing|No support|Pending implementation in v0.2.x|\n|Python version|3.2+|2.6, 2.7, 3.4+|\n\n```memoization``` solves some drawbacks of ```functools.lru_cache```:\n\n1. ```lru_cache``` does not support __unhashable types__, which means function arguments cannot contain dict or list.\n\n```python\n>>> from functools import lru_cache\n>>> @lru_cache()\n... def f(x): return x\n... \n>>> f([1, 2]) # unsupported\nTraceback (most recent call last):\n File \"\", line 1, in \nTypeError: unhashable type: 'list'\n```\n\n2. ```lru_cache``` is vulnerable to [__hash collision attack__](https://learncryptography.com/hash-functions/hash-collision-attack)\n and can be hacked or compromised. In ```memoization```, caching is always typed, which means ```f(3)``` and \n ```f(3.0)``` will be treated as different calls and cached separately. This prevents the attack from happening\n (or at least makes it a lot harder).\n\n```python\n>>> hash((1,))\n3430019387558\n>>> hash(3430019387558.0) # two different arguments have an identical hash value\n3430019387558\n```\n\n\n## Installation\n\n```bash\npip install memoization\n```\n\n## Usage in 2 lines\n\n```python\nfrom memoization import cached\n\n@cached\ndef func(arg):\n ... # do something slow\n```\n\nSimple enough - the results of ```func()``` are cached. \nRepetitive calls to ```func()``` with the same arguments run ```func()``` only once, enhancing performance.\n\n\n## Advanced features\n\nConfigurable options include `ttl`, `max_size`, `algorithm` and `thread_safe`.\n\n### TTL (Time-To-Live)\n\n```python\n@cached(ttl=5) # the cache expires after 5 seconds\ndef expensive_db_query(user_id):\n ...\n```\n\nFor impure functions, TTL (in second) will be a solution. This will be useful when the function returns resources that is valid only for a short time, e.g. fetching something from databases.\n\n### Limited cache capacity\n\n```python\n@cached(max_size=128) # the cache holds no more than 128 items\ndef get_a_very_large_object(filename):\n ...\n```\n\nBy default, if you don't specify ```max_size```, the cache can hold unlimited number of items.\nWhen the cache is fully occupied, the former data will be overwritten by a certain algorithm described below.\n\n### Choosing your caching algorithm\n\n```python\nfrom memoization import cached, CachingAlgorithmFlag\n\n@cached(max_size=128, algorithm=CachingAlgorithmFlag.LFU) # the cache overwrites items using the LFU algorithm\ndef func(arg):\n ...\n```\n\nPossible values for ```algorithm``` are:\n\n- `CachingAlgorithmFlag.LRU`: _Least Recently Used_ (default)\n- `CachingAlgorithmFlag.LFU`: _Least Frequently Used_ \n- `CachingAlgorithmFlag.FIFO`: _First In First Out_ \n\nThis option is valid only when a ```max_size``` is explicitly specified.\n\n### Thread safe?\n\n```python\n@cached(thread_safe=False)\ndef func(arg):\n ...\n```\n\n```thread_safe``` is ```True``` by default. Setting it to ```False``` enhances performance.\n\n### Knowing how well the cache is behaving\n\n```python\n>>> @cached\n... def f(x): return x\n... \n>>> f.cache_info()\nCacheInfo(hits=0, misses=0, current_size=0, max_size=None, algorithm=, ttl=None, thread_safe=True)\n```\n\nWith ```cache_info```, you can retrieve the number of ```hits``` and ```misses``` of the cache, and other information indicating the caching status.\n\n- `hits`: the number of cache hits\n- `misses`: the number of cache misses\n- `current_size`: the number of items that were cached\n- `max_size`: the maximum number of items that can be cached (user-specified)\n- `algorithm`: caching algorithm (user-specified)\n- `ttl`: Time-To-Live value (user-specified)\n- `thread_safe`: whether the cache is thread safe (user-specified)\n\n### Other APIs\n\n- Access the original function ```f``` by ```f.__wrapped__```.\n- Clear the cache by ```f.cache_clear()```.\n\n## Contributing\n\nThis project welcomes contributions from anyone.\n- [Submit bugs](https://github.com/lonelyenvoy/python-memoization/issues) and help me verify fixes.\n- [Submit pull requests](https://github.com/lonelyenvoy/python-memoization/pulls) for bug fixes and features and discuss existing proposals. Please make sure that your PR passes the tests in ```test/```.\n\n## License\n\n[The MIT License](https://github.com/lonelyenvoy/python-memoization/blob/master/LICENSE)\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/lonelyenvoy/python-memoization", "keywords": "memoization memorization remember decorator cache caching function callablefunctional ttl limited capacity fast high-performance optimization", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "memoization", "package_url": "https://pypi.org/project/memoization/", "platform": "", "project_url": "https://pypi.org/project/memoization/", "project_urls": { "Homepage": "https://github.com/lonelyenvoy/python-memoization" }, "release_url": "https://pypi.org/project/memoization/0.1.4/", "requires_dist": null, "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4", "summary": "A powerful caching library for Python, with TTL support and multiple algorithm options. (https://github.com/lonelyenvoy/python-memoization)", "version": "0.1.4" }, "last_serial": 5260928, "releases": { "0.0.10": [ { "comment_text": "", "digests": { "md5": "fc59f33778f289d67130ca296598fb4e", "sha256": "8505f40330ec128d571a717bd45fdca5e15fc1b9f57d573b681d918ff1bc1618" }, "downloads": -1, "filename": "memoization-0.0.10.tar.gz", "has_sig": false, "md5_digest": "fc59f33778f289d67130ca296598fb4e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3200, "upload_time": "2018-08-16T13:28:10", "url": "https://files.pythonhosted.org/packages/9c/1b/68e687bf5b501bc3158f968c1b88e5148a4bfc6d289355d674b52905d7a2/memoization-0.0.10.tar.gz" } ], "0.0.6": [ { "comment_text": "", "digests": { "md5": "69c7a80c479e68c8bfffe1ed6a9cb7ed", "sha256": "38b0d8b592c420695b880a5afa01943c228ae497f34eb6fd40a2b703f959fe64" }, "downloads": -1, "filename": "memoization-0.0.6.tar.gz", "has_sig": false, "md5_digest": "69c7a80c479e68c8bfffe1ed6a9cb7ed", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 782, "upload_time": "2018-08-15T19:49:38", "url": "https://files.pythonhosted.org/packages/07/81/8d7fcf215f4b36f89e12bdadd8a29d28b3763560b238db365831732564c3/memoization-0.0.6.tar.gz" } ], "0.0.7": [ { "comment_text": "", "digests": { "md5": "e7dd6a3e6dcfe3fb979c65a021b798bc", "sha256": "c611ed913efe2727ad6efb20a1c14d8da12d90eda2c89c7245a4ec22c05893bf" }, "downloads": -1, "filename": "memoization-0.0.7.tar.gz", "has_sig": false, "md5_digest": "e7dd6a3e6dcfe3fb979c65a021b798bc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 962, "upload_time": "2018-08-16T12:13:04", "url": "https://files.pythonhosted.org/packages/5e/2e/0f721f7d24c53fdcb9c715186aeb65392727023d67ec3f58e3b092169e85/memoization-0.0.7.tar.gz" } ], "0.0.9": [ { "comment_text": "", "digests": { "md5": "492f4fc3eca27a73892597a1116d231d", "sha256": "21097958f3a33ff20134b45ced682727401310e7c57e9db96dfa622e4ea8e9c2" }, "downloads": -1, "filename": "memoization-0.0.9.tar.gz", "has_sig": false, "md5_digest": "492f4fc3eca27a73892597a1116d231d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 999, "upload_time": "2018-08-16T13:09:51", "url": "https://files.pythonhosted.org/packages/a4/96/5316661fc585d542f2764acbfd829136a1b037810f6e5c745651043efaa9/memoization-0.0.9.tar.gz" } ], "0.1.4": [ { "comment_text": "", "digests": { "md5": "deb9fb6384817a1a18768a4f62b71960", "sha256": "fb7116356b6183b3baae55aedc81f287200ad852be88730c3cca304844bd5793" }, "downloads": -1, "filename": "memoization-0.1.4-py3-none-any.whl", "has_sig": false, "md5_digest": "deb9fb6384817a1a18768a4f62b71960", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4", "size": 25333, "upload_time": "2019-05-13T07:04:00", "url": "https://files.pythonhosted.org/packages/08/fc/28e480593bb9fa3475f5bc9d4c2090e381aac1fb8fedf1c6678c9e7c10de/memoization-0.1.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c00887b545be063b5225a01851235a9b", "sha256": "bb141c5f950e32f6318ed50861876a4c47e551d816fd5b800af7e96ddb7c5615" }, "downloads": -1, "filename": "memoization-0.1.4.tar.gz", "has_sig": false, "md5_digest": "c00887b545be063b5225a01851235a9b", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4", "size": 24076, "upload_time": "2019-05-13T07:04:02", "url": "https://files.pythonhosted.org/packages/4b/60/023e8952dbebea9f6d6bdbbefcddf64303273969c12f64bdaa2f20551746/memoization-0.1.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "deb9fb6384817a1a18768a4f62b71960", "sha256": "fb7116356b6183b3baae55aedc81f287200ad852be88730c3cca304844bd5793" }, "downloads": -1, "filename": "memoization-0.1.4-py3-none-any.whl", "has_sig": false, "md5_digest": "deb9fb6384817a1a18768a4f62b71960", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4", "size": 25333, "upload_time": "2019-05-13T07:04:00", "url": "https://files.pythonhosted.org/packages/08/fc/28e480593bb9fa3475f5bc9d4c2090e381aac1fb8fedf1c6678c9e7c10de/memoization-0.1.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c00887b545be063b5225a01851235a9b", "sha256": "bb141c5f950e32f6318ed50861876a4c47e551d816fd5b800af7e96ddb7c5615" }, "downloads": -1, "filename": "memoization-0.1.4.tar.gz", "has_sig": false, "md5_digest": "c00887b545be063b5225a01851235a9b", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4", "size": 24076, "upload_time": "2019-05-13T07:04:02", "url": "https://files.pythonhosted.org/packages/4b/60/023e8952dbebea9f6d6bdbbefcddf64303273969c12f64bdaa2f20551746/memoization-0.1.4.tar.gz" } ] }