tempcache


Nametempcache JSON
Version 0.0.11 PyPI version JSON
download
home_pageNone
SummaryPython library to cache data and function results in temporary files
upload_time2025-02-27 20:12:39
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tempcache",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "caching, tempfiles",
    "author": null,
    "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",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python library to cache data and function results in temporary files",
    "version": "0.0.11",
    "project_urls": {
        "homepage": "https://github.com/furechan/tempcache"
    },
    "split_keywords": [
        "caching",
        " tempfiles"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c41651cffe49c6e9ef46ddf6fbb02e98c10c8763cef2c76787da08c605075d5c",
                "md5": "f1e47601189ba1c25caf40bc260430ec",
                "sha256": "bf0757aad05b5c500b03f12fda5facabb1c855e4ed4f96af980662a7373f4e45"
            },
            "downloads": -1,
            "filename": "tempcache-0.0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f1e47601189ba1c25caf40bc260430ec",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5813,
            "upload_time": "2025-02-27T20:12:39",
            "upload_time_iso_8601": "2025-02-27T20:12:39.797378Z",
            "url": "https://files.pythonhosted.org/packages/c4/16/51cffe49c6e9ef46ddf6fbb02e98c10c8763cef2c76787da08c605075d5c/tempcache-0.0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-27 20:12:39",
    "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.73948s