# RedisCache

> **Class** in `langchain_redis`

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

Redis cache implementation for LangChain.

This class provides a Redis-based caching mechanism for LangChain, allowing
storage and retrieval of language model responses.

## Signature

```python
RedisCache(
    self,
    redis_url: str = 'redis://localhost:6379',
    ttl: Optional[int] = None,
    prefix: Optional[str] = 'redis',
    redis_client: Optional[Redis] = None,
)
```

## Description

**Example:**

```python
from langchain_redis import RedisCache
from langchain_core.globals import set_llm_cache

# Create a Redis cache instance
redis_cache = RedisCache(redis_url="redis://localhost:6379", ttl=3600)

# Set it as the global LLM cache
set_llm_cache(redis_cache)

# Now, when you use an LLM, it will automatically use this cache
```

**Note:**

- This cache implementation uses Redis JSON capabilities to store
    structured data.
- The cache key is created using MD5 hashes of the prompt and LLM string.
- If TTL is set, cache entries will automatically expire
    after the specified duration.
- The prefix can be used to namespace cache entries.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `redis_url` | `str` | No | The URL of the Redis instance to connect to. (default: `'redis://localhost:6379'`) |
| `ttl` | `Optional[int]` | No | Time-to-live for cache entries in seconds. (default: `None`) |
| `prefix` | `Optional[str]` | No | Prefix for all keys stored in Redis. (default: `'redis'`) |
| `redis_client` | `Optional[Redis]` | No | An existing Redis client instance.  If provided, `redis_url` is ignored. (default: `None`) |

## Extends

- `BaseCache`

## Constructors

```python
__init__(
    self,
    redis_url: str = 'redis://localhost:6379',
    ttl: Optional[int] = None,
    prefix: Optional[str] = 'redis',
    redis_client: Optional[Redis] = None,
)
```

| Name | Type |
|------|------|
| `redis_url` | `str` |
| `ttl` | `Optional[int]` |
| `prefix` | `Optional[str]` |
| `redis_client` | `Optional[Redis]` |


## Properties

- `redis`
- `ttl`
- `prefix`

## Methods

- [`lookup()`](https://reference.langchain.com/python/langchain-redis/cache/RedisCache/lookup)
- [`update()`](https://reference.langchain.com/python/langchain-redis/cache/RedisCache/update)
- [`clear()`](https://reference.langchain.com/python/langchain-redis/cache/RedisCache/clear)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-redis/blob/17794ab183d4abde98747360f251478088836347/libs/redis/langchain_redis/cache.py#L108)