The cache command
cache shows the caches declared in CacheConfigurer and
clears the ones that live outside the server’s memory: file and
redis. Use it to check what is in a cache and to reset it on a deploy
or after a manual change to the data.
Why
A cache you cannot see is hard to debug: it is unclear whether a value is stored, when it expires and why a method returns the old one. And after a deploy or a manual edit in the database a cache sometimes has to be reset without restarting the server.
call cache answers both — for the caches a separate process can reach. The caches
themselves are covered on the
Method caching page.
Actions
| Action | What it does |
|---|---|
show |
Every declared cache: store, settings, number of entries |
show <name> |
Entries of a file or redis cache: key, size, time left |
show <name> --all |
Every entry instead of the first 100 |
clear <name> |
Empty one file or redis cache |
clear --all |
Empty every file and redis cache |
php call cache show
php call cache show api-token
php call cache clear products
php call cache clear --allWhat the console can see
The command is a separate process. So it sees only what lives outside the server’s memory:
| Store | show |
clear |
|---|---|---|
table |
settings and reserved memory, no entries | no — a restart empties it |
memory |
settings, no entries | no — a restart empties it |
file |
settings and entries | yes |
redis |
settings and entries | yes |
The entries of table and memory live in the running server’s memory: the console
process has its own, empty copy. The command says so plainly instead of showing an empty list.
show
| [ Caches (4) ]
| products ............................. [table · 10000 rows · 1 KB · ≈26.8 MB · ttl 300 s]
| stock ................................ [memory · 100 items per worker · ttl 30 s]
| api-token ............................ [file · ttl 3300 s · 1 entry]
| shared ............................... [redis · ttl 120 s · version v1 · 2 entries]
| [ Caches (4) ]If Redis cannot be reached, its cache shows UNAVAILABLE instead of a count, and
the command exits with code 1.
show <name>
| shared ............................... [redis · ttl 120 s · version v1]
| [i] cache:shared:v1:* on Main\Config\MainRedisConfig
| [ Entries (2) ]
| user:1 ............................... [74 B · 119 s left]
| user:2 ............................... [74 B · 119 s left]
| [ Entries (2) ]Keys are shown as the attribute builds them. A key built from several arguments is
printed with commas between them, and null as the word null. The [i] line says
where the entries live: a folder for file, the prefix and the connection class for
redis.
By default the first 100 entries are printed with the total; --all prints them all.
clear
| products ............................. [SKIPPED (table, server memory)]
| stock ................................ [SKIPPED (memory, server memory)]
| api-token ............................ [CLEARED (1 entries)]
| shared ............................... [CLEARED (2 entries)]clear --all empties every file and redis cache and skips table and memory.
Clearing redis removes only the keys with this cache’s prefix — the rest of the
database is left alone.
`clear` on `table` and `memory` is an error
call cache clear products on a table cache does not run: the command explains
that it lives in the server’s memory and is emptied by a restart, and exits with code
1. That way a script that counted on the clear learns it did not happen.
In a deploy script
file and redis caches survive a deploy — together with entries written by the old
code. If the classes of the values changed, clear them before starting:
php call di build || exit 1
php call cache clear --all || exit 1
exec php call runThe command exits with code 1 when something did not work: an unknown cache name,
an unreachable Redis, an attempt to clear table or memory. Without #[EnableCaching]
on the application it also exits with 1 and says caching is not enabled.
The alternative to clearing is changing the cache’s version: older entries are then
simply not found. More in
Deploys and old data.
Next
- Method caching — the attributes, the stores and their settings
- The di command — building proxies and checking cache names
- The storage command — the service folders
filecaches live in - Console — overview — every command