Jamm\Memory\Shm\SHMObject::read PHP Method

read() public method

Read data from memory storage
public read ( string | array $key, integer &$ttl_left ) : mixed
$key string | array key or array of keys
$ttl_left integer = (ttl - time()) of key. Use to exclude dog-pile effect, with lock/unlock_key methods.
return mixed
    public function read($key, &$ttl_left = -1)
    {
        if (empty($key)) {
            $this->ReportError('empty key are not allowed', __LINE__);
            return NULL;
        }
        $auto_unlocker = NULL;
        if (!$this->mutex->get_access_read($auto_unlocker)) {
            return NULL;
        }
        $map = $this->mem_object->read('map');
        if (empty($map)) {
            $this->ReportError('map are empty', __LINE__);
            return NULL;
        }
        if (is_array($key)) {
            $todelete = array();
            $from_points = array();
            $to_points = array();
            foreach ($key as $arr_key) {
                $arr_key = (string) $arr_key;
                if (!array_key_exists($arr_key, $map)) {
                    continue;
                }
                if (!empty($map[$arr_key][self::map_key_ttl]) && $map[$arr_key][self::map_key_ttl] < time()) {
                    $todelete[] = $arr_key;
                    continue;
                }
                $from_points[] = $map[$arr_key][self::map_key_start];
                $to_points[] = $map[$arr_key][self::map_key_fin];
            }
            if (!empty($todelete)) {
                $this->del($todelete);
            }
            $data = $this->read_data($from_points, $to_points, $key);
            if (!empty($data)) {
                foreach ($data as $key => &$value) {
                    if ($map[$key][self::map_key_serialized] == 1) {
                        $value = unserialize($value);
                    }
                    if (is_numeric($value)) {
                        if (intval($value) == $value) {
                            $value = intval($value);
                        } else {
                            if (floatval($value) == $value) {
                                $value = floatval($value);
                            }
                        }
                    }
                }
            }
        } else {
            $key = (string) $key;
            if (!array_key_exists($key, $map)) {
                return NULL;
            }
            $ttl_left = self::max_ttl;
            if (!empty($map[$key][self::map_key_ttl])) {
                $ttl_left = $map[$key][self::map_key_ttl] - time();
                if ($ttl_left <= 0) {
                    $this->del($key);
                    return NULL;
                }
            }
            $from = $map[$key][self::map_key_start];
            $to = $map[$key][self::map_key_fin];
            $data = $this->read_data($from, $to);
            if ($map[$key][self::map_key_serialized] == 1) {
                $data = unserialize($data);
            } else {
                if (is_numeric($data)) {
                    if (intval($data) == $data) {
                        $data = intval($data);
                    } else {
                        if (floatval($data) == $data) {
                            $data = floatval($data);
                        }
                    }
                }
            }
        }
        return $data;
    }