Horde_Memcache::get PHP Method

get() public method

Get data associated with a key.
See also: Memcache::get()
public get ( mixed $keys ) : mixed
$keys mixed The key or an array of keys.
return mixed The string/array on success (return type is the type of $keys), false on failure.
    public function get($keys)
    {
        $flags = null;
        $key_map = $missing_parts = $os = $out_array = array();
        $ret_array = true;
        if (!is_array($keys)) {
            $keys = array($keys);
            $ret_array = false;
        }
        $search_keys = $keys;
        foreach ($search_keys as $v) {
            $key_map[$v] = $this->_key($v);
        }
        if ($this->_memcache instanceof Memcached) {
            $res = $this->_memcache->getMulti(array_values($key_map));
        } else {
            $res = $this->_memcache->get(array_values($key_map), $flags);
        }
        if ($res === false) {
            return false;
        }
        /* Check to see if we have any oversize items we need to get. */
        if (!empty($this->_params['large_items'])) {
            foreach ($key_map as $key => $val) {
                $part_count = isset($flags[$val]) ? ($flags[$val] >> self::FLAGS_RESERVED) - 1 : -1;
                switch ($part_count) {
                    case -1:
                        /* Ignore. */
                        unset($res[$val]);
                        break;
                    case 0:
                        /* Not an oversize part. */
                        break;
                    default:
                        $os[$key] = $this->_getOSKeyArray($key, $part_count);
                        foreach ($os[$key] as $val2) {
                            $missing_parts[] = $key_map[$val2] = $this->_key($val2);
                        }
                        break;
                }
            }
            if (!empty($missing_parts)) {
                if (($res2 = $this->_memcache->get($missing_parts)) === false) {
                    return false;
                }
                /* $res should now contain the same results as if we had
                 * run a single get request with all keys above. */
                $res = array_merge($res, $res2);
            }
        }
        foreach ($key_map as $k => $v) {
            if (!isset($res[$v])) {
                $this->_noexist[$k] = true;
            }
        }
        foreach ($keys as $k) {
            $out_array[$k] = false;
            if (isset($res[$key_map[$k]])) {
                $data = $res[$key_map[$k]];
                if (isset($os[$k])) {
                    foreach ($os[$k] as $v) {
                        if (isset($res[$key_map[$v]])) {
                            $data .= $res[$key_map[$v]];
                        } else {
                            $this->delete($k);
                            continue 2;
                        }
                    }
                }
                $out_array[$k] = @unserialize($data);
            } elseif (isset($os[$k]) && !isset($res[$key_map[$k]])) {
                $this->delete($k);
            }
        }
        return $ret_array ? $out_array : reset($out_array);
    }

Usage Example

Esempio n. 1
0
 /**
  */
 public function get($key, $lifetime = 0)
 {
     $original_key = $key;
     $key = $this->_params['prefix'] . $key;
     if (isset($this->_expirecache[$key])) {
         return $this->_expirecache[$key];
     }
     $key_list = array($key);
     if (!empty($lifetime)) {
         $key_list[] = $key . '_e';
     }
     $res = $this->_memcache->get($key_list);
     if ($res === false) {
         unset($this->_expirecache[$key]);
     } else {
         // If we can't find the expire time, assume we have exceeded it.
         if (empty($lifetime) || $res[$key . '_e'] !== false && $res[$key . '_e'] + $lifetime > time()) {
             $this->_expirecache[$key] = $res[$key];
         } else {
             $res[$key] = false;
             $this->expire($original_key);
         }
     }
     return $res[$key];
 }
All Usage Examples Of Horde_Memcache::get