Backed by Redis, Mechanic offers a simple key/value cache store that you can use in your task scripts.
Cache values may be retrieved in task scripts using the cache object, e.g. {{ cache[key] }}
.
For developers
This action has two usage styles: a verbose nested object, and a simple list of positional arguments.
Nested objects are structured with a single root key to define the cache operation, having the value of an object of arguments.
Positional arguments are in the same order as the related Redis command. For reference, see the list of Redis commands. Only the operations listed below are supported with Mechanic's "cache" action.
All operations require an initial "key"
string argument, matching the expression /^[a-z0-9_:\-\.\/]+$/i
.
set
Sets a value. Requires "key"
and "value"
. The stored value may be any JSON object. The key will expire in 60 days from the latest write.
setex
Sets a value, and automatically expires it later. Requires "key"
, "value"
, and a "ttl"
argument, containing an integer from 0 to 5184000, representing the number of seconds before the key expires.
del
Deletes a stored key. Requires "key"
.
incr
Increments a numeric key by 1. Requires "key"
. If the key is not already set, the initial value (before incrementing) is interpreted to be 0.
incrby
Increments a numeric key by the value of your choice. Requires "key"
, and an integer "increment"
. If the key is not already set, the initial value (before incrementing) is interpreted to be 0.
decr
Decrements a numeric key by 1. Requires "key"
. If the key is not already set, the initial value (before decrementing) is interpreted to be 0.
decrby
Decrements a numeric key by the value of your choice. Requires "key"
, and an integer "decrement"
. If the key is not already set, the initial value (before decrementing) is interpreted to be 0.
Examples
Set a cache value (which will auto-expire in 60 days):
{% action "cache" %}
{
"set": {
"key": "foobar",
"value": 5
}
}
{% endaction %}
{% action "cache" "set", "foobar", 5 %}
Set a cache value to expire in 1 minute:
{% action "cache" %}
{
"setex": {
"key": "foo",
"ttl": 60,
"value": "bar"
}
}
{% endaction %}
{% action "cache" "foo", 60, "bar" %}
Clear a cache value:
{% action "cache" %}
{
"del": {
"key": "foobar"
}
}
{% endaction %}
{% action "cache" "del", "foobar" %}