# Cache

Misago uses caching aggressively to save and re-use results of costful operations like users ACLs resolution.

Setting up cache - ideally memory based one like Redis - is great way to speed up your forum and cut down database traffic.

> **Note:** If you are using Misago-Docker, this is already done without any additional configuration.

## Cache versioning

The `misago.cache.versions` module implements cache versioning feature. Cache version is string made of 8 random characters that should be included in cache key, or as value of `version` named argument on `cache.set` and `cache.get`.

Current cache versions are available under the `cache_versions` attribute on `request`. It's set by `misago.cache.middleware.cache_versions_middleware` middleware.

## Invalidating cache

If you wish to invalidate certain versioned cache, you may do it with `misago.cache.versions.invalidate_cache` function:

```python
from misago.cache.versions import invalidate_cache


# Invalidates "bans" cache after creating new ban
invalidate_cache("bans")
```

## Invalidating all versioned caches

Misago provides two ways for invalidating all versioned caches.

### Invalidating all versioned caches from python code

All versioned caches can be invalidated by calling `misago.cache.versions.invalidate_all_caches`:

```python
from misago.cache.versions import invalidate_all_caches

invalidate_all_caches()
```

### Invalidating all versioned caches using management command

Misago has management command that can be executed from console to invalidate all versioned caches:

```
python manage.py invalidateversionedcaches
```

## Versioning new caches

`misago.cache.operations` module provides migration operations that allow you to easily add or remove cache versioning:

```python
from django.db import migrations

from misago.cache.operations import StartCacheVersioning, StopCacheVersioning


class Migration(migrations.Migration):
    dependencies = [
        ('misago_cache', '0001_initial'),
    ]

    operations = [
        # Starts cache versioning for "my_cache"
        StartCacheVersioning("my_cache"),
        # Stop cache versioning "my_cache"
        StopCacheVersioning("my_cache"),
    ]
```

In order for your migration to work, you need to declare dependency on `misago_cache` migration no. `0001_initial`.


---

# 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://misago.gitbook.io/docs/cache.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.
