BackendInterface.php
5 years ago
BaseSettingsTable.php
6 years ago
Cache.php
5 years ago
Config.php
5 years ago
MeasurableSettingsTable.php
5 years ago
NullBackend.php
5 years ago
PluginSettingsTable.php
5 years ago
SitesTable.php
5 years ago
Cache.php
78 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Settings\Storage\Backend; |
| 11 | |
| 12 | use Piwik\Tracker; |
| 13 | use Piwik\Cache as PiwikCache; |
| 14 | |
| 15 | /** |
| 16 | * Loads settings from tracker cache instead of database. If not yet present in tracker cache will cache it. |
| 17 | * |
| 18 | * Can be used as a decorator in combination with any other storage backend. |
| 19 | */ |
| 20 | class Cache implements BackendInterface |
| 21 | { |
| 22 | /** |
| 23 | * @var BackendInterface |
| 24 | */ |
| 25 | private $backend; |
| 26 | |
| 27 | public function __construct(BackendInterface $backend) |
| 28 | { |
| 29 | $this->backend = $backend; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Saves (persists) the current setting values in the database. |
| 34 | */ |
| 35 | public function save($values) |
| 36 | { |
| 37 | $this->backend->save($values); |
| 38 | self::clearCache(); |
| 39 | } |
| 40 | |
| 41 | public function getStorageId() |
| 42 | { |
| 43 | return $this->backend->getStorageId(); |
| 44 | } |
| 45 | |
| 46 | public function delete() |
| 47 | { |
| 48 | $this->backend->delete(); |
| 49 | self::clearCache(); |
| 50 | } |
| 51 | |
| 52 | public function load() |
| 53 | { |
| 54 | $cacheId = $this->getStorageId(); |
| 55 | $cache = self::buildCache(); |
| 56 | |
| 57 | if ($cache->contains($cacheId)) { |
| 58 | return $cache->fetch($cacheId); |
| 59 | } |
| 60 | |
| 61 | $settings = $this->backend->load(); |
| 62 | $cache->save($cacheId, $settings); |
| 63 | |
| 64 | return $settings; |
| 65 | } |
| 66 | |
| 67 | public static function clearCache() |
| 68 | { |
| 69 | Tracker\Cache::deleteTrackerCache(); |
| 70 | self::buildCache()->flushAll(); |
| 71 | } |
| 72 | |
| 73 | public static function buildCache() |
| 74 | { |
| 75 | return PiwikCache::getEagerCache(); |
| 76 | } |
| 77 | } |
| 78 |