Automatic caching¶
Turn caching on and the reads of a table are answered from Redis, for every server of the cluster. API Maker knows when the table changes, because the writes go through it too, so it drops the cached answers itself : no stale reads, no code.
| Turn on | enableCaching: true in the collection settings or the database settings ; in the settings of a custom API or a system API. |
| Key | The URL, the query, the body and the tokens of the request : two callers with different rights never share an answer, tenants never share one. |
| Lifetime | Until a write of the table, or redisValueExpireInSeconds (7200 by default) of the configuration. Answers longer than maxCharsResToCache are not cached. |
| Reset | A write through any API of the table ; the reset cache system APIs ; the header x-am-cache-control: reset_cache on one request. |
| See it | The Redis dashboard lists the keys ; the API testing page says whether an answer came from the cache. |
Tables¶
- Set it on the table, or on the database for all its tables. The setting is not per API : every read of the table is cached, every write of the table resets.
- Data changed outside API Maker is not seen : reset the table with reset database cache from the job which changed it, or send
x-am-cache-control: reset_cachewith the next read. - Cached answers run no hook : a pre hook which scopes rows to the caller still holds, because the caller is part of the key.
Custom APIs¶
Basic Info
enableCaching: true,
resetCacheOnModificationOf: [ 'DB:mongodb:shop:products', 'DB:mongodb:shop:categories' ],
- The answer of the API is cached per body, query and caller.
resetCacheOnModificationOfnames the tables (DB:instance:database:table), custom APIs (CA:name) and bundle versions (TP:bundle:version) whose writes drop it. - Without the list, reset it yourself with reset custom API cache.
System APIs¶
enableCaching: truein the settings of the system API ; reset with reset system API cache.
Per request¶
| Header | Effect |
|---|---|
x-am-cache-control: no_action |
Answer without reading or writing the cache. |
x-am-cache-control: reset_cache |
Drop the cached answers of the API, then answer from the database and cache that. |