Metadata-Version: 2.4
Name: cachepot
Version: 0.5.0
Summary: Yet another Python cache library
Author-email: Yui KITSU <kitsuyui+github@kitsuyui.com>
License: Copyright 2019 Kitsu Yui
        
        Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: Homepage, https://github.com/kitsuyui/cachepot
Classifier: Development Status :: 3 - Alpha
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: redis
Requires-Dist: redis<9,>=8; extra == "redis"
Dynamic: license-file

# cachepot

[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cachepot.svg)](https://pypi.python.org/pypi/cachepot/)
[![PyPI](https://img.shields.io/pypi/v/cachepot.svg)](https://pypi.python.org/pypi/cachepot/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/cachepot.svg)](https://pypi.python.org/pypi/cachepot/)
[![Lint and Test Python](https://github.com/kitsuyui/cachepot/actions/workflows/python-test.yml/badge.svg)](https://github.com/kitsuyui/cachepot/actions/workflows/python-test.yml)
![Coverage](https://raw.githubusercontent.com/kitsuyui/octocov-central/main/badges/kitsuyui/cachepot/coverage.svg)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)

Yet another Python cache library. This has Python 3 typing hints.

## Installation

```
$ pip install cachepot
```

## Usage

```python
>>> from cachepot import CacheStore, FileSystemCacheBackend
>>> from cachepot.serializer.pickle import PickleSerializer
>>> store = CacheStore(
...     namespace='testing',
...     key_serializer=PickleSerializer(),
...     value_serializer=PickleSerializer(),
...     backend=FileSystemCacheBackend('/tmp'),
...     default_expire_seconds=3600,
... )
>>> store.put({'some': 'key'}, {'some': 'value'})
>>> store.get({'some': 'key'})
{'some': 'value'}
>>> store.put({'some': 'short expiring key'}, {'some': 'value'}, expire_seconds=10)
>>> store.delete_expired()
0
```

`delete_expired()` returns the number of entries removed when the backend can
observe that count.  Redis handles TTL expiry server-side, so its backend
returns `None` when the deleted-entry count is unknown.
SQLite also opportunistically removes expired rows during later writes, while
`delete_expired()` remains available when you want to force a cleanup cycle.

> **Security note**: `PickleSerializer` uses Python's `pickle` module, which can
> execute arbitrary code during deserialization. The serializer is intentionally
> not exported from `cachepot`'s top-level namespace. Import it explicitly from
> `cachepot.serializer.pickle` only when the cache backend storage is fully
> under your control and trusted. For untrusted environments, prefer
> `JSONSerializer` instead.

### Proxy method

```python
result = store.proxy(some_func)(some_args, cache_key=some_arg)
```

is the equivalent of

```python
result = store.get(some_arg)
if result is None:
    result = some_func(some_args)
    store.put(some_arg, result)
```

In short, this works as proxy. This helps to make codes straight forward.
`cache_key` and `expire_seconds` are keyword-only arguments of the proxy
itself, not of the proxied function. `store.proxy(...)` raises `TypeError`
at proxy creation time if the proxied function declares a parameter named
`cache_key` or `expire_seconds`, because those names are reserved for the
proxy.

When a proxied cache write fails, `cachepot` keeps returning the computed
result, emits a `CachepotWarning`, logs the failure on the `cachepot.store`
logger, and increments `store.cache_write_failures`. This lets applications
forward failures to their existing logging or metrics pipeline without giving
up graceful degradation.

## Core idea

Serializers convert python objects into bytes.
Backends save/load bytes.
So serializers and backends are independent.
CacheStore is the facade of them.

Backends now enforce a finite per-entry size contract by default:
`max_entry_bytes=8 * 1024 * 1024` (8 MiB). Entries larger than that are
rejected on `save()`, and `load()` checks the backend-side size before
materializing the payload into process memory. You can override the limit
per backend instance when your application has a different budget.

- Python3 typing supports
- namespaces
- Proxy method
- Expired entry cleanup

## Features

### Serializers

- str ... [cachepot.serializer.str.StringSerializer](https://github.com/kitsuyui/cachepot/blob/master/cachepot/serializer/str.py)
- [pickle](https://docs.python.org/3/library/pickle.html) ... [cachepot.serializer.pickle.PickleSerializer](https://github.com/kitsuyui/cachepot/blob/master/cachepot/serializer/pickle.py)
- [JSON](https://tools.ietf.org/html/rfc8259) ... [cachepot.serializer.json.JSONSerializer](https://github.com/kitsuyui/cachepot/blob/master/cachepot/serializer/json.py)

And more serializers you can define.

### Backends

- Save to files ... [cachepot.backend.filesystem.FileSystemCacheBackend](https://github.com/kitsuyui/cachepot/blob/master/cachepot/backend/filesystem.py)
- Save to SQLite3 DB records ... [cachepot.backend.sqlite.SQLiteCacheBackend](https://github.com/kitsuyui/cachepot/blob/master/cachepot/backend/sqlite.py)
- Save to Redis DB ... [cachepot.backend.redis.RedisCacheBackend](https://github.com/kitsuyui/cachepot/blob/master/cachepot/backend/redis.py)

Of course you can define own backend.

## Development

This repository uses [lefthook](https://lefthook.dev/) to run the same checks as CI
locally, so problems surface before they reach CI.

```sh
# Install dependencies
uv sync

# Install the Git hooks (once; requires lefthook on your PATH)
lefthook install
```

Once installed, the hooks run automatically:

- **pre-commit**: `uv run poe check`
- **pre-push**: `uv run poe check` and `uv run poe test`

You can also run the checks manually:

```sh
uv run poe check
uv run poe test
```

CI still runs the full matrix (see `.github/workflows/`); the hooks only bring that
feedback earlier on your machine.

# LICENSE

The 3-Clause BSD License. See also LICENSE file.
