# Flask Cache

***

**1. Cache a function's result**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(timeout=500)
def get_data():
    return expensive_function()
```

**2. Cache a view function's response**

```python
from flask import Flask, render_template
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@app.route('/')
@cache.cached(timeout=500)
def index():
    return render_template('index.html')
```

**3. Cache a specific URL**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached_blueprint(cache_timeout=500)
def my_blueprint():
    from flask import Blueprint
    bp = Blueprint('my_blueprint', __name__)

    @bp.route('/')
    def index():
        return 'Hello, world!'

    return bp
```

**4. Cache a specific request with wildcard matching**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(60 * 60 * 24)
def my_route(category, id):
    # Get the data from the database
    return data
```

**5. Cache a specific request with regular expression matching**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(60 * 60 * 24)
def my_route():
    user_id = request.args.get('user_id')
    # Get the data from the database
    return data
```

**6. Cache a specific response code**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(timeout=600)
def error_404():
    return render_template('404.html'), 404
```

**7. Cache a specific request with conditional caching**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached()
def my_route():
    if 'If-Modified-Since' in request.headers:
        since = time.strptime(request.headers['If-Modified-Since'], "%a, %d %b %Y %H:%M:%S %Z")
        if since >= last_modified:
            return '', 304
    last_modified = datetime.datetime.now(tz=tz)
    return data, 200, {'Last-Modified': last_modified.strftime("%a, %d %b %Y %H:%M:%S %Z")}
```

**8. Cache a specific request with ETags**

```python
from flask import Flask, request
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached()
def my_route():
    etag = request.headers.get('If-None-Match')
    if etag == data.etag:
        return '', 304
    data.etag = md5(data).hexdigest()
    return data, 200, {'ETag': data.etag}
```

**9. Cache a request with vary headers**

```python
from flask import Flask, request
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(vary=('Accept-Language',))
def my_route():
    # Get the data from the database
    return data
```

**10. Use a custom cache key**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(key_prefix='my_cache_key')
def my_route():
    # Get the data from the database
    return data
```

**11. Use a custom cache timeout**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(timeout=600)
def my_route():
    # Get the data from the database
    return data
```

**12. Use a custom cache backend**

```python
from flask import Flask
from flask_cache import Cache, SimpleCache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@cache.cached(timeout=600)
def my_route():
    # Get the data from the database
    return data
```

**13. Use a custom cache key function**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

def my_cache_key_function(view, args, kwargs):
    return 'my_cache_key'

@cache.cached(key_prefix=my_cache_key_function)
def my_route():
    # Get the data from the database
    return data
```

**14. Use a custom cache timeout function**

```python
from flask import Flask
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

def my_cache_timeout_function(view, args, kwargs):
    return 600

@cache.cached(timeout=my_cache_timeout_function)
def my_route():
    # Get the data from the database
    return data
```

**15. Use a custom cache backend options**

```python
from flask import Flask
from flask_cache import Cache, SimpleCache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple', 'CACHE_DEFAULT_TIMEOUT': 600})

@cache.cached()
def my_route():
    # Get the data from the database
    return data
```

**16. Cache a request with a custom cache-control header**

```python
from flask import Flask, request, Response
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached()
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**17. Cache a request with a custom cache-control header and vary headers**

```python
from flask import Flask, request, Response
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(vary=('Accept-Language',))
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**18. Cache a request with a custom cache-control header and a custom cache key**

```python
from flask import Flask, request, Response
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(key_prefix='my_cache_key')
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**19. Cache a request with a custom cache-control header and a custom cache timeout**

```python
from flask import Flask, request, Response
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

@cache.cached(timeout=600)
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**20. Cache a request with a custom cache-control header and a custom cache backend**

```python
from flask import Flask, request, Response
from flask_cache import Cache, SimpleCache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@cache.cached()
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**21. Cache a request with a custom cache-control header and a custom cache key function**

```python
from flask import Flask, request, Response
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

def my_cache_key_function(view, args, kwargs):
    return 'my_cache_key'

@cache.cached(key_prefix=my_cache_key_function)
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**22. Cache a request with a custom cache-control header and a custom cache timeout function**

```python
from flask import Flask, request, Response
from flask_cache import Cache

app = Flask(__name__)
cache = Cache(app)

def my_cache_timeout_function(view, args, kwargs):
    return 600

@cache.cached(timeout=my_cache_timeout_function)
def my_route():
    response = Response(data, mimetype='text/plain')
    response.cache_control.max_age = 600
    return response
```

**23. Cache a request with a custom cache-control header and a custom cache backend options**

```python
from flask import Flask, request, Response
from flask_cache import Cache, SimpleCache

app = Flask(__name__)
cache = Cache

```
