> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ag2.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# autogen.Cache

<h2 id="autogen.Cache" class="doc doc-heading">
  <code class="doc-symbol doc-symbol-heading doc-symbol-class" />

  <span class="doc doc-object-name doc-class-name">Cache</span>
</h2>

```python theme={null}
Cache(config: dict[str, Any])
```

A wrapper class for managing cache configuration and instances.<br />This class provides a unified interface for creating and interacting with
different types of cache (e.g., Redis, Disk). It abstracts the underlying
cache implementation details, providing methods for cache operations.<br />Attributes: <br />    config (Dict\[str, Any]): A dictionary containing cache configuration.<br />    cache: The cache instance created based on the provided configuration.<br />Initialize the Cache with the given configuration.<br />Validates the configuration keys and creates the cache instance.<br />

<b>Parameters:</b>

| Name     | Description               |
| -------- | ------------------------- |
| `config` | **Type:** dict\[str, Any] |

### Class Attributes

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### ALLOWED\_CONFIG\_KEYS

<br />

<br />

### Static Methods

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### cosmos\_db

```python theme={null}
cosmos_db(
    connection_string: Optional[str] = None,
    container_id: Optional[str] = None,
    cache_seed: Union[str, int] = 42,
    client: Optional[Any] = None
) -> Cache
```

Create a Cosmos DB cache instance with 'autogen\_cache' as database ID.<br />

<b>Parameters:</b>

| Name                | Description                                                                                                                  |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `connection_string` | Connection string to the Cosmos DB account.<br /><br />**Type:** Optional\[str]<br /><br />**Default:** None                 |
| `container_id`      | The container ID for the Cosmos DB account.<br /><br />**Type:** Optional\[str]<br /><br />**Default:** None                 |
| `cache_seed`        | A seed for the cache.<br /><br />**Type:** Union\[str, int]<br /><br />**Default:** 42                                       |
| `client`            | Optional\[CosmosClient]: Pass an existing Cosmos DB client.<br /><br />**Type:** Optional\[Any]<br /><br />**Default:** None |

<b>Returns:</b>

| Type                                       | Description                                       |
| ------------------------------------------ | ------------------------------------------------- |
| [Cache](/docs/api-reference/autogen/Cache) | Cache: A Cache instance configured for Cosmos DB. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### disk

```python theme={null}
disk(cache_seed: Union[str, int] = 42, cache_path_root: str = '.cache') -> Cache
```

Create a Disk cache instance.<br />

<b>Parameters:</b>

| Name              | Description                                                                                                                  |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `cache_seed`      | A seed for the cache.<br /><br />Defaults to 42.<br /><br />**Type:** Union\[str, int]<br /><br />**Default:** 42            |
| `cache_path_root` | The root path for the disk cache.<br /><br />Defaults to ".cache".<br /><br />**Type:** str<br /><br />**Default:** '.cache' |

<b>Returns:</b>

| Type                                       | Description                                          |
| ------------------------------------------ | ---------------------------------------------------- |
| [Cache](/docs/api-reference/autogen/Cache) | Cache: A Cache instance configured for Disk caching. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get\_current\_cache

```python theme={null}
get_current_cache(cache: "'Optional[Cache]'" = None) -> Cache | None
```

Get the current cache instance.<br />Returns: <br />    Cache: The current cache instance.

<b>Parameters:</b>

| Name    | Description                                                                                      |
| ------- | ------------------------------------------------------------------------------------------------ |
| `cache` | **Type:** "'Optional\[[Cache](/docs/api-reference/autogen/Cache)]'"<br /><br />**Default:** None |

<b>Returns:</b>

| Type                                               | Description                        |
| -------------------------------------------------- | ---------------------------------- |
| [Cache](/docs/api-reference/autogen/Cache) \| None | Cache: The current cache instance. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### redis

```python theme={null}
redis(cache_seed: Union[str, int] = 42, redis_url: str = 'redis://localhost:6379/0') -> Cache
```

Create a Redis cache instance.<br />

<b>Parameters:</b>

| Name         | Description                                                                                                                               |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `cache_seed` | A seed for the cache.<br /><br />Defaults to 42.<br /><br />**Type:** Union\[str, int]<br /><br />**Default:** 42                         |
| `redis_url`  | The URL for the Redis server.<br /><br />Defaults to "redis\://localhost:6379/0".<br /><br />**Type:** str<br /><br />**Default:** 'redis |

<b>Returns:</b>

| Type                                       | Description                                   |
| ------------------------------------------ | --------------------------------------------- |
| [Cache](/docs/api-reference/autogen/Cache) | Cache: A Cache instance configured for Redis. |

<br />

### Instance Methods

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### close

```python theme={null}
close(self) -> None
```

Close the cache.<br />Perform any necessary cleanup, such as closing connections or releasing resources.

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get

```python theme={null}
get(
    self,
    key: str,
    default: Optional[Any] = None
) -> Any | None
```

Retrieve an item from the cache.<br />

<b>Parameters:</b>

| Name      | Description                                                                                                                                        |
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key`     | The key identifying the item in the cache.<br /><br />**Type:** str                                                                                |
| `default` | The default value to return if the key is not found.<br /><br />Defaults to None.<br /><br />**Type:** Optional\[Any]<br /><br />**Default:** None |

<b>Returns:</b>

| Type        | Description                                                         |
| ----------- | ------------------------------------------------------------------- |
| Any \| None | The value associated with the key if found, else the default value. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### set

```python theme={null}
set(
    self,
    key: str,
    value: Any
) -> None
```

Set an item in the cache.<br />

<b>Parameters:</b>

| Name    | Description                                                            |
| ------- | ---------------------------------------------------------------------- |
| `key`   | The key under which the item is to be stored.<br /><br />**Type:** str |
| `value` | The value to be stored in the cache.<br /><br />**Type:** Any          |

<br />
