snapcache


Namesnapcache JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://del.wang
SummarySnap-in memory caching for Python, no persistence, just pure function result caching.
upload_time2024-11-09 09:18:27
maintainerDel Wang
docs_urlNone
authorDel Wang
requires_python<4.0,>=3.8
licenseMIT
keywords cache functools lru_cache unhashable
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SnapCache

<a href="https://pypi.python.org/pypi/snapcache"><img src="http://img.shields.io/pypi/v/snapcache.svg" alt="Latest version on PyPI"></a> <a href="https://pypi.python.org/pypi/snapcache"><img src="https://img.shields.io/pypi/pyversions/snapcache.svg" alt="Compatible Python versions."></a>

Snap-in memory caching for Python, no persistence, just pure function result caching.

## Motivation

Python’s built-in `functools.lru_cache` offers an easy way to cache function results, but it has limitations. For example, attempting to cache complex objects like `NumPy` arrays results in a `TypeError: unhashable type: 'numpy.ndarray'`.

**SnapCache** addresses this issue by offering a similar simple decorator interface while supporting caching for any data structure ✨.

## Installation

To install **SnapCache**, use `pip`:

```bash
pip install snapcache
```

## Usage

### Function-level Caching

You can cache function results using the `WithCache` decorator. Just apply it to any function you want to memoize.

```python
from snapcache import WithCache

@WithCache(maxsize=3)
def add(a, b):
    return a + b

# Call the function multiple times
print(add(1, 2))  # First call, result is computed and cached
print(add(1, 2))  # Second call, result is retrieved from cache
```

### Method-level Caching

You can also apply the decorator to class methods:

```python
class Foo:
    @WithCache(maxsize=3)
    def bar(self, a, b):
        return a - b

foo = Foo()
print(foo.bar(10, 5))  # First call, result is computed and cached
print(foo.bar(10, 5))  # Second call, result is retrieved from cache
```

## License

This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://del.wang",
    "name": "snapcache",
    "maintainer": "Del Wang",
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": "hello@xbox.work",
    "keywords": "cache, functools, lru_cache, unhashable",
    "author": "Del Wang",
    "author_email": "hello@xbox.work",
    "download_url": "https://files.pythonhosted.org/packages/70/1e/77a4a03c5befb8023b98953ff075b7f2d68234b617c4583d1f41901060bc/snapcache-1.0.0.tar.gz",
    "platform": null,
    "description": "# SnapCache\n\n<a href=\"https://pypi.python.org/pypi/snapcache\"><img src=\"http://img.shields.io/pypi/v/snapcache.svg\" alt=\"Latest version on PyPI\"></a> <a href=\"https://pypi.python.org/pypi/snapcache\"><img src=\"https://img.shields.io/pypi/pyversions/snapcache.svg\" alt=\"Compatible Python versions.\"></a>\n\nSnap-in memory caching for Python, no persistence, just pure function result caching.\n\n## Motivation\n\nPython\u2019s built-in `functools.lru_cache` offers an easy way to cache function results, but it has limitations. For example, attempting to cache complex objects like `NumPy` arrays results in a `TypeError: unhashable type: 'numpy.ndarray'`.\n\n**SnapCache** addresses this issue by offering a similar simple decorator interface while supporting caching for any data structure \u2728.\n\n## Installation\n\nTo install **SnapCache**, use `pip`:\n\n```bash\npip install snapcache\n```\n\n## Usage\n\n### Function-level Caching\n\nYou can cache function results using the `WithCache` decorator. Just apply it to any function you want to memoize.\n\n```python\nfrom snapcache import WithCache\n\n@WithCache(maxsize=3)\ndef add(a, b):\n    return a + b\n\n# Call the function multiple times\nprint(add(1, 2))  # First call, result is computed and cached\nprint(add(1, 2))  # Second call, result is retrieved from cache\n```\n\n### Method-level Caching\n\nYou can also apply the decorator to class methods:\n\n```python\nclass Foo:\n    @WithCache(maxsize=3)\n    def bar(self, a, b):\n        return a - b\n\nfoo = Foo()\nprint(foo.bar(10, 5))  # First call, result is computed and cached\nprint(foo.bar(10, 5))  # Second call, result is retrieved from cache\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Snap-in memory caching for Python, no persistence, just pure function result caching.",
    "version": "1.0.0",
    "project_urls": {
        "Changelog": "https://github.com/idootop/snapcache/blob/main/CHANGELOG.md",
        "Documentation": "https://github.com/idootop/snapcache/README.md",
        "Homepage": "https://del.wang",
        "Repository": "https://github.com/idootop/snapcache"
    },
    "split_keywords": [
        "cache",
        " functools",
        " lru_cache",
        " unhashable"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "451901ab712570e6cb460e4e349865286c8549758e9908502fadcba30165b588",
                "md5": "a43eacfcd81c567799a8f5a012450c84",
                "sha256": "524aea906b696879a27ccdeea18e7f518cad325ea8a0acfdbb69efd9fce2ec27"
            },
            "downloads": -1,
            "filename": "snapcache-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a43eacfcd81c567799a8f5a012450c84",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 3236,
            "upload_time": "2024-11-09T09:18:25",
            "upload_time_iso_8601": "2024-11-09T09:18:25.178342Z",
            "url": "https://files.pythonhosted.org/packages/45/19/01ab712570e6cb460e4e349865286c8549758e9908502fadcba30165b588/snapcache-1.0.0-py3-none-any.whl",
            "yanked": true,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "701e77a4a03c5befb8023b98953ff075b7f2d68234b617c4583d1f41901060bc",
                "md5": "498eae6673501bc36902a5169cb108f2",
                "sha256": "ef48ae47c2c6aa90be8df772d47dc5dd9859892f8873917f09d6223bc6c8edc7"
            },
            "downloads": -1,
            "filename": "snapcache-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "498eae6673501bc36902a5169cb108f2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 2799,
            "upload_time": "2024-11-09T09:18:27",
            "upload_time_iso_8601": "2024-11-09T09:18:27.128573Z",
            "url": "https://files.pythonhosted.org/packages/70/1e/77a4a03c5befb8023b98953ff075b7f2d68234b617c4583d1f41901060bc/snapcache-1.0.0.tar.gz",
            "yanked": true,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-09 09:18:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "idootop",
    "github_project": "snapcache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "snapcache"
}
        
Elapsed time: 0.35085s