blob: 6d1c7ed8309fe7a9d409fd7504e69bebd70835ce (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Cache;
use Piwik\Development;
use Piwik\Tracker;
use Piwik\Translate;
/**
* Caching class used for static caching.
*
* TODO the default static cache should actually not be language aware. Especially since we would end up in classes like
* LanguageAwareStaticCache, PluginAwareStaticCache, PluginAwareLanguageAwareStaticCache, PluginAwareXYZStaticCache,...
* once we have dependency injection we should "build" all the caches we need removing duplicated code and extend the
* static cache by using decorators which "enrich" the cache key depending on their awareness.
*/
class StaticCache
{
protected static $staticCache = array();
protected static $entriesToPersist = array();
private $persistForTracker = false;
private $cacheKey;
public function __construct($cacheKey)
{
$this->setCacheKey($cacheKey);
}
public function enablePersistForTracker()
{
$this->persistForTracker = true;
}
public function setCacheKey($cacheKey)
{
$this->cacheKey = $this->completeKey($cacheKey);
}
public function get()
{
return self::$staticCache[$this->cacheKey];
}
public function has()
{
return array_key_exists($this->cacheKey, self::$staticCache);
}
public function set($content)
{
self::$staticCache[$this->cacheKey] = $content;
if ($this->persistForTracker) {
self::$entriesToPersist[] = $this->cacheKey;
}
}
public static function loadTrackerCache()
{
if (Development::isEnabled()) {
return;
}
$cache = \Piwik\Tracker\Cache::getCacheGeneral();
if (array_key_exists('staticCache', $cache)) {
self::$staticCache = $cache['staticCache'];
}
}
public static function saveTrackerCache()
{
$cache = \Piwik\Tracker\Cache::getCacheGeneral();
if (array_key_exists('staticCache', $cache)) {
$oldContent = $cache['staticCache'];
} else {
$oldContent = array();
}
$save = false;
foreach (self::$entriesToPersist as $key) {
if (!array_key_exists($key, $oldContent) && array_key_exists($key, self::$staticCache)) {
$oldContent[$key] = self::$staticCache[$key];
$save = true;
}
}
if (!empty($save)) {
$cache['staticCache'] = $oldContent;
\Piwik\Tracker\Cache::setCacheGeneral($cache);
}
}
protected function completeKey($cacheKey)
{
return $cacheKey;
}
}
|