tempcache


Nametempcache JSON
Version 0.0.8 PyPI version JSON
download
home_page
SummaryPython library to cache data and function results in temporary files
upload_time2024-02-17 22:39:33
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT License
keywords caching tempfiles
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python library to cache data and function results in temporary files

This library offers a simple way to
cache data and function results using temporary files.
By default it will use the `pickle` module
to hash key values and serialize output data.
This is meant to be used with long running functions
that have repeatable results. The hashing of keys is dependent
on the pickle algorithm and so it may work accross different
python environments, as long as they have a compatible pickling algorithm.

To avoid possible collisions make sure to use
a unique name when instantiating `TempCache`.
You can also add a `source` argument as any opaque string
to further differentiate cache keys from other caches.


> **Note**
For more advanced use cases you may want to look at the `Memory` class
in [joblib](https://github.com/joblib/joblib).


## Basic Usage

An instance of the `TempCache` class be used as a decorator
to automatically cache the results of a function.

```python
from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
temp_cache = TempCache(__name__, max_age=CACHE_MAX_AGE)

@temp_cache
def long_running(...):
    ...

result = long_running(...)
```

## Caching results at the call site

You can also use a `TempCache` object to cache a result
at the call site with the `cache_result` method. 

```python
from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
temp_cache = TempCache(__name__, max_age=CACHE_MAX_AGE)

def long_running(...):
    ...

result = temp_cache.cache_result(long_running, ...)
```

## Advanced usage

In cases where the function or some of its arguments
are defined in the `__main__` namespace or in a jupyter notebook
and cannot be pickled by `pickle` you may want
to use a different pickle module like `cloupickle`.


```python
import cloudpickle

from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
temp_cache = TempCache("tempcache-foo",
                       pickler=cloudpickle,
                       max_age=CACHE_MAX_AGE)
```

## Examples

Examples notebooks are in the `extras` folder.

## Installation

You can install this package with `pip`.

```console
pip install tempcache
```

## Related Projects

- [joblib](https://github.com/joblib/joblib)
Computing with Python functions
- [percache](https://pypi.org/project/percache/)
Persistently cache results of callables
- [cloudpickle](https://github.com/cloudpipe/cloudpickle)
Extended pickling support for Python objects

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "tempcache",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "caching,tempfiles",
    "author": "",
    "author_email": "furechan <furechan@xsmail.com>",
    "download_url": "",
    "platform": null,
    "description": "# Python library to cache data and function results in temporary files\n\nThis library offers a simple way to\ncache data and function results using temporary files.\nBy default it will use the `pickle` module\nto hash key values and serialize output data.\nThis is meant to be used with long running functions\nthat have repeatable results. The hashing of keys is dependent\non the pickle algorithm and so it may work accross different\npython environments, as long as they have a compatible pickling algorithm.\n\nTo avoid possible collisions make sure to use\na unique name when instantiating `TempCache`.\nYou can also add a `source` argument as any opaque string\nto further differentiate cache keys from other caches.\n\n\n> **Note**\nFor more advanced use cases you may want to look at the `Memory` class\nin [joblib](https://github.com/joblib/joblib).\n\n\n## Basic Usage\n\nAn instance of the `TempCache` class be used as a decorator\nto automatically cache the results of a function.\n\n```python\nfrom tempcache import TempCache\n\nCACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days\ntemp_cache = TempCache(__name__, max_age=CACHE_MAX_AGE)\n\n@temp_cache\ndef long_running(...):\n    ...\n\nresult = long_running(...)\n```\n\n## Caching results at the call site\n\nYou can also use a `TempCache` object to cache a result\nat the call site with the `cache_result` method. \n\n```python\nfrom tempcache import TempCache\n\nCACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days\ntemp_cache = TempCache(__name__, max_age=CACHE_MAX_AGE)\n\ndef long_running(...):\n    ...\n\nresult = temp_cache.cache_result(long_running, ...)\n```\n\n## Advanced usage\n\nIn cases where the function or some of its arguments\nare defined in the `__main__` namespace or in a jupyter notebook\nand cannot be pickled by `pickle` you may want\nto use a different pickle module like `cloupickle`.\n\n\n```python\nimport cloudpickle\n\nfrom tempcache import TempCache\n\nCACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days\ntemp_cache = TempCache(\"tempcache-foo\",\n                       pickler=cloudpickle,\n                       max_age=CACHE_MAX_AGE)\n```\n\n## Examples\n\nExamples notebooks are in the `extras` folder.\n\n## Installation\n\nYou can install this package with `pip`.\n\n```console\npip install tempcache\n```\n\n## Related Projects\n\n- [joblib](https://github.com/joblib/joblib)\nComputing with Python functions\n- [percache](https://pypi.org/project/percache/)\nPersistently cache results of callables\n- [cloudpickle](https://github.com/cloudpipe/cloudpickle)\nExtended pickling support for Python objects\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Python library to cache data and function results in temporary files",
    "version": "0.0.8",
    "project_urls": {
        "homepage": "https://github.com/furechan/tempcache"
    },
    "split_keywords": [
        "caching",
        "tempfiles"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "17c530182aa3d2b5d11ea27de410cfab70ae1537c3f7bf6e68b13793aa6878fe",
                "md5": "328671355a16b3dc2d9832beb76edf62",
                "sha256": "0b420161cd4d3fad2a40cc030dcd45bc1053409e9917fc7fa51b14bd028b6b52"
            },
            "downloads": -1,
            "filename": "tempcache-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "328671355a16b3dc2d9832beb76edf62",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5224,
            "upload_time": "2024-02-17T22:39:33",
            "upload_time_iso_8601": "2024-02-17T22:39:33.982616Z",
            "url": "https://files.pythonhosted.org/packages/17/c5/30182aa3d2b5d11ea27de410cfab70ae1537c3f7bf6e68b13793aa6878fe/tempcache-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-17 22:39:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "furechan",
    "github_project": "tempcache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "tempcache"
}
        
Elapsed time: 0.19857s