Example #1
0
include "vendor/autoload.php";
/**
 * This example shows how to create a connection and then query for a key,
 * set data and then retrieve the data.
 */
/**
 * Create the redis object and initialise a connection.
 */
$helper = new RedisHelper\RedisHelper();
$helper->connect(true, 'localhost', 6379, '');
$ourKey = "example_key";
/**
 * So if the key does not exist, lets store some data with that key.
 */
if ($helper->exists($ourKey) === false) {
    $helper->set($ourKey, 'This is our test data');
}
/**
 * And now lets check if that key now exists.
 */
if ($helper->exists($ourKey) === true) {
    echo $helper->get($ourKey) . "\n";
}
/**
 * Re-initialise the helper object and start from fresh to
 * absolutely 100% ensure that our data was saved to redis.
 */
$helper = new RedisHelper\RedisHelper();
$helper->connect(true, 'localhost', 6379, '');
echo $helper->get($ourKey) . "\n";
$helper = new RedisHelper\RedisHelper();
$helper->connect(true, 'localhost', 6379, '');
$ourKey = "our_number_key";
$ourNumber = 1;
/**
 * Start off by setting the key
 *
 * Note that if we do not check for its exist()ance then we will
 * overwrite the value.
 */
$helper->set($ourKey, $ourNumber);
/**
 * Increase by 1
 */
$helper->add($ourKey);
echo "The number is now:" . $helper->get($ourKey) . "\n";
// 2
/**
 * Increase by 4
 */
$helper->add($ourKey, 4);
echo "The number is now:" . $helper->get($ourKey) . "\n";
// 6
/**
 * Decrease by 1
 */
$helper->subtract($ourKey);
echo "The number is now:" . $helper->get($ourKey) . "\n";
// 6
/**
 * Decrease by 3
$ourString = "Totally generic string";
$ttl = 300;
// 5 minutes
/**
 * Good
 */
if ($helper->exists($ourKey) === false) {
    /**
     * TTL: Time to live. How long the value should persist in the
     * cache before Redis purges it. If no value is passed it defaults
     * to the constant value 14400.
     */
    $helper->set($ourKey, $ourString, $ttl);
    echo "Not set:" . $ourString . "\n";
} else {
    echo "Set:" . $helper->get($ourKey) . "\n";
}
/**
 * Bad
 *
 * The reason I am pointing this out is I have seen several
 * implementations where people store the key/data pair and
 * then retrieve it. You already have the value, by using get()
 * you are introducing a second call to Redis unnecessarily.
 *
 * set() used to return $this, so to prevent this abuse it
 * has been removed so method chaining can not be used.
 * I.e. $helper->set()->get()
 */
if ($helper->exists($ourKey) === false) {
    $helper->set($ourKey, $ourString);
/**
 * This example shows the results of doing naughty actions.
 */
include "vendor/autoload.php";
/**
 * Create the redis object and initialise a connection.
 */
$helper = new RedisHelper\RedisHelper();
$helper->connect(true, 'localhost', 6379, '');
/**
 * Requesting a key that does not exist.
 *
 * Result: If you use get() to request an unset key, it simply returns false.
 */
echo "The value is: " . ($helper->get("an_unset_key") !== false) . "\n";
/**
 * Attempting to increase a string.
 */
$key = "set_string_for_add";
$helper->set($key, "Test string");
echo "The value is:" . $helper->add($key, 5)->get($key) . "\n";
exit;
/**
 * Increasing a number that does not exist.
 *
 * Result: Throws an exception.
 */
$helper->add("unset_number_for_add", 5);
/**
 * Decreasing a number that does not exist.