{ "info": { "author": "Sven R. Kunze", "author_email": "srkunze@mail.de", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python" ], "description": "`XCACHE `_\n===============================================\n\nIt's like `lru_caches `_ but can be cleared automatically or via context managers.\n\n\nWhy?\n----\n\n`There are only two hard things in Computer Science: cache invalidation and naming things. `_\n\n-- Phil Karlton\n\nSo, caches are fairly easy, but their invalidation is kind of hard to get right.\n\n\nWhat type of cache is used?\n---------------------------\n\nThe default implementation is the lru_cache of the stdlib but you can drop in your own.\n\n\nHow?\n----\n\nPython features garbage collection aka. automatic memory management. Let's make use of it:\n\n.. code:: python\n\n from xcache import cache_gen\n\n request_cache = cache_gen(lambda: request) # default cache_impl=lru_cache\n\n\nHere, we defined our own cache that needs the global request for invalidating the cache.\nAs soon as ``request`` is garbage-collected, the cached data is freed.\n\n.. code:: python\n\n @request_cache()\n def fib(n):\n return fib(n-1) + fib(n-2) if n > 1 else 1\n\n\nHere, we see how to decorate a function to work like a lru_cache.\n\nNow, let's see how this works:\n\n.. code:: python\n\n class Request(): pass\n\n request = Request()\n\n print(fib(10))\n print(fib(20))\n print(fib.cache_info()) # fib cache contains 2 items\n\n request = Request() # invalidates all caches\n\n print(fib.cache_info()) # fib cache contains 0 items\n print(fib(10))\n print(fib(20))\n\n\nContext Manager for more more control over cache invalidation\n-------------------------------------------------------------\n\nIf you need more control, the context manager ``clean_caches`` is what you need:\n\n.. code:: python\n\n from xcache import cached, clean_caches\n\n @cached()\n def fib(n):\n return fib(n-1) + fib(n-2) if n > 1 else 1\n\n with clean_caches():\n print(fib(10))\n print(fib(20))\n print(fib.cache_info()) # fib cache contains 2 items\n print(fib.cache_info()) # fib cache contains 0 items\n\n\nYou can even specify what object the caches should be attached to:\n\n.. code:: python\n\n @cached()\n def fib(n):\n return fib(n-1) + fib(n-2) if n > 1 else 1\n\n with clean_caches(Request()) as request:\n print(fib(10))\n print(fib(20))\n print(fib.cache_info()) # fib cache contains 2 items\n print(fib.cache_info()) # fib cache contains 0 items\n\n\nCan these context managers be nested?\n-------------------------------------\n\nSure. At each entrance and and exit of each context, all associated caches are emptied.\n\n\nConclusion\n----------\n\nGood\n****\n\n- cache invalidation done easy\n- works via garbage collection\n- works via context managers\n- works with Python2 and Python3\n\nBad\n***\n\n- unkown ;-)\n\n\nIdeas are always welcome. :-)\n", "description_content_type": null, "docs_url": null, "download_url": null, "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/srkunze/xcache", "keywords": null, "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "xcache", "package_url": "https://pypi.org/project/xcache/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/xcache/", "project_urls": { "Homepage": "https://github.com/srkunze/xcache" }, "release_url": "https://pypi.org/project/xcache/0.2/", "requires_dist": null, "requires_python": null, "summary": "clean caches when needed", "version": "0.2" }, "last_serial": 2021481, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "05a18bb56bcad24ab8ad4a29cc3802d1", "sha256": "a92fcebb8deeb6250f0b3ea7f7cb80a1625b714c509cac0567d0b52a596bd4fe" }, "downloads": -1, "filename": "xcache-0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "05a18bb56bcad24ab8ad4a29cc3802d1", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 2588, "upload_time": "2016-03-14T18:15:03", "url": "https://files.pythonhosted.org/packages/f6/b1/33894acd8ef9303ea8a80eba23a756e169854e4af89f0a17fbd0f78bd0c1/xcache-0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e25ab1cb6fda35391720fb54217aa678", "sha256": "763088dd5e3140bef0d8acf232fc21ca30ab5e22f2064bd9fe09dd1d8539a194" }, "downloads": -1, "filename": "xcache-0.1.tar.gz", "has_sig": false, "md5_digest": "e25ab1cb6fda35391720fb54217aa678", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3177, "upload_time": "2016-03-14T18:15:26", "url": "https://files.pythonhosted.org/packages/01/df/ac5ee0234e7eb8eec082e6ef19ef23ef231058191e4faef05b3a95a2ab2a/xcache-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "7b2b0f28b9630cf48c599b72e51115e9", "sha256": "ead03a7a87c1461cd4a00cbf7f36f960311e803219cfe9ae44d7d9340bdc8411" }, "downloads": -1, "filename": "xcache-0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "7b2b0f28b9630cf48c599b72e51115e9", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 2582, "upload_time": "2016-03-22T21:15:53", "url": "https://files.pythonhosted.org/packages/c7/24/f410a7c7e21cd8aa3b677761002ca4aff412156bd86f119401c7701b08dd/xcache-0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3c038c984243274ca505e376ff2c7ad3", "sha256": "baff70b9c911315ab96a11d262db1da55b594311358618b955db1aef01aa482c" }, "downloads": -1, "filename": "xcache-0.2.tar.gz", "has_sig": false, "md5_digest": "3c038c984243274ca505e376ff2c7ad3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3193, "upload_time": "2016-03-22T21:16:23", "url": "https://files.pythonhosted.org/packages/4e/14/30842615cf687f6267e83a166b6824b2b3c15381f38c4d2f39e9cfa273e6/xcache-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "7b2b0f28b9630cf48c599b72e51115e9", "sha256": "ead03a7a87c1461cd4a00cbf7f36f960311e803219cfe9ae44d7d9340bdc8411" }, "downloads": -1, "filename": "xcache-0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "7b2b0f28b9630cf48c599b72e51115e9", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 2582, "upload_time": "2016-03-22T21:15:53", "url": "https://files.pythonhosted.org/packages/c7/24/f410a7c7e21cd8aa3b677761002ca4aff412156bd86f119401c7701b08dd/xcache-0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3c038c984243274ca505e376ff2c7ad3", "sha256": "baff70b9c911315ab96a11d262db1da55b594311358618b955db1aef01aa482c" }, "downloads": -1, "filename": "xcache-0.2.tar.gz", "has_sig": false, "md5_digest": "3c038c984243274ca505e376ff2c7ad3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3193, "upload_time": "2016-03-22T21:16:23", "url": "https://files.pythonhosted.org/packages/4e/14/30842615cf687f6267e83a166b6824b2b3c15381f38c4d2f39e9cfa273e6/xcache-0.2.tar.gz" } ] }