# Caching with Redis

Caching is implemented as middleware and supports both in-memory and optional Redis backends.

## Basic Usage

Apply caching using the `cache_response` middleware:

```python
from slush.cache.middleware import cache_response

@app.route("/users", methods=["GET"], middlewares=[cache_response(ttl=120)])
def users(request):
    return {"users": ["a", "b"]}
```

## How It Works

* The `ttl` (time-to-live) defines how long the response is cached (in seconds)
* Cached responses are returned directly without executing the route handler
* Only `GET` requests are cached by default

## Using Redis Backend

You can configure Redis for distributed caching:

```python
app.configure_cache(
    backend="redis",
    url="redis://localhost:6379/0"
)
```

This allows cache sharing across multiple instances of your application.

## Summary

Response caching in Slush allows you to:

* Reduce repeated computation
* Improve response times
* Use in-memory or Redis-backed caching
* Apply caching easily via middleware


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slush.gitbook.io/docs/documentation/basics/caching-with-redis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
