persist-cache


Namepersist-cache JSON
Version 0.3.2 PyPI version JSON
download
home_pageNone
SummaryAn easy-to-use Python library for lightning-fast persistent function caching.
upload_time2024-03-21 02:27:11
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords cache caching decorator function functions local memoisation memoise memoization memoize optimisation optimise optimising optimization optimize optimizing performance persistent speed
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # persist-cache
<a href="https://pypi.org/project/persist-cache/" alt="PyPI Version"><img src="https://img.shields.io/pypi/v/persist-cache"></a> <a href="https://github.com/umarbutler/persist-cache/actions/workflows/ci.yml" alt="Build Status"><img src="https://img.shields.io/github/actions/workflow/status/umarbutler/persist-cache/ci.yml?branch=main"></a> <a href="https://app.codecov.io/gh/umarbutler/persist-cache" alt="Code Coverage"><img src="https://img.shields.io/codecov/c/github/umarbutler/persist-cache"></a> <!-- <a href="https://pypistats.org/packages/persist-cache" alt="Downloads"><img src="https://img.shields.io/pypi/dm/persist-cache"></a> -->

`persist-cache` is an *easy-to-use* Python library for lightning-fast persistent function caching. It is capable of caching both synchronous and asynchronous functions as well as methods, and is also process-safe and thread-safe.

## Features 🎯
- **⚡ Lightning-fast**: a function call can be cached in as little as 145 microseconds and be returned back in as few as 95 microseconds.
- **💽 Persistent**: cached returns persist across sessions and are stored locally.
- **⌛ Stale-free**: cached returns may be given a shelf life, after which they will be automatically flushed out.
- **🦺 Process- and thread-safe**: interprocess file locks prevent processes and threads from writing over each other.
- **⏱️ Async-compatible**: asynchronous functions can be cached with the same decorator as synchronous ones.
- **👨‍🏫 Class-compatible**: methods can be cached with the same decorator as functions (although the `self` argument is always ignored).

## Installation 🧑‍🔧
`persist-cache` may be installed with `pip`:
```bash
pip install persist-cache
```

## Usage 👩‍💻
The code snippet below demonstrates how both synchronous and asynchronous functions as well as methods may be cached with `persist-cache`:
```python
from persist_cache import cache

@cache
def my_function(): ...

@cache
async def my_function(): ...

class MyClass:
    @cache
    def my_method(self): ...

    @cache
    async def my_method(self): ...
```

It is also possible to name caches and specify their shelf life:
```python
from datetime import timedelta

@cache(name='my_shared_cache', expiry=timedelta(months=1))
def my_function(): ...

@cache(name='my_shared_cache', expiry=60 * 60 * 24 * 30)
def my_other_function(): ...
```

Once created, cached functions may be managed as follows:
```python
my_function.set_expiry(60 * 60) # Change cached returns to expire after an hour.
my_function.flush_cache() # Flush out any expired cached returns.
my_function.clear_cache() # Clear out all cached returns.
my_function.delete_cache() # Delete the cache.
```

## API 🧩
### `cache()`
```python
def cache(
    name: str | Callable | None = None,
    dir: str | None = None,
    expiry: int | float | timedelta | None = None,
) -> None
```

`cache()` persistently and locally cache the returns of a function.
    
The function to be cached must accept and return [dillable](https://dill.readthedocs.io/en/latest/) objects only (with the exception of methods' `self` argument, which is always ignored). Additionally, for consistent caching across subsequent sessions, arguments and returns should also be hashable.
    
`name` represents the name of the cache (or, if `cache()` is being called as an argument-less decorator (ie, as `@cache` instead of `@cache(...)`), the function to be cached). It defaults to the qualified name of the function. If `dir` is set, `name` will be ignored.

`dir` represents the directory in which the cache should be stored. It defaults to a subdirectory named after the hash of the cache's name in a parent folder named '.persist_cache' in the current working directory.
        
`expiry` represents how long, in seconds or as a `timedelta`, function calls should persist in the cache. It defaults to `None`.

If `cache()` is called with arguments, a decorator that wraps the function to be cached will be returned, otherwise, the wrapped function itself will be returned.

After being wrapped, the cached function will have the following methods attached to it:
- `set_expiry(value: int | float | timedelta) -> None`: Sets the expiry of the cache.
- `flush_cache() -> None`: Flushes out any expired cached returns.
- `clear_cache() -> None`: Clears out all cached returns.
- `delete_cache() -> None`: Deletes the cache.

## Licence 📜
This library is licensed under the [MIT Licence](https://github.com/umarbutler/persist-cache/blob/main/LICENCE).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "persist-cache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "cache, caching, decorator, function, functions, local, memoisation, memoise, memoization, memoize, optimisation, optimise, optimising, optimization, optimize, optimizing, performance, persistent, speed",
    "author": null,
    "author_email": "Umar Butler <umar@umar.au>",
    "download_url": "https://files.pythonhosted.org/packages/20/7b/4835a5a2d13d69c29b0381fe47e4db543c73ac95ba19fbff08c040a25b3e/persist_cache-0.3.2.tar.gz",
    "platform": null,
    "description": "# persist-cache\n<a href=\"https://pypi.org/project/persist-cache/\" alt=\"PyPI Version\"><img src=\"https://img.shields.io/pypi/v/persist-cache\"></a> <a href=\"https://github.com/umarbutler/persist-cache/actions/workflows/ci.yml\" alt=\"Build Status\"><img src=\"https://img.shields.io/github/actions/workflow/status/umarbutler/persist-cache/ci.yml?branch=main\"></a> <a href=\"https://app.codecov.io/gh/umarbutler/persist-cache\" alt=\"Code Coverage\"><img src=\"https://img.shields.io/codecov/c/github/umarbutler/persist-cache\"></a> <!-- <a href=\"https://pypistats.org/packages/persist-cache\" alt=\"Downloads\"><img src=\"https://img.shields.io/pypi/dm/persist-cache\"></a> -->\n\n`persist-cache` is an *easy-to-use* Python library for lightning-fast persistent function caching. It is capable of caching both synchronous and asynchronous functions as well as methods, and is also process-safe and thread-safe.\n\n## Features \ud83c\udfaf\n- **\u26a1 Lightning-fast**: a function call can be cached in as little as 145 microseconds and be returned back in as few as 95 microseconds.\n- **\ud83d\udcbd Persistent**: cached returns persist across sessions and are stored locally.\n- **\u231b Stale-free**: cached returns may be given a shelf life, after which they will be automatically flushed out.\n- **\ud83e\uddba Process- and thread-safe**: interprocess file locks prevent processes and threads from writing over each other.\n- **\u23f1\ufe0f Async-compatible**: asynchronous functions can be cached with the same decorator as synchronous ones.\n- **\ud83d\udc68\u200d\ud83c\udfeb Class-compatible**: methods can be cached with the same decorator as functions (although the `self` argument is always ignored).\n\n## Installation \ud83e\uddd1\u200d\ud83d\udd27\n`persist-cache` may be installed with `pip`:\n```bash\npip install persist-cache\n```\n\n## Usage \ud83d\udc69\u200d\ud83d\udcbb\nThe code snippet below demonstrates how both synchronous and asynchronous functions as well as methods may be cached with `persist-cache`:\n```python\nfrom persist_cache import cache\n\n@cache\ndef my_function(): ...\n\n@cache\nasync def my_function(): ...\n\nclass MyClass:\n    @cache\n    def my_method(self): ...\n\n    @cache\n    async def my_method(self): ...\n```\n\nIt is also possible to name caches and specify their shelf life:\n```python\nfrom datetime import timedelta\n\n@cache(name='my_shared_cache', expiry=timedelta(months=1))\ndef my_function(): ...\n\n@cache(name='my_shared_cache', expiry=60 * 60 * 24 * 30)\ndef my_other_function(): ...\n```\n\nOnce created, cached functions may be managed as follows:\n```python\nmy_function.set_expiry(60 * 60) # Change cached returns to expire after an hour.\nmy_function.flush_cache() # Flush out any expired cached returns.\nmy_function.clear_cache() # Clear out all cached returns.\nmy_function.delete_cache() # Delete the cache.\n```\n\n## API \ud83e\udde9\n### `cache()`\n```python\ndef cache(\n    name: str | Callable | None = None,\n    dir: str | None = None,\n    expiry: int | float | timedelta | None = None,\n) -> None\n```\n\n`cache()` persistently and locally cache the returns of a function.\n    \nThe function to be cached must accept and return [dillable](https://dill.readthedocs.io/en/latest/) objects only (with the exception of methods' `self` argument, which is always ignored). Additionally, for consistent caching across subsequent sessions, arguments and returns should also be hashable.\n    \n`name` represents the name of the cache (or, if `cache()` is being called as an argument-less decorator (ie, as `@cache` instead of `@cache(...)`), the function to be cached). It defaults to the qualified name of the function. If `dir` is set, `name` will be ignored.\n\n`dir` represents the directory in which the cache should be stored. It defaults to a subdirectory named after the hash of the cache's name in a parent folder named '.persist_cache' in the current working directory.\n        \n`expiry` represents how long, in seconds or as a `timedelta`, function calls should persist in the cache. It defaults to `None`.\n\nIf `cache()` is called with arguments, a decorator that wraps the function to be cached will be returned, otherwise, the wrapped function itself will be returned.\n\nAfter being wrapped, the cached function will have the following methods attached to it:\n- `set_expiry(value: int | float | timedelta) -> None`: Sets the expiry of the cache.\n- `flush_cache() -> None`: Flushes out any expired cached returns.\n- `clear_cache() -> None`: Clears out all cached returns.\n- `delete_cache() -> None`: Deletes the cache.\n\n## Licence \ud83d\udcdc\nThis library is licensed under the [MIT Licence](https://github.com/umarbutler/persist-cache/blob/main/LICENCE).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "An easy-to-use Python library for lightning-fast persistent function caching.",
    "version": "0.3.2",
    "project_urls": {
        "Documentation": "https://github.com/umarbutler/persist-cache/blob/main/README.md",
        "Homepage": "https://github.com/umarbutler/persist-cache",
        "Issues": "https://github.com/umarbutler/persist-cache/issues",
        "Source": "https://github.com/umarbutler/persist-cache"
    },
    "split_keywords": [
        "cache",
        " caching",
        " decorator",
        " function",
        " functions",
        " local",
        " memoisation",
        " memoise",
        " memoization",
        " memoize",
        " optimisation",
        " optimise",
        " optimising",
        " optimization",
        " optimize",
        " optimizing",
        " performance",
        " persistent",
        " speed"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bdb01531fbbfeb023fd1b41bf29205212ff4c005dde8dbf827df97bdf7f22df9",
                "md5": "c0c942728b42c77e3c628716547135cf",
                "sha256": "c7c70072e2bf6ff785f782cbf85e5c56544ce3c07c24a4469c85386f62e1d3f4"
            },
            "downloads": -1,
            "filename": "persist_cache-0.3.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c0c942728b42c77e3c628716547135cf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 11468,
            "upload_time": "2024-03-21T02:27:09",
            "upload_time_iso_8601": "2024-03-21T02:27:09.506946Z",
            "url": "https://files.pythonhosted.org/packages/bd/b0/1531fbbfeb023fd1b41bf29205212ff4c005dde8dbf827df97bdf7f22df9/persist_cache-0.3.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "207b4835a5a2d13d69c29b0381fe47e4db543c73ac95ba19fbff08c040a25b3e",
                "md5": "134761e4ad93527a77e4b3b41e3d0b92",
                "sha256": "652c132f09d934e415335acc461ec632bf2914f993db6985002777946532b819"
            },
            "downloads": -1,
            "filename": "persist_cache-0.3.2.tar.gz",
            "has_sig": false,
            "md5_digest": "134761e4ad93527a77e4b3b41e3d0b92",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 50150,
            "upload_time": "2024-03-21T02:27:11",
            "upload_time_iso_8601": "2024-03-21T02:27:11.982593Z",
            "url": "https://files.pythonhosted.org/packages/20/7b/4835a5a2d13d69c29b0381fe47e4db543c73ac95ba19fbff08c040a25b3e/persist_cache-0.3.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-21 02:27:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "umarbutler",
    "github_project": "persist-cache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "persist-cache"
}
        
Elapsed time: 0.19780s