> For the complete documentation index, see [llms.txt](https://jjrdev.gitbook.io/luna/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jjrdev.gitbook.io/luna/o-basico/cache.md).

# Cache

Algumas tarefas ou processamento de dados podem consumir muita CPU ou levar vários segundos para serem concluídas. Quando esse é o caso, é comum armazenar em cache os dados recuperados por um tempo para que possam ser recuperados com maior velocidade em solicitações subsequentes dos mesmos dados.

Com isso, o Luna fornece uma opção prática para armazenamento de cache do resultado de rotas, a definição do cache deve ser realizada na criação da rota:

```php
$router->get('/', [
    'cache' => 10000,
    function($request, $response) {
        return Pages\Example::getPage($request, $response);
    }
]);
```

{% hint style="info" %}
O tempo de cache é definido em milisegundos.
{% endhint %}

No exemplo, o cache será armazenado por 10 segundos, nesse período toda requisição trará o mesmo resultado e após um novo intervalo se inicia.

As configurações de cache podem ser definidas no arquivo `.env`:

<table><thead><tr><th width="372">Configuração</th><th>Descrição</th></tr></thead><tbody><tr><td>CACHE_TIME</td><td>Valor padrão de cache</td></tr><tr><td>CACHE_DIR</td><td>Diretório de armazenamento do cache</td></tr><tr><td>ALLOW_NO_CACHE_HEADER</td><td>Permitir o header <code>Cache-Control: no-cache</code></td></tr></tbody></table>

O valor de `CACHE_TIME` é definido como tempo de cache (também em milisegundos) quando o `cache` da rota for definido como `true`:

```php
$router->get('/', [
    'cache' => true,
    function($request, $response) {
        return Pages\Example::getPage($request, $response);
    }
]);
```

{% hint style="info" %}
O uso de `cache: true` facilita para utilizar um valor padrão de cache e modifica-lo com praticidade se necessário.
{% endhint %}

### Diretório de cache

Os caches são armazenados em arquivos no diretório `./cache` por padrão, caso deseje alterar o endereço para um diretório do sistema operacional utilize um diretório absoluto.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://jjrdev.gitbook.io/luna/o-basico/cache.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
