<?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"; } }
* 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"; } 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()
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";
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. * * Result: Throws an exception. */ $helper->subtract("unset_number_for_subtract", 5);