/** * 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 */ $helper->subtract($ourKey, 3); echo "The number is now:" . $helper->get($ourKey) . "\n"; // 6
<?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"; } }
/** * 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. * * Result: Throws an exception. */ $helper->subtract("unset_number_for_subtract", 5);