Elgg  Version 6.2
QueryCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use Elgg\Config;
6 
12 class QueryCache extends CacheService {
13 
14  protected int $query_cache_limit = 50;
15 
16  protected array $keys = [];
17 
23  public function __construct(protected Config $config) {
25 
26  $this->cache = new CompositeCache('query_cache', $this->config, $flags);
27 
28  $this->enabled = $this->config->db_disable_query_cache !== true;
29  if (isset($this->config->db_query_cache_limit)) {
30  $this->query_cache_limit = (int) $this->config->db_query_cache_limit;
31  }
32  }
33 
37  public function purge(): void {
38  $this->keys = [];
39 
40  parent::purge();
41  }
42 
46  public function invalidate(): void {
47  $this->keys = [];
48 
49  parent::invalidate();
50  }
51 
55  public function clear(): void {
56  $this->keys = [];
57 
58  parent::clear();
59  }
60 
64  public function save(string $key, mixed $data, null|int|\DateTime $expire_after = null): bool {
65  $result = parent::save($key, $data, $expire_after);
66  if ($result && !isset($this->keys[$key])) {
67  $this->keys[$key] = true;
68 
69  if (count($this->keys) > $this->query_cache_limit) {
70  $this->delete(array_key_first($this->keys));
71  }
72  }
73 
74  return $result;
75  }
76 
80  public function load(string $key): mixed {
81  if (isset($this->keys[$key])) {
82  // when used move key to bottom
83  unset($this->keys[$key]);
84  $this->keys[$key] = true;
85  }
86 
87  return parent::load($key);
88  }
89 
93  public function delete(string $key): bool {
94  unset($this->keys[$key]);
95 
96  return parent::delete($key);
97  }
98 }
if(! $entity instanceof \ElggUser) $data
Definition: attributes.php:13
Composite cache pool.
Volatile cache for select queries.
Definition: QueryCache.php:12
load(string $key)
{Retrieve the contents of a cached item.Identifier of the cached itemmixed null if key not found in c...
Definition: QueryCache.php:80
purge()
{Purges the caches.void}
Definition: QueryCache.php:37
__construct(protected Config $config)
Constructor.
Definition: QueryCache.php:23
save(string $key, mixed $data, null|int|\DateTime $expire_after=null)
{Saves data in the cache.Identifier of the cached item The data to be saved Number of seconds to expi...
Definition: QueryCache.php:64
clear()
{Clears the caches.void}
Definition: QueryCache.php:55
invalidate()
{Invalidates the caches.void}
Definition: QueryCache.php:46
Extension of the DateTime class to support formatting a date using the locale.
Definition: DateTime.php:12
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10