|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Database\Table; |
| 11 | + |
| 12 | +use Nette\Caching\Cache; |
| 13 | +use Nette\Caching\IStorage; |
| 14 | + |
| 15 | + |
| 16 | +class ColumnAccessCache |
| 17 | +{ |
| 18 | + /** @var Selection */ |
| 19 | + protected $selection; |
| 20 | + |
| 21 | + /** @var Cache */ |
| 22 | + protected $cache; |
| 23 | + |
| 24 | + /** @var string */ |
| 25 | + protected $generalCacheKey; |
| 26 | + |
| 27 | + /** @var string */ |
| 28 | + protected $specificCacheKey; |
| 29 | + |
| 30 | + /** @var array of touched columns */ |
| 31 | + protected $accessedColumns = []; |
| 32 | + |
| 33 | + /** @var array of earlier touched columns */ |
| 34 | + protected $previousAccessedColumns; |
| 35 | + |
| 36 | + /** @var Selection instance of observed accessed columns */ |
| 37 | + protected $observeCache; |
| 38 | + |
| 39 | + |
| 40 | + public function __construct(Selection $selection, IStorage $cacheStorage = null) |
| 41 | + { |
| 42 | + $this->selection = $selection; |
| 43 | + $this->cache = $cacheStorage ? new Cache($cacheStorage, 'Nette.Database.' . md5($selection->getConnection()->getDsn())) : null; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public function getStorage(): ?IStorage |
| 48 | + { |
| 49 | + return $this->cache ? $this->cache->getStorage() : null; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns general cache key independent on query parameters or sql limit |
| 55 | + * Used e.g. for previously accessed columns caching |
| 56 | + */ |
| 57 | + public function getGeneralCacheKey(): string |
| 58 | + { |
| 59 | + if ($this->generalCacheKey) { |
| 60 | + return $this->generalCacheKey; |
| 61 | + } |
| 62 | + |
| 63 | + $key = [__CLASS__, $this->selection->getName(), $this->selection->getSqlBuilder()->getConditions()]; |
| 64 | + $trace = []; |
| 65 | + foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $item) { |
| 66 | + $trace[] = isset($item['file'], $item['line']) ? $item['file'] . $item['line'] : null; |
| 67 | + } |
| 68 | + |
| 69 | + $key[] = $trace; |
| 70 | + return $this->generalCacheKey = md5(serialize($key)); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public function setGeneralCacheKey(?string $key): void |
| 75 | + { |
| 76 | + $this->generalCacheKey = $key; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns object specific cache key dependent on query parameters |
| 82 | + * Used e.g. for reference memory caching |
| 83 | + */ |
| 84 | + public function getSpecificCacheKey(): string |
| 85 | + { |
| 86 | + if ($this->specificCacheKey) { |
| 87 | + return $this->specificCacheKey; |
| 88 | + } |
| 89 | + |
| 90 | + return $this->specificCacheKey = $this->selection->getSqlBuilder()->getSelectQueryHash($this->getPreviousAccessedColumns()); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + public function setSpecificCacheKey(?string $key): void |
| 95 | + { |
| 96 | + $this->specificCacheKey = $key; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + public function getAccessedColumns(): array |
| 101 | + { |
| 102 | + return $this->accessedColumns; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public function setAccessedColumns(array $accessedColumns): void |
| 107 | + { |
| 108 | + if ($this->cache) { |
| 109 | + $this->accessedColumns = $accessedColumns; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public function setAccessedColumn(string $key, bool $value): void |
| 115 | + { |
| 116 | + if ($this->cache) { |
| 117 | + $this->accessedColumns[$key] = $value; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /** |
| 123 | + * Loads cache of previous accessed columns and returns it. |
| 124 | + */ |
| 125 | + public function getPreviousAccessedColumns(): array |
| 126 | + { |
| 127 | + if ($this->cache && $this->previousAccessedColumns === null) { |
| 128 | + $this->accessedColumns = $this->previousAccessedColumns = $this->cache->load($this->getGeneralCacheKey()) ?: []; |
| 129 | + } |
| 130 | + |
| 131 | + return array_keys(array_filter((array) $this->previousAccessedColumns)); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + public function setPreviousAccessedColumns(array $previousAccessedColumns): void |
| 136 | + { |
| 137 | + $this->previousAccessedColumns = $previousAccessedColumns; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + public function clearPreviousAccessedColumns(): void |
| 142 | + { |
| 143 | + $this->previousAccessedColumns = null; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + public function saveState(): void |
| 148 | + { |
| 149 | + if ($this->observeCache === $this->selection && $this->cache && !$this->selection->getSqlBuilder()->getSelect() && $this->accessedColumns !== $this->previousAccessedColumns) { |
| 150 | + $previousAccessed = $this->cache->load($this->getGeneralCacheKey()); |
| 151 | + $accessed = $this->accessedColumns; |
| 152 | + $needSave = is_array($previousAccessed) |
| 153 | + ? array_intersect_key($accessed, $previousAccessed) !== $accessed |
| 154 | + : $accessed !== $previousAccessed; |
| 155 | + |
| 156 | + if ($needSave) { |
| 157 | + $save = is_array($previousAccessed) ? $previousAccessed + $accessed : $accessed; |
| 158 | + $this->cache->save($this->getGeneralCacheKey(), $save); |
| 159 | + $this->previousAccessedColumns = null; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + public function &loadFromRefCache(&$referencing): string |
| 166 | + { |
| 167 | + $hash = $this->getSpecificCacheKey(); |
| 168 | + $this->observeCache = &$referencing['observeCache']; |
| 169 | + $this->accessedColumns = &$referencing[$hash]['accessed']; |
| 170 | + $this->specificCacheKey = &$referencing[$hash]['specificCacheKey']; |
| 171 | + |
| 172 | + if ($this->accessedColumns === null) { |
| 173 | + $this->accessedColumns = []; |
| 174 | + } |
| 175 | + |
| 176 | + return $hash; |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + /** |
| 181 | + * @param Selection |
| 182 | + */ |
| 183 | + public function setObserveCache(Selection $observeCache): void |
| 184 | + { |
| 185 | + $this->observeCache = $observeCache; |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + /** |
| 190 | + * @internal |
| 191 | + */ |
| 192 | + public function setSelection(Selection $selection): void |
| 193 | + { |
| 194 | + $this->selection = $selection; |
| 195 | + } |
| 196 | +} |
0 commit comments