The "cache" action

Mechanic offers a simple key/value cache that you can use in your tasks for temporary value storage. Cache values may be retrieved in task scripts using the cache object, e.g. {{ cache[key] }}, or via cache endpoints. Cache values may each be up to 256 kilobytes.

For more on the cache and its purpose and restrictions, see Using the cache.

Usage

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 this expression:

/^[a-z0-9_:\-\.\/]+$/i

Note that the commands "incr", "incrby", "decr", and "decrby" will all behave as intended during concurrent action runs – this is a trait inherited from the related Redis commands.

Expiration

By default, each key will expire in 60 days. Any updates to existing values (i.e. incr/incrby/decr/decrby) will reset this expiration period for that key.

To set a custom expiration period, call this action using an object of options, and add a "ttl" option defining the number of seconds the value should last. In this example, we increment the value stored in "foo", and set the key to expire in 10 minutes:

{% action "cache" %}
  {
    "incr": {
      "key": "foo",
      "ttl": 600
    }
  }
{% endaction %}

Commands

set

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

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, using an expiration time of 1 minute:

{% action "cache" %}
  {
    "set": {
      "key": "foo",
      "value": "bar",
      "ttl": 60
    }
  }
{% endaction %}

Clear a cache value:

{% action "cache" %}
  {
    "del": {
      "key": "foobar"
    }
  }
{% endaction %}
{% action "cache", "del", "foobar" %}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.