{ "info": { "author": "Vasyl Paliy", "author_email": "vpaliy97@gmail.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "# LRU Cache\n\n[![Build Status](https://travis-ci.org/vpaliy/lru-cache.svg?branch=master)](https://travis-ci.org/vpaliy/lru-cache)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![codecov](https://codecov.io/gh/vpaliy/lru-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/vpaliy/lru-cache)\n\nThis repository contains a dictionary-like data structure, supporting LRU caching semantics and data expiration mechanism. You can add a new record to the cache and assign an expiration time for that record. Records are not required to have the same \"life span\": you can mix them up, and it will still work.\n\n### How does it work?\nLRU cache uses a daemon thread - AKA cache cleaner - to silently clean up expired items in the background. The daemon thread receives proxied objects from a shared queue, picks up the one with the shortest life span, and uses a condition variable to wait until the record expires.\n\n### Install\n\n`pip install lru-cache`\n\nor:\n\n```\n $ git clone https://github.com/vpaliy/lru-cache.git\n $ cd lru-cache/\n```\n\n\n### Usage\n\n```python\nimport time\nfrom lru import LruCache\n\n# every record will expire in 5 seconds unless otherwise specified\ncache = LruCache(maxsize=10, expires=5)\ncache['foo'] = 'bar'\n\nprint(cache['foo']) # prints 'bar'\n\n# sleep for 5 minutes\ntime.sleep(5)\n\nprint(cache['foo']) # KeyError\n\n# adding a new item that expires in 10 seconds\ncache.add(key='foo', value='bar', expires=10)\n\n# deleting\ndel cache['foo']\n ```\n\nTo make the LRU cache thread-safe, just pass `concurrent=True` when constructing a new instance:\n\n```python\nfrom lru import LruCache\n\ncache = LruCache(maxsize=10, concurrent=True)\n```\n\nNote: LRU cache extends the `MutableMapping` interface from the standard library; therefore it supports all methods inherent to the standard mapping types in Python.\n\nAdditionally, you can use cache decorators:\n\n- `lru_cache(maxsize, expires)`\n- `lazy_cache(maxsize, expires)`\n\nBoth are memoization decorators that support data expiration. The difference is that `lru_cache` uses `LruCache` (obviously) under the hood, and `lazy_cache` uses the native `dict`.\n\nFor example, using `lazy_cache` is super easy:\n\n```python\nimport time\nfrom lru import lazy_cache\n\n# each new item will expire in 10 seconds\n@lazy_cache(maxsize=10, expires=10)\ndef function(first, second, third):\n # simulate performing a computationaly expensive task\n time.sleep(10)\n return first + second + third\n\nfunction(10, 10, 10) # sleeps for 10 seconds and returns 30\nfunction(10, 10, 10) # returns 30 instantaneously\n\ntime.sleep(10) # wait until expires\n\nfunction(10, 10, 10) # sleeps for 10 seconds because all cached results have expired\n\n```\n\nWhich one to use?\n\nIf your function requires the functionality of LRU cache (removing the least recently used records to give room to the new ones), then use `lru_cache`; otherwise if you just need an expiring caching mechaniism, use `lazy_cache`. Note that `lazy_cache` clears the entire cache when the number of records have reached `maxsize`.\n\n\n## License\n```\nMIT License\n\nCopyright (c) 2019 Vasyl Paliy\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/vpaliy/lru-cache", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "lru-expiry-cache", "package_url": "https://pypi.org/project/lru-expiry-cache/", "platform": "", "project_url": "https://pypi.org/project/lru-expiry-cache/", "project_urls": { "Homepage": "https://github.com/vpaliy/lru-cache" }, "release_url": "https://pypi.org/project/lru-expiry-cache/1.0/", "requires_dist": [ "six", "future-fstrings" ], "requires_python": ">=2.7", "summary": "LRU cache with that supports data expiration", "version": "1.0" }, "last_serial": 4678328, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "aada642fb4a263f7ff67cde510186258", "sha256": "30acd1ab5248e777b52c681ca025179a8721979c884246c4fa8cb75a224e2f4d" }, "downloads": -1, "filename": "lru_expiry_cache-1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "aada642fb4a263f7ff67cde510186258", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 12132, "upload_time": "2019-01-09T20:26:08", "url": "https://files.pythonhosted.org/packages/ce/82/312f2104798e45e4057fcc37952e1f938e1c15c3b746f5012ffc8c91e212/lru_expiry_cache-1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "544bcd37f2fef4abb298a980d7b11504", "sha256": "db9d4f62e8a85e005a1fe4dfedf6045f12d2e490a95f9e054e567c3538e41b27" }, "downloads": -1, "filename": "lru-expiry-cache-1.0.tar.gz", "has_sig": false, "md5_digest": "544bcd37f2fef4abb298a980d7b11504", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 9527, "upload_time": "2019-01-09T20:26:10", "url": "https://files.pythonhosted.org/packages/c4/02/dbb8d8ab6c4f2a7d08bf5ec0b1212b946359a675a9e05944c4c1add6ff2f/lru-expiry-cache-1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "aada642fb4a263f7ff67cde510186258", "sha256": "30acd1ab5248e777b52c681ca025179a8721979c884246c4fa8cb75a224e2f4d" }, "downloads": -1, "filename": "lru_expiry_cache-1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "aada642fb4a263f7ff67cde510186258", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 12132, "upload_time": "2019-01-09T20:26:08", "url": "https://files.pythonhosted.org/packages/ce/82/312f2104798e45e4057fcc37952e1f938e1c15c3b746f5012ffc8c91e212/lru_expiry_cache-1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "544bcd37f2fef4abb298a980d7b11504", "sha256": "db9d4f62e8a85e005a1fe4dfedf6045f12d2e490a95f9e054e567c3538e41b27" }, "downloads": -1, "filename": "lru-expiry-cache-1.0.tar.gz", "has_sig": false, "md5_digest": "544bcd37f2fef4abb298a980d7b11504", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 9527, "upload_time": "2019-01-09T20:26:10", "url": "https://files.pythonhosted.org/packages/c4/02/dbb8d8ab6c4f2a7d08bf5ec0b1212b946359a675a9e05944c4c1add6ff2f/lru-expiry-cache-1.0.tar.gz" } ] }