# Cache

```python
gradio.Cache(···)
```

### Description

Thread-safe cache with manual get/set control, injected as a function parameter (add as a default parameter value and Gradio will inject it automatically). Supports per-session isolation so cached data doesn't leak between users, content-aware hashing for ML types (numpy, PIL, pandas), and LRU eviction with memory limits.

### Example Usage

```python
import gradio as gr

def generate(prompt, c=gr.Cache(per_session=True)):
    hit = c.get(prompt)
    if hit is not None:
        return hit["result"]
    result = llm(prompt)
    c.set(prompt, result=result)
    return result
```

### Initialization

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `max_size` | `int` | `128` |  |
| `max_memory` | `str \| int \| None` | `None` |  |
| `per_session` | `bool` | `False` |  |
### Methods

#### Description

Event listeners allow you to respond to user interactions with the UI components you've defined in a Gradio Blocks app. When a user interacts with an element, such as changing a slider value or uploading an image, a function is called.

#### Supported Event Listeners

The `Cache` component supports the following event listeners:

- `Cache.get(fn, ...)`: Look up a cache entry by key. Returns a dict of stored data, or None on miss. Keys can be any type supported by gr.cache (strings, numbers, numpy arrays, PIL images, etc.).
- `Cache.set(fn, ...)`: Store arbitrary keyword data under a key.
- `Cache.keys(fn, ...)`: Return all stored raw keys. Useful for iteration or prefix matching.
- `Cache.clear(fn, ...)`: Clear all entries from the cache.

#### Event Parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `key` | `Any` | `` | The cache key to look up. |
- [Caching](https://www.gradio.app/guides/caching)
