# CassandraSemanticCache

> **Class** in `langchain_community`

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

Cache that uses Cassandra as a vector-store backend for semantic
(i.e. similarity-based) lookup.

Example:

    .. code-block:: python

        import cassio

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

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

        my_embedding = ...

        set_llm_cache(CassandraSemanticCache(
            embedding=my_embedding,
            table_name="my_semantic_cache",
        ))

It uses a single (vector) Cassandra table and stores, in principle,
cached values from several LLMs, so the LLM's llm_string is part
of the rows' primary keys.

One can choose a similarity measure (default: "dot" for dot-product).
Choosing another one ("cos", "l2") almost certainly requires threshold tuning.
(which may be in order nevertheless, even if sticking to "dot").

## Signature

```python
CassandraSemanticCache(
    self,
    session: Optional[CassandraSession] = None,
    keyspace: Optional[str] = None,
    embedding: Optional[Embeddings] = None,
    table_name: str = CASSANDRA_SEMANTIC_CACHE_DEFAULT_TABLE_NAME,
    distance_metric: Optional[str] = None,
    score_threshold: float = CASSANDRA_SEMANTIC_CACHE_DEFAULT_SCORE_THRESHOLD,
    ttl_seconds: Optional[int] = CASSANDRA_SEMANTIC_CACHE_DEFAULT_TTL_SECONDS,
    skip_provisioning: bool = False,
    similarity_measure: str = CASSANDRA_SEMANTIC_CACHE_DEFAULT_DISTANCE_METRIC,
    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`) |
| `embedding` | `Optional[Embeddings]` | No | Embedding provider for semantic encoding and search. (default: `None`) |
| `table_name` | `str` | No | name of the Cassandra (vector) table to use as cache. There is a default for "simple" usage, but remember to explicitly specify different tables if several embedding models coexist in your app (they cannot share one cache table). (default: `CASSANDRA_SEMANTIC_CACHE_DEFAULT_TABLE_NAME`) |
| `distance_metric` | `Optional[str]` | No | an alias for the 'similarity_measure' parameter (see below). As the "distance" terminology is misleading, please prefer 'similarity_measure' for clarity. (default: `None`) |
| `score_threshold` | `float` | No | numeric value to use as cutoff for the similarity searches (default: `CASSANDRA_SEMANTIC_CACHE_DEFAULT_SCORE_THRESHOLD`) |
| `ttl_seconds` | `Optional[int]` | No | time-to-live for cache entries (default: None, i.e. forever) (default: `CASSANDRA_SEMANTIC_CACHE_DEFAULT_TTL_SECONDS`) |
| `similarity_measure` | `str` | No | which measure to adopt for similarity searches. Note: this parameter is aliased by 'distance_metric' - however, it is suggested to use the "similarity" terminology since this value is in fact a similarity (i.e. higher means closer). Note that at most one of the two parameters 'distance_metric' and 'similarity_measure' can be provided. (default: `CASSANDRA_SEMANTIC_CACHE_DEFAULT_DISTANCE_METRIC`) |
| `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,
    embedding: Optional[Embeddings] = None,
    table_name: str = CASSANDRA_SEMANTIC_CACHE_DEFAULT_TABLE_NAME,
    distance_metric: Optional[str] = None,
    score_threshold: float = CASSANDRA_SEMANTIC_CACHE_DEFAULT_SCORE_THRESHOLD,
    ttl_seconds: Optional[int] = CASSANDRA_SEMANTIC_CACHE_DEFAULT_TTL_SECONDS,
    skip_provisioning: bool = False,
    similarity_measure: str = CASSANDRA_SEMANTIC_CACHE_DEFAULT_DISTANCE_METRIC,
    setup_mode: CassandraSetupMode = CassandraSetupMode.SYNC,
)
```

| Name | Type |
|------|------|
| `session` | `Optional[CassandraSession]` |
| `keyspace` | `Optional[str]` |
| `embedding` | `Optional[Embeddings]` |
| `table_name` | `str` |
| `distance_metric` | `Optional[str]` |
| `score_threshold` | `float` |
| `ttl_seconds` | `Optional[int]` |
| `skip_provisioning` | `bool` |
| `similarity_measure` | `str` |
| `setup_mode` | `CassandraSetupMode` |


## Properties

- `session`
- `keyspace`
- `embedding`
- `table_name`
- `similarity_measure`
- `score_threshold`
- `ttl_seconds`
- `table`

## Methods

- [`update()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/update)
- [`aupdate()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/aupdate)
- [`lookup()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/lookup)
- [`alookup()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/alookup)
- [`lookup_with_id()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/lookup_with_id)
- [`alookup_with_id()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/alookup_with_id)
- [`lookup_with_id_through_llm()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/lookup_with_id_through_llm)
- [`alookup_with_id_through_llm()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/alookup_with_id_through_llm)
- [`delete_by_document_id()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/delete_by_document_id)
- [`adelete_by_document_id()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/adelete_by_document_id)
- [`clear()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/clear)
- [`aclear()`](https://reference.langchain.com/python/langchain-community/cache/CassandraSemanticCache/aclear)

---

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