protected function getResults($query, $callback = null, $single = false, array $params = [])
{
// Since we want to cache results of running the callback, we need to
// need to namespace the query with the callback and single result request.
// https://github.com/elgg/elgg/issues/4049
$query_id = (int) $single . $query . '|';
if ($params) {
$query_id .= serialize($params) . '|';
}
if ($callback) {
if (!is_callable($callback)) {
$inspector = new \Elgg\Debug\Inspector();
throw new \RuntimeException('$callback must be a callable function. Given ' . $inspector->describeCallable($callback));
}
$query_id .= $this->fingerprintCallback($callback);
}
// MD5 yields smaller mem usage for cache and cleaner logs
$hash = md5($query_id);
// Is cached?
if ($this->query_cache) {
if (isset($this->query_cache[$hash])) {
if ($this->logger) {
// TODO add params in $query here
$this->logger->info("DB query {$query} results returned from cache (hash: {$hash})");
}
return $this->query_cache[$hash];
}
}
$return = array();
$stmt = $this->executeQuery($query, $this->getConnection('read'), $params);
while ($row = $stmt->fetch()) {
if ($callback) {
$row = call_user_func($callback, $row);
}
if ($single) {
$return = $row;
break;
} else {
$return[] = $row;
}
}
// Cache result
if ($this->query_cache) {
$this->query_cache[$hash] = $return;
if ($this->logger) {
// TODO add params in $query here
$this->logger->info("DB query {$query} results cached (hash: {$hash})");
}
}
return $return;
}