A simple database key/value store that can be used in place of variable_set and variable_get for persisting simple data, and even handles expiry times (in seconds) and asserting that data exists before using it.

  • Persist data quickly without having to use session or cookies.
  • Set an expiry time in seconds, or have the data persist until it's replaced / set again.
  • Quickly check if a value exists using the dx_cache_has() method.

Please note

This module, as with any other database-based utility, will not sanitise data for you, as a result of this, you should ensure that any inputted data is properly sanitised into a safe state before inserting new data, or printing stored data.

Usage

Simply set a value with dx_cache_set($key, $value, $time_to_live),
for example you can set an item that will expire in 30 seconds like this:

dx_cache_set("my.key", array('my' => 'value'), 30);

Then you can check if it exists (and is valid) using:

dx_cache_has('my.key'); // Return true or false

You can retrieve the value using:

$myvalue = dx_cache_get('my.key'); // Returns the value if valid, false if expired, and false if it does not exist.

You can also set a default value to return if the key is not set:

$myval = dx_cache_get('my.key', 'value to return if not found or expired');

And you can delete values using:

dx_cache_del('my.key');

Cron will also periodically delete expired keys every time it runs.

Project information

Releases