Beispiel #1
0
 /**
  * @param array $keys
  * @return array a key-value array without keys that are not in the hash table
  */
 public function get($keys)
 {
     $keys = array_values($keys);
     $items = $this->redis->hmget($this->name, $keys);
     $keyValues = [];
     for ($i = 0; $i < count($keys); $i++) {
         if (!is_null($items[$i])) {
             $keyValues[$keys[$i]] = $items[$i];
         }
     }
     return ArraySerialization::unserializeArray($keyValues);
 }
Beispiel #2
0
    public function shift($n = 1, $timeout = 0)
    {
        $lua = <<<LUA
                local values = redis.call('lrange', KEYS[1], 0, KEYS[2] - 1)
                redis.call('ltrim', KEYS[1], KEYS[2], - 1)
                return values
LUA;
        $items = $this->redis->eval($lua, 2, $this->name, $n);
        if (!count($items) && $timeout) {
            $items = $this->redis->blpop([$this->name], $timeout);
            if (is_null($items)) {
                return [];
            }
            $items = [$items[1]];
            if ($n > 1) {
                $remainingItems = $this->redis->eval($lua, 2, $this->name, $n - 1);
                $items = array_merge($items, $remainingItems);
            }
        }
        return ArraySerialization::unserializeArray($items);
    }