Fetches a value from the cache, with the option of calculating on miss
public get ( string | integer $key, callable $callback = null, mixed $default = null ) : mixed | ||
$key | string | integer | A plain string ID for the cache entry |
$callback | callable | Logic for calculating the cache entry on miss |
$default | mixed | Default value returned if the value is missing and no callback is provided |
return | mixed | The cache value or the $default if no value and no callable |
/** * Get the value of a datalist element. * * Plugin authors should use elgg_get_config() and pass null for the site GUID. * * @internal Datalists are stored in the datalist table. * * @tip Use datalists to store information common to a full installation. * * @param string $name The name of the datalist * @return string|null|false String if value exists, null if doesn't, false on error * @access private */ function get($name) { $name = trim($name); if (!$this->validateName($name)) { return false; } return $this->cache->get($name, function () use($name) { $escaped_name = $this->db->sanitizeString($name); $result = $this->db->getDataRow("SELECT * FROM {$this->table} WHERE name = '{$escaped_name}'"); return $result ? $result->value : null; }); }
/** * Get scraped data * * @param string $url URL * @return array|void * @throws \InvalidArgumentException */ public function get($url) { if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new \InvalidArgumentException(__METHOD__ . ' expects a valid URL'); } $data = $this->cache->get(sha1($url)); if ($data) { return $data; } $dbprefix = elgg_get_config('dbprefix'); $row = get_data_row("\n\t\t\tSELECT * FROM {$dbprefix}scraper_data\n\t\t\tWHERE url = :url\n\t\t", null, [':url' => $url]); return $row ? unserialize($row->data) : null; }
/** * Gets the id associated with this string, case-sensitively. * Will add the string to the table if not present. * * @param string $string The value * * @return int */ private function getIdCaseSensitive($string) { $string = (string) $string; return $this->cache->get($string, function () use($string) { $escaped_string = $this->db->sanitizeString($string); $query = "SELECT * FROM {$this->getTableName()} WHERE string = BINARY '{$escaped_string}' LIMIT 1"; $results = $this->db->getData($query); if (isset($results[0])) { return $results[0]->id; } else { return $this->add($string); } }); }
/** * Get the value of a datalist element. * * Plugin authors should use elgg_get_config() and pass null for the site GUID. * * @internal Datalists are stored in the datalist table. * * @tip Use datalists to store information common to a full installation. * * @param string $name The name of the datalist * @return string|null|false String if value exists, null if doesn't, false on error * @access private */ function get($name) { $name = trim($name); // cannot store anything longer than 255 characters in db, so catch here if (elgg_strlen($name) > 255) { $this->logger->error("The name length for configuration variables cannot be greater than 255"); return false; } return $this->cache->get($name, function () use($name) { $escaped_name = $this->db->sanitizeString($name); $result = $this->db->getDataRow("SELECT * FROM {$this->table} WHERE name = '{$escaped_name}'"); return $result ? $result->value : null; }); }
/** * Returns an \ElggPlugin object with the path $path. * * @param string $plugin_id The id (dir name) of the plugin. NOT the guid. * @return \ElggPlugin|null */ function get($plugin_id) { return $this->plugins_by_id->get($plugin_id, function () use($plugin_id) { $plugin_id = sanitize_string($plugin_id); $db_prefix = get_config('dbprefix'); $options = array('type' => 'object', 'subtype' => 'plugin', 'joins' => array("JOIN {$db_prefix}objects_entity oe on oe.guid = e.guid"), 'selects' => array("oe.title", "oe.description"), 'wheres' => array("oe.title = '{$plugin_id}'"), 'limit' => 1, 'distinct' => false); $plugins = elgg_get_entities($options); if ($plugins) { return $plugins[0]; } return null; }); }