<?php /** * This example shows a series of number additions and subtractions pulled from * an array of up/down scores. */ include "vendor/autoload.php"; /** * Create the redis object and initialise a connection. */ $helper = new RedisHelper\RedisHelper(); $helper->connect(true, 'localhost', 6379, ''); $ourKey = "our_vote_site_key"; $ourNumber = 100; $helper->set($ourKey, $ourNumber); $votes = [['type' => 'down', 'value' => 1], ['type' => 'up', 'value' => 1], ['type' => 'up', 'value' => 3], ['type' => 'down', 'value' => 2]]; foreach ($votes as $vote) { echo "Rolling count: "; if ($vote['type'] == 'down') { echo $helper->subtract($ourKey, $vote['value'])->get($ourKey) . "\n"; } else { echo $helper->add($ourKey, $vote['value'])->get($ourKey) . "\n"; } }
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";
<?php /** * This example shows a series of number additions and subtractions. */ include "vendor/autoload.php"; /** * Create the redis object and initialise a connection. */ $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";
<?php /** * This example shows a more practical example of checking, storing * & retrieving data. */ include "vendor/autoload.php"; /** * Create the redis object and initialise a connection. */ $helper = new RedisHelper\RedisHelper(); $helper->connect(true, 'localhost', 6379, ''); /** * For our example we are going to check if a key exists and if it * does, print it out else store data to it and then print it out. */ $ourKey = "our_practical_example_key"; $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";
<?php /** * 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); /**