Example #1
0
 /**
  * Returns the value of a given key
  *
  * Examples:
  *
  * ``` php
  * <?php
  * // Strings
  * $I->grabFromRedis('string');
  *
  * // Lists: get all members
  * $I->grabFromRedis('example:list');
  *
  * // Lists: get a specific member
  * $I->grabFromRedis('example:list', 2);
  *
  * // Lists: get a range of elements
  * $I->grabFromRedis('example:list', 2, 4);
  *
  * // Sets: get all members
  * $I->grabFromRedis('example:set');
  *
  * // ZSets: get all members
  * $I->grabFromRedis('example:zset');
  *
  * // ZSets: get a range of members
  * $I->grabFromRedis('example:zset', 3, 12);
  *
  * // Hashes: get all fields of a key
  * $I->grabFromRedis('example:hash');
  *
  * // Hashes: get a specific field of a key
  * $I->grabFromRedis('example:hash', 'foo');
  * ```
  *
  * @param string $key The key name
  *
  * @return mixed
  *
  * @throws ModuleException if the key does not exist
  */
 public function grabFromRedis($key)
 {
     $args = func_get_args();
     switch ($this->driver->type($key)) {
         case 'none':
             throw new ModuleException($this, "Cannot grab key \"{$key}\" as it does not exist");
             break;
         case 'string':
             $reply = $this->driver->get($key);
             break;
         case 'list':
             if (count($args) === 2) {
                 $reply = $this->driver->lindex($key, $args[1]);
             } else {
                 $reply = $this->driver->lrange($key, isset($args[1]) ? $args[1] : 0, isset($args[2]) ? $args[2] : -1);
             }
             break;
         case 'set':
             $reply = $this->driver->smembers($key);
             break;
         case 'zset':
             if (count($args) === 2) {
                 throw new ModuleException($this, "The method grabFromRedis(), when used with sorted " . "sets, expects either one argument or three");
             }
             $reply = $this->driver->zrange($key, isset($args[2]) ? $args[1] : 0, isset($args[2]) ? $args[2] : -1, 'WITHSCORES');
             break;
         case 'hash':
             $reply = isset($args[1]) ? $this->driver->hget($key, $args[1]) : $this->driver->hgetall($key);
             break;
         default:
             $reply = null;
     }
     return $reply;
 }
 /**
  * {@inheritdoc}
  */
 public function getItem($index)
 {
     $this->initializeRedis();
     $key = $this->getKeyForItemList();
     $item = $this->redis->lindex($key, $index);
     return $item;
 }
Example #3
0
 /**
  * Read association from Redis. If no handle given 
  * and multiple associations found, returns latest issued
  */
 function getAssociation($server_url, $handle = null)
 {
     // simple case: handle given
     if ($handle !== null) {
         return $this->getAssociationFromServer($this->associationKey($server_url, $handle));
     }
     // no handle given, receiving the latest issued
     $serverKey = $this->associationServerKey($server_url);
     $lastKey = $this->redis->lindex($serverKey, -1);
     if (!$lastKey) {
         // no previous association with this server
         return null;
     }
     // get association, return null if failed
     return $this->getAssociationFromServer($lastKey);
 }