Cache
The Cache action allows developers to interact with the store's Mechanic cache, using commands inspired by Redis. Cache entries have a key, a value containing up to 256 kilobytes, and a ttl (Time To Live) defaulting to the maximum of 60 days.

Options

This action supports two styles of options: a more verbose nested structure, and a simpler set of positional arguments.
All commands must define a cache key, matching the regular expression /^[a-z0-9_:\-\.\/]+$/i.

Verbose options

In this option style, the cache command is given as the root key of the options object. The root value is itself an option, containing the arguments needed for the selected cache command.
Liquid
JSON
1
{% action "cache" %}
2
{
3
"incr": {
4
"key": "foo",
5
"ttl": 600
6
}
7
}
8
{% endaction %}
Copied!
1
{
2
"action": {
3
"type": "cache",
4
"options": {
5
"incr": {
6
"key": "foo",
7
"ttl": 600
8
}
9
}
10
}
11
}
Copied!

Positional options

In this option style, the cache command and its arguments are given in a list. Use the cache command reference below to find the argument order required for each command.
Liquid
JSON
1
{% action "cache", "incr", "foo" %}
Copied!
1
{
2
"action": {
3
"type": "cache",
4
"options": [
5
"incr",
6
"foo"
7
]
8
}
9
}
Copied!

Expiration

Each cache entry is given a default TTL value of 60 days. (A cache entry's TTL may not exceed 60 days.)
A cache command will always reset the entry's TTL value upon execution, regardless of the TTL's original value.

Commands

The required arguments for each command are given below, in the order in which they are supported for positional options.
When a command is given using verbose options, the ttl value (in seconds) is always supported.

set

Stores a value. Requires key and value. The stored value may be any JSON object.

setex

Using a defined TTL (an expiration interval) given in seconds, stores a value. Requires key, ttl, and value. The stored value may be any JSON object.
The "setex" command has the same net functionality as "set", but it does have one difference: because "setex" requires an explicit ttl value, it's possible to use "setex" to express an expiring value using a single line of Liquid. The same result could be achieved with "set", but it would require using verbose options.
1
{% action "cache", "setex", "foo", 5, "bar" %}
Copied!

del

Deletes a stored key. Requires key.

incr

Increments a numeric key by 1. Requires key. If the key is not already set, the value before incrementing will be assumed 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 value before incrementing will be assumed to be 0.

decr

Decrements a numeric key by 1. Requires key. If the key is not already set, the value before incrementing will be assumed 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 value before incrementing will be assumed to be 0.

Examples

Set a value, auto-expiring in 60 days

Verbose options
Positional options
1
{% action "cache" %}
2
{
3
"set": {
4
"key": "foo",
5
"value": 5
6
}
7
}
8
{% endaction %}
Copied!
1
{% action "cache", "set", "foo", 5 %}
Copied!

Set a value, explicitly expiring in 1 minute

Verbose options
Positional options
1
{% action "cache" %}
2
{
3
"setex": {
4
"key": "foo",
5
"ttl": 60,
6
"value": 5
7
}
8
}
9
{% endaction %}
Copied!
1
{% action "cache", "setex", 60, "foo" %}
Copied!

Clear a value

Verbose options
Positional options
1
{% action "cache" %}
2
{
3
"del": {
4
"key": "foo"
5
}
6
}
7
{% endaction %}
Copied!
1
{% action "cache", "del", "foo" %}
Copied!
Last modified 7mo ago