tempcache


Nametempcache JSON
Version 0.0.12 PyPI version JSON
download
home_pageNone
SummaryPython library to cache data and function results in temporary files
upload_time2025-10-08 17:34:24
maintainerNone
docs_urlNone
authorfurechan
requires_python>=3.10
licenseNone
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, including a mechanism for automatic expiration after a certain time.

Dy default, the package uses the `pickle` module to serialize data and hash key values.


> **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
cache = TempCache("mycache", max_age=CACHE_MAX_AGE)

@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
cache = TempCache("mycache", max_age=CACHE_MAX_AGE)

def long_running(...):
    ...

result = 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 can use a different pickle module
like `cloupickle`.


```python
import cloudpickle

from tempcache import TempCache

CACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days
cache = TempCache("mycache",
                    pickler=cloudpickle,
                    max_age=CACHE_MAX_AGE)

key = ...
# key object can be complex as long as it is pickle-able

item = cache.item_for_key(key)
# cache item for the given key whether it exists or not

# load item if it exists
if item.exists():
    value = item.load()

# save item
item.save(value)
```


## 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
- [disckcache](https://pypi.org/project/diskcache/)
Disk and file backed cache library compatible with Django
- [cloudpickle](https://github.com/cloudpipe/cloudpickle)
Extended pickling support for Python objects
- [cached_path](https://github.com/allenai/cached_path)
A file utility for accessing both local and remote files through a unified interface

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tempcache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "caching, tempfiles",
    "author": "furechan",
    "author_email": "furechan <furechan@xsmail.com>",
    "download_url": null,
    "platform": null,
    "description": "# Python library to cache data and function results in temporary files\n\nThis library offers a simple way to cache data and function results using temporary files, including a mechanism for automatic expiration after a certain time.\n\nDy default, the package uses the `pickle` module to serialize data and hash key values.\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\ncache = TempCache(\"mycache\", max_age=CACHE_MAX_AGE)\n\n@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\ncache = TempCache(\"mycache\", max_age=CACHE_MAX_AGE)\n\ndef long_running(...):\n    ...\n\nresult = 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 can use a different pickle module\nlike `cloupickle`.\n\n\n```python\nimport cloudpickle\n\nfrom tempcache import TempCache\n\nCACHE_MAX_AGE = 24 * 60 * 60 * 2    # two days\ncache = TempCache(\"mycache\",\n                    pickler=cloudpickle,\n                    max_age=CACHE_MAX_AGE)\n\nkey = ...\n# key object can be complex as long as it is pickle-able\n\nitem = cache.item_for_key(key)\n# cache item for the given key whether it exists or not\n\n# load item if it exists\nif item.exists():\n    value = item.load()\n\n# save item\nitem.save(value)\n```\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- [disckcache](https://pypi.org/project/diskcache/)\nDisk and file backed cache library compatible with Django\n- [cloudpickle](https://github.com/cloudpipe/cloudpickle)\nExtended pickling support for Python objects\n- [cached_path](https://github.com/allenai/cached_path)\nA file utility for accessing both local and remote files through a unified interface\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python library to cache data and function results in temporary files",
    "version": "0.0.12",
    "project_urls": {
        "homepage": "https://github.com/furechan/tempcache"
    },
    "split_keywords": [
        "caching",
        " tempfiles"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4cc4474e3dadf3776e8350da503c22a73c9c8362b59671cf10f64979505a00f6",
                "md5": "774bd371dad033c039ffe8fa8baf0eb4",
                "sha256": "d822f718345362054685a69dd9598a228544bea94362aceb142b51ad648d5291"
            },
            "downloads": -1,
            "filename": "tempcache-0.0.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "774bd371dad033c039ffe8fa8baf0eb4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 5606,
            "upload_time": "2025-10-08T17:34:24",
            "upload_time_iso_8601": "2025-10-08T17:34:24.191341Z",
            "url": "https://files.pythonhosted.org/packages/4c/c4/474e3dadf3776e8350da503c22a73c9c8362b59671cf10f64979505a00f6/tempcache-0.0.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-08 17:34:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "furechan",
    "github_project": "tempcache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tempcache"
}
        
Elapsed time: 2.21003s