Elgg  Version 4.3
QueryCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
6 
17 class QueryCache extends LRUCache {
18 
19  use Loggable;
20 
24  protected $config_disabled = false;
25 
29  protected $runtime_enabled = false;
30 
37  public function __construct(int $size = 50, bool $config_disabled = false) {
38 
39  $this->config_disabled = $config_disabled;
40 
41  parent::__construct($size);
42  }
43 
51  public function enable() {
52  $this->runtime_enabled = true;
53  }
54 
65  public function disable(bool $clear = true) {
66  $this->runtime_enabled = false;
67 
68  if ($clear) {
69  $this->clear();
70  }
71  }
72 
78  public function isEnabled() {
79  if ($this->config_disabled) {
80  return false;
81  }
82 
84  }
85 
89  public function clear() {
90  parent::clear();
91 
92  $this->getLogger()->info('Query cache invalidated');
93  }
94 
98  public function get($key, $default = null) {
99  if (!$this->isEnabled()) {
100  return $default;
101  }
102 
103  $result = parent::get($key, $default);
104 
105  $this->getLogger()->info("DB query results returned from cache (hash: {$key})");
106 
107  return $result;
108  }
109 
113  public function set($key, $value) {
114  if (!$this->isEnabled()) {
115  return;
116  }
117 
118  parent::set($key, $value);
119 
120  $this->getLogger()->info("DB query results cached (hash: {$key})");
121  }
122 
132  public function getHash(string $sql, array $params = [], string $extras = '') {
133 
134  $query_id = $sql . '|';
135  if (!empty($params)) {
136  $query_id .= serialize($params) . '|';
137  }
138 
139  $query_id .= $extras;
140 
141  // MD5 yields smaller mem usage for cache and cleaner logs
142  return md5($query_id);
143  }
144 
145 }
$default
Definition: checkbox.php:31
disable(bool $clear=true)
Disable the query cache.
Definition: QueryCache.php:65
getHash(string $sql, array $params=[], string $extras= '')
Returns a hashed key for storage in the cache.
Definition: QueryCache.php:132
__construct(int $size=50, bool $config_disabled=false)
Definition: QueryCache.php:37
$params
Saves global plugin settings.
Definition: save.php:13
Least Recently Used Cache.
Definition: LRUCache.php:17
$value
Definition: generic.php:51
enable()
Enable the query cache.
Definition: QueryCache.php:51
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
Volatile cache for select queries.
Definition: QueryCache.php:17
$size
Definition: thumb.php:23
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
getLogger()
Returns logger.
Definition: Loggable.php:37
isEnabled()
Checks if this cache is enabled.
Definition: QueryCache.php:78