Metadata-Version: 2.4
Name: pygridgain-dogpile
Version: 1.7.0
Summary: A dogpile.cache backend backed by the GridGain Python thin client
Author-email: GridGain Systems <info@gridgain.com>
License-Expression: LicenseRef-Proprietary
Project-URL: Homepage, https://www.gridgain.com
Project-URL: Documentation, https://pygridgain.readthedocs.io
Project-URL: Support, https://www.gridgain.com/services/support
Keywords: gridgain,ignite,dogpile,dogpile.cache,cache,caching
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
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: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Intended Audience :: Developers
Classifier: Topic :: Database :: Front-Ends
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Operating System :: OS Independent
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: licenses/LICENSE
License-File: NOTICE
Requires-Dist: pygridgain<2,>=1.7.0
Requires-Dist: dogpile.cache>=1.1.0
Dynamic: license-file

# pygridgain-dogpile

A [dogpile.cache](https://dogpilecache.sqlalchemy.org/) backend that stores entries in GridGain,
built on the GridGain 8.x Python thin client.

An application that already uses dogpile can switch its cache to a GridGain cluster by changing
only the configuration.

## Installation

```bash
pip install pygridgain-dogpile
```

This also installs `pygridgain` (the 8.x client) and `dogpile.cache`.

The backend is registered in the `dogpile.cache` entry point group as `gridgain` and as
`dogpile.cache.gridgain`, so you do not need to import it.

## Minimal example

```python
from dogpile.cache import make_region

region = make_region().configure(
    'gridgain',
    expiration_time=3600,
    arguments={'url': '127.0.0.1:10800'}
)

region.set('greeting', 'Hello, world!')
print(region.get('greeting'))


@region.cache_on_arguments()
def expensive(number):
    return number * number
```

A full example is in `examples/dogpile_cache.py`, which is part of the source distribution
(`pip download --no-binary :all: pygridgain-dogpile`).

## Configuration arguments

| Argument | Meaning | Default |
| --- | --- | --- |
| `url` | Address of a cluster node as `'host:port'`, several addresses separated by commas, or an iterable of addresses. Write an IPv6 address in brackets: `'[::1]:10800'`. | `localhost:10800` |
| `cache_name` | Cache for the entries. It is created on first use if it does not exist. | `dogpile_cache` |
| `backups` | Number of backups. Used only when the backend creates the cache. | `0` |
| `gridgain_expiration_time` | Seconds after which an entry expires on the server. | `0` (never) |
| `pool_size` | Number of clients the backend may open. Each client connects to every node in `url`. | `8` |
| `distributed_lock` | Whether `get_mutex` returns a lock held in the cluster, not in the process. | `False` |
| `lock_timeout` | Seconds after which a distributed lock is released, even if its holder did not release it. | `0` (never) |
| `client_args` | Keyword arguments for `pygridgain.Client`, such as SSL and authentication settings. `timeout` is in seconds per socket operation. It is 10 by default, so a node that stops answering cannot hang the application. Pass `None` to block forever. | `{'timeout': 10}` |
| `client` | An already connected client to use instead of opening connections. Cannot be combined with `pool_size`. | `None` |

You can pass any argument as a string, as `oslo.cache` does. An unknown argument raises
`ValueError`, so a typo in a configuration file is not ignored.

## What is stored

The backend is a `BytesBackend`: dogpile serializes values, and the cluster stores them as byte
arrays. You do not need to register any binary types, and other clients can read the entries as
plain bytes. Keys are stored as strings. A region with its own `key_mangler` also works.

`get_multi`, `set_multi` and `delete_multi` use the cluster's bulk operations, so a request for many
keys takes one round trip.

## Expiration

A region tracks the age of each value itself. `gridgain_expiration_time` also sets a TTL on the
server, so old entries that the region no longer uses do not take memory.

Set it higher than the region's `expiration_time`, for example to twice that value. When a value
is stale, dogpile lets one caller create a new value and gives the stale value to all other callers
in the meantime. This works only while the server still has the stale value. If the two times are
equal, all callers wait for the new value. If the TTL is shorter, the region misses earlier than
expected.

Reading an entry does not extend its life. dogpile also counts the age of an entry from the time it
was created.

The TTL needs cluster support for the ExpiryPolicy API. Without it, the backend raises
`NotSupportedByClusterError`. With the default 0, entries never expire on the server.

## Connections and threads

`pygridgain.Client` is not thread-safe, but all threads of a process share one dogpile region. So
the backend keeps a pool of connections and gives one to each operation. Callers wait when all
`pool_size` connections are in use.

Each client in the pool connects to every node in `url` and sends each single-key request to the
node that owns the key. So a process opens up to `pool_size` sockets per node, and each worker
process opens its own sockets. Choose `pool_size` with this in mind. To use one socket per client,
pass `client_args={'partition_aware': False}`. Single-key requests then take an extra hop inside
the cluster.

If you pass your own `client`, the pool has only this one connection: operations wait for each
other, and you must close it yourself.

## Locking and the dogpile effect

When a popular entry expires, many threads may try to create it again at the same time. This is
the dogpile effect that gives the library its name. dogpile makes these threads wait on a mutex
from the backend.

By default the backend returns `None`, and dogpile uses a `threading.Lock`. This works within one
process, but with several worker processes, each process still creates the value.

With `distributed_lock` set to True, the lock is held in the cluster. Like dogpile's own memcached
lock, it uses `put_if_absent`. Each lock has a unique token, so a caller cannot release a lock it
does not hold.

With `lock_timeout`, the lock expires by itself, so a holder that dies does not keep the key locked
forever. But if creating the value takes longer than the timeout, another thread may start to
create the same value. dogpile has the same limitation for memcached. With `lock_timeout` at 0,
the lock never expires, so a holder that never releases it keeps the key locked.

```python
region = make_region().configure(
    'gridgain',
    expiration_time=3600,
    arguments={
        'url': '127.0.0.1:10800',
        'distributed_lock': True,
        'lock_timeout': 60,
    }
)
```

## Use with oslo.cache

`oslo.cache` wraps dogpile.cache. OpenStack services, such as Keystone, use it to configure
caching. The backend works with it (tested with oslo.cache 4.1.1), but you cannot select it with
configuration only yet.

`oslo.cache` checks `[cache] backend` against a fixed list of names in `oslo_cache/_opts.py`. Any
other name, including `dogpile.cache.gridgain`, is rejected before dogpile sees it:

```
ValueError: Valid values are [oslo_cache.memcache_pool, oslo_cache.dict,
..., dogpile.cache.redis, dogpile.cache.memory, dogpile.cache.null],
but found 'gridgain'
```

So `oslo.cache` needs a one-line patch that adds the name to this list, as was done for
`dogpile.cache.redis_sentinel` and `oslo_cache.etcd3gw`. With this patch, everything else works
unchanged: region configuration, `backend_argument` strings, single-key and multi-key operations,
the distributed mutex, and `get_memoization_decorator`, which Keystone uses for caching.

When the name is accepted upstream, the configuration is:

```ini
[cache]
enabled = true
backend = dogpile.cache.gridgain
backend_argument = url:127.0.0.1:10800
backend_argument = cache_name:keystone_cache
backend_argument = gridgain_expiration_time:3600
```

`oslo.cache` sets the region's `expiration_time` from `[cache] expiration_time`, so you only need
to set the server TTL here. Keep it above `[cache] expiration_time`, which is 600 by default.

## Running the tests

The tests need a running cluster; they do not start one. `GRIDGAIN_ADDRESS` sets its address, the
default is `127.0.0.1:10800`:

```bash
docker-compose up -d
pip install -r requirements/tests.txt
pip install -e .
pytest
```

If no cluster is available, the tests that need one are skipped. Tests of argument parsing and the
entry point do not need a cluster and always run.

The cluster restart test also needs a command that restarts the cluster. Without it, the test is
skipped. For the node from `docker-compose.yml`:

```bash
GRIDGAIN_RESTART_COMMAND='docker-compose restart gridgain' pytest
```

## License

Copyright (c) 2026 MariaDB plc. Licensed under the MariaDB Software License Terms; see
[licenses/LICENSE](licenses/LICENSE).
