# CassandraCache

> **Class** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/cache/CassandraCache)

Cache that uses Cassandra / Astra DB as a backend.

Example:

    .. code-block:: python

        import cassio

        from langchain_community.cache import CassandraCache
        from langchain_core.globals import set_llm_cache

        cassio.init(auto=True)  # Requires env. variables, see CassIO docs

        set_llm_cache(CassandraCache())

It uses a single Cassandra table.
The lookup keys (which get to form the primary key) are:
    - prompt, a string
    - llm_string, a deterministic str representation of the model parameters.
      (needed to prevent same-prompt-different-model collisions)

## Signature

```python
CassandraCache(
    self,
    session: Optional[CassandraSession] = None,
    keyspace: Optional[str] = None,
    table_name: str = CASSANDRA_CACHE_DEFAULT_TABLE_NAME,
    ttl_seconds: Optional[int] = CASSANDRA_CACHE_DEFAULT_TTL_SECONDS,
    skip_provisioning: bool = False,
    setup_mode: CassandraSetupMode = CassandraSetupMode.SYNC,
)
```

## Description

**Note:**

The session and keyspace parameters, when left out (or passed as None),
fall back to the globally-available cassio settings if any are available.
In other words, if a previously-run 'cassio.init(...)' has been
executed previously anywhere in the code, Cassandra-based objects
need not specify the connection parameters at all.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `session` | `Optional[CassandraSession]` | No | an open Cassandra session. Leave unspecified to use the global cassio init (see below) (default: `None`) |
| `keyspace` | `Optional[str]` | No | the keyspace to use for storing the cache. Leave unspecified to use the global cassio init (see below) (default: `None`) |
| `table_name` | `str` | No | name of the Cassandra table to use as cache (default: `CASSANDRA_CACHE_DEFAULT_TABLE_NAME`) |
| `ttl_seconds` | `Optional[int]` | No | time-to-live for cache entries (default: None, i.e. forever) (default: `CASSANDRA_CACHE_DEFAULT_TTL_SECONDS`) |
| `setup_mode` | `CassandraSetupMode` | No | a value in langchain_community.utilities.cassandra.SetupMode. Choose between SYNC, ASYNC and OFF - the latter if the Cassandra table is guaranteed to exist already, for a faster initialization. (default: `CassandraSetupMode.SYNC`) |

## Extends

- `BaseCache`

## Constructors

```python
__init__(
    self,
    session: Optional[CassandraSession] = None,
    keyspace: Optional[str] = None,
    table_name: str = CASSANDRA_CACHE_DEFAULT_TABLE_NAME,
    ttl_seconds: Optional[int] = CASSANDRA_CACHE_DEFAULT_TTL_SECONDS,
    skip_provisioning: bool = False,
    setup_mode: CassandraSetupMode = CassandraSetupMode.SYNC,
)
```

| Name | Type |
|------|------|
| `session` | `Optional[CassandraSession]` |
| `keyspace` | `Optional[str]` |
| `table_name` | `str` |
| `ttl_seconds` | `Optional[int]` |
| `skip_provisioning` | `bool` |
| `setup_mode` | `CassandraSetupMode` |


## Properties

- `session`
- `keyspace`
- `table_name`
- `ttl_seconds`
- `kv_cache`

## Methods

- [`lookup()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/lookup)
- [`alookup()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/alookup)
- [`update()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/update)
- [`aupdate()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/aupdate)
- [`delete_through_llm()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/delete_through_llm)
- [`delete()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/delete)
- [`clear()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/clear)
- [`aclear()`](https://reference.langchain.com/python/langchain-community/cache/CassandraCache/aclear)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/a6a6079511ac8a5c1293337f88096b8641562e77/libs/community/langchain_community/cache.py#L1044)