<?php

require 'vendor/autoload.php';
Predis\Autoloader::register();
$client = new Predis\Client(array('host' => '127.0.0.1', 'port' => 6379), array('prefix' => 'php:'));
$client->set("string:my_key", "Hello World");
$client->get("string:my_key");
# "Hello World"
$client->incr("string:counter");
$client->mget(array("string:my_key", "string:counter"));
# array('Hello World', '2')
$client->rpush("list:my_list", "item1", "item2");
$client->lpop("list:my_list");
# 'item1'
$client->hset("set:redis_book", "title", "Redis Essentials");
$client->hgetall("set:redis_book");
# array('title' => 'Redis Essentials')
$client->sadd("set:users", "alice", "bob");
$client->smembers("set:users");
# array('bob', 'alice')
$client->zadd("sorted_set:programmers", 1940, "Alan Kay");
$client->zadd("sorted_set:programmers", 1912, "Alan Turing");
$client->zrange("sorted_set:programmers", 0, -1, "withscores");
# array('Alan Turing' => 1912, 'Alan Kay' => 1940)
Esempio n. 2
0
 /**
  */
 protected function _get($keys)
 {
     $keys = array_values($keys);
     $out = array();
     try {
         $data = $this->_predis->mget($keys);
     } catch (Exception $e) {
         /* MGET doesn't work on clusters. */
         $data = array();
         foreach ($keys as $key) {
             $data[$key] = $this->_predis->get($key);
         }
     }
     foreach ($data as $key => $val) {
         $out[$keys[$key]] = is_null($val) ? false : $val;
     }
     return $out;
 }
<?php

/*
 * This file is part of the Predis package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require 'SharedConfigurations.php';
// redis can set keys and their relative values in one go
// using MSET, then the same values can be retrieved with
// a single command using MGET.
$mkv = array('usr:0001' => 'First user', 'usr:0002' => 'Second user', 'usr:0003' => 'Third user');
$client = new Predis\Client($single_server);
$client->mset($mkv);
$retval = $client->mget(array_keys($mkv));
print_r($retval);
/* OUTPUT:
Array
(
    [0] => First user
    [1] => Second user
    [2] => Third user
)
*/
 /**
  * Retrieve multiple values from cache.
  *
  * Gets multiple values from cache, including across multiple groups
  *
  * Usage: array( 'group0' => array( 'key0', 'key1', 'key2', ), 'group1' => array( 'key0' ) )
  *
  * Mirrors the Memcached Object Cache plugin's argument and return-value formats
  *
  * @param   array                           $groups  Array of groups and keys to retrieve
  * @uses    this::filter_redis_get_multi()
  * @return  bool|mixed                               Array of cached values, keys in the format $group:$key. Non-existent keys null.
  */
 public function get_multi($groups)
 {
     if (empty($groups) || !is_array($groups)) {
         return false;
     }
     // Retrieve requested caches and reformat results to mimic Memcached Object Cache's output
     $cache = array();
     foreach ($groups as $group => $keys) {
         if (in_array($group, $this->no_redis_groups) || !$this->redis_status()) {
             foreach ($keys as $key) {
                 $cache[$this->build_key($key, $group)] = $this->get($key, $group);
             }
         } else {
             // Reformat arguments as expected by Redis
             $derived_keys = array();
             foreach ($keys as $key) {
                 $derived_keys[] = $this->build_key($key, $group);
             }
             // Retrieve from cache in a single request
             $group_cache = $this->redis->mget($derived_keys);
             // Build an array of values looked up, keyed by the derived cache key
             $group_cache = array_combine($derived_keys, $group_cache);
             // Restores cached data to its original data type
             $group_cache = array_map(array($this, 'maybe_unserialize'), $group_cache);
             // Redis returns null for values not found in cache, but expected return value is false in this instance
             $group_cache = array_map(array($this, 'filter_redis_get_multi'), $group_cache);
             $cache = array_merge($cache, $group_cache);
         }
     }
     // Add to the internal cache the found values from Redis
     foreach ($cache as $key => $value) {
         if ($value) {
             $this->cache_hits++;
             $this->add_to_internal_cache($key, $value);
         } else {
             $this->cache_misses++;
         }
     }
     return $cache;
 }
Esempio n. 5
0
/*
 * This file is part of the Predis package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license info, please view the LICENSE
 * file that was distributed with this source code.
 */
require 'SharedConfigurations.php';
// Predis ships with a KeyPrefixProcessor class that is used to transparently
// prefix each key before sending commands to Redis, even for complex commands
// such as SORT, ZUNIONSTORE and ZINTERSTORE. Key prefixes are useful to create
// user-level namespaces for you keyspace, thus eliminating the need for separate
// logical databases.
$client = new Predis\Client($single_server, array('prefix' => 'nrk:'));
$client->mset(array('foo' => 'bar', 'lol' => 'wut'));
var_dump($client->mget('foo', 'lol'));
/*
array(2) {
  [0]=> string(3) "bar"
  [1]=> string(3) "wut"
}
*/
var_dump($client->keys('*'));
/*
array(2) {
  [0]=> string(7) "nrk:foo"
  [1]=> string(7) "nrk:lol"
}
*/
 * file that was distributed with this source code.
 */
require __DIR__ . '/shared.php';
$client = new Predis\Client($single_server);
// Plain old SET and GET example...
$client->set('library', 'predis');
$response = $client->get('library');
var_export($response);
echo PHP_EOL;
/* OUTPUT: 'predis' */
// Redis has the MSET and MGET commands to set or get multiple keys in one go,
// cases like this Predis accepts arguments for variadic commands both as a list
// of arguments or an array containing all of the keys and/or values.
$mkv = array('uid:0001' => '1st user', 'uid:0002' => '2nd user', 'uid:0003' => '3rd user');
$client->mset($mkv);
$response = $client->mget(array_keys($mkv));
var_export($response);
echo PHP_EOL;
/* OUTPUT:
  array (
  0 => '1st user',
  1 => '2nd user',
  2 => '3rd user',
  ) */
// Predis can also send "raw" commands to Redis. The difference between sending
// commands to Redis the usual way and the "raw" way is that in the latter case
// their arguments are not filtered nor responses coming from Redis are parsed.
$response = $client->executeRaw(array('MGET', 'uid:0001', 'uid:0002', 'uid:0003'));
var_export($response);
echo PHP_EOL;
/* OUTPUT:
Esempio n. 7
0
    // Check entry value.
    echo ">>> Value for 'k1': " . $redis->get('k1') . "\n";
    // Change entry's value.
    if ($redis->set('k1', 'new_value')) {
        echo ">>> Successfully put entry in cache. \n";
    }
    // Check entry value.
    echo ">>> Value for 'k1': " . $redis->get('k1') . "\n";
    // Put entry to cache.
    if ($redis->set('k2', '2')) {
        echo ">>> Successfully put entry in cache. \n";
    }
    // Check entry value.
    echo ">>> Value for 'k2': " . $redis->get('k2') . "\n";
    // Get two entries.
    $val = $redis->mget('k1', 'k2');
    echo ">>> Value for 'k1' and 'k2': " . var_dump($val) . "\n";
    // Delete on entry.
    if ($redis->del('k1')) {
        echo ">>> Successfully deleted 'k1'. \n";
    }
    // Db size.
    echo ">>> Db size: " . $redis->dbsize() . "\n";
    // Increment.
    echo ">>> Incremented: " . $redis->incr("inc_k") . "\n";
    // Increment by 5.
    echo ">>> Incremented: " . $redis->incrby("inc_k", 5) . "\n";
} catch (Exception $e) {
    echo ">>> Couldn't connected to Redis.";
    echo $e->getMessage();
}
<?php

require_once 'SharedConfigurations.php';
// redis can set keys and their relative values in one go
// using MSET, then the same values can be retrieved with
// a single command using MGET.
$mkv = array('usr:0001' => 'First user', 'usr:0002' => 'Second user', 'usr:0003' => 'Third user');
$redis = new Predis\Client($single_server);
$redis->mset($mkv);
$retval = $redis->mget(array_keys($mkv));
print_r($retval);
/* OUTPUT:
Array
(
    [0] => First user
    [1] => Second user
    [2] => Third user
)
*/
Esempio n. 9
0
<?php

/*
 * This file is part of the Predis package.
 *
 * (c) Daniele Alessandri <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require __DIR__ . '/shared.php';
// Predis can prefix keys found in commands arguments before sending commands to
// Redis, even for complex commands such as SORT, ZUNIONSTORE and ZINTERSTORE.
// Prefixing keys can be useful to create user-level namespaces for you keyspace
// thus reducing the need for separate logical databases in certain scenarios.
$client = new Predis\Client($single_server, array('prefix' => 'nrk:'));
$client->mset(array('foo' => 'bar', 'lol' => 'wut'));
var_export($client->mget('foo', 'lol'));
/*
array (
  0 => 'bar',
  1 => 'wut',
)
*/
var_export($client->keys('*'));
/*
array (
  0 => 'nrk:foo',
  1 => 'nrk:lol',
)
*/