# Cache

Simple caching system to cache the API return in the http **GET method**, ⚠ *by default the caching system is disabled*.

We use key and value database embedded in *prestd* ([BuntDB](https://github.com/tidwall/buntdb)).

We have a timeout system (TTL) for the cached data, by default it is kept for `10 minutes` - with the possibility to change it in the settings.

### Data Architecture

For each URI (with its parameters) a *BuntDB* database cache file is created.

> It was implemented this way with performance in mind - there is no point in putting in a caching system that is slower than the SQL query in PostgreSQL.

* **key:** URI with all string query parameters
* **value:** json return (http body)

#### Because BuntDB

Is a low-level, in-memory, key/value store in pure Go. It persists to disk, is ACID compliant, and uses locking for multiple readers and a single writer. It supports custom indexes and geospatial data. It's ideal for projects that need a dependable database and favor speed over data size.

We didn't want to depend on an external database (and we can't create tables in the existing database), with this premise we decided to use an embedded database (write in Go language) and BuntDB proved to be the best option at the moment, [here you can see the discussion existing since **2017**](https://github.com/prest/prest/issues/112).

### Configuration for specific endpoint *("advanced")*

Activating the caching system all endpoints in your api will have the caching system active, following the defined default configuration.

You can customize the configuration made for one (or more) specific endpoints, for example:

* `/prest/public/my-table` I want more caching time
* `/prest/public/my-table-uncached` I don't want caching, *I need the data that is in the database in "real time*

For this configuration, you must use the TOML's `[[cache.endpoints]]` node (because it is an array it is not possible to configure it via an environment variable).

### Environment vars

| var                       | default          | description                                    |
| ------------------------- | ---------------- | ---------------------------------------------- |
| PREST\_CACHE\_ENABLED     | false            | embedded cache system                          |
| PREST\_CACHE\_TIME        | 10               | TTL in minute (time to live)                   |
| PREST\_CACHE\_STORAGEPATH | ./               | path where the cache file will be created      |
| PREST\_CACHE\_SUFIXFILE   | .cache.prestd.db | suffix of the name of the file that is created |

### TOML

Optionally the pREST can be configured by TOML file.

You can follow this sample and create your own `prest.toml` file and put this on the same folder that you run `prestd` command.

```toml
[cache]
enabled = true
time = 10
storagepath = "./"
sufixfile = ".cache.prestd.db"

    [[cache.endpoints]]
    endpoint = "/prest/public/test"
    time = 5

    # this endpoint will have no caching system
    [[cache.endpoints]]
    enabled = false
    endpoint = "/prest/public/test-disable"
```


---

# 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://docs.prestd.com/get-started/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.
