Elgg  Version 3.0
QueryCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use Elgg\Loggable;
6 
17 class QueryCache extends LRUCache {
18  use Loggable;
19 
23  protected $config_disabled = false;
24 
28  protected $runtime_enabled = false;
29 
36  public function __construct(int $size = 50, bool $config_disabled = false) {
37 
38  $this->config_disabled = $config_disabled;
39 
40  parent::__construct($size);
41  }
42 
50  public function enable() {
51  $this->runtime_enabled = true;
52  }
53 
62  public function disable() {
63  $this->runtime_enabled = false;
64 
65  $this->clear();
66  }
67 
73  public function isEnabled() {
74  if ($this->config_disabled) {
75  return false;
76  }
77 
79  }
80 
84  public function clear() {
85  parent::clear();
86 
87  if ($this->logger) {
88  $this->logger->info('Query cache invalidated');
89  }
90  }
91 
95  public function get($key, $default = null) {
96  if (!$this->isEnabled()) {
97  return $default;
98  }
99 
100  $result = parent::get($key, $default);
101 
102  if ($this->logger) {
103  $this->logger->info("DB query results returned from cache (hash: $key)");
104  }
105 
106  return $result;
107  }
108 
112  public function set($key, $value) {
113  if (!$this->isEnabled()) {
114  return;
115  }
116 
117  parent::set($key, $value);
118 
119  if ($this->logger) {
120  $this->logger->info("DB query results cached (hash: $key)");
121  }
122  }
123 
133  public function getHash(string $sql, array $params = [], string $extras = '') {
134 
135  $query_id = $sql . '|';
136  if (!empty($params)) {
137  $query_id .= serialize($params) . '|';
138  }
139 
140  $query_id .= $extras;
141 
142  // MD5 yields smaller mem usage for cache and cleaner logs
143  return md5($query_id);
144  }
145 
146 }
getHash(string $sql, array $params=[], string $extras= '')
Returns a hashed key for storage in the cache.
Definition: QueryCache.php:133
__construct(int $size=50, bool $config_disabled=false)
Definition: QueryCache.php:36
$params
Saves global plugin settings.
Definition: save.php:13
Least Recently Used Cache.
Definition: LRUCache.php:18
$size
Definition: remove.php:24
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
disable()
Disable the query cache.
Definition: QueryCache.php:62
enable()
Enable the query cache.
Definition: QueryCache.php:50
Volatile cache for select queries.
Definition: QueryCache.php:17
elgg get
Wrapper function for elgg.ajax which forces the request type to &#39;get.
Definition: ajax.js:118
$default
Definition: checkbox.php:35
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
$value
Definition: debugging.php:7
isEnabled()
Checks if this cache is enabled.
Definition: QueryCache.php:73