This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCache.php
173 lines (145 loc) · 4.06 KB
/
Cache.php
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @package Piwik.Counter
* @copyright Copyright (C) 2010 Libra.ms. All rights reserved.
* @license GNU General Public License version 3 or later
* @url http://xn--80aeqbhthr9b.com/en/others/piwik/10-piwik-graphical-counter.html
* @url http://киноархив.com/ru/разное/piwik/9-piwik-графический-счетчик.html
*/
namespace Piwik\Plugins\Counter;
/**
* Plugin cache class.
*/
class Cache
{
protected $options = array(
'cacheDir' => '',
'cacheId' => '',
'fileLocking' => true,
'cache_file_umask' => 0600
);
public function __construct($options)
{
$this->options['cacheDir'] = array_key_exists('cacheDir', $options) ? $options['cacheDir'] : '';
$this->options['cacheId'] = array_key_exists('cacheId', $options) ? $options['cacheId'] : '';
$this->options['fileLocking'] = array_key_exists('fileLocking', $options) ? $options['fileLocking'] : $this->options['fileLocking'];
}
/**
* Validate cache.
*
* @return boolean
*/
public function validate()
{
$metaFile = file_get_contents($this->options['cacheDir'] . $this->options['cacheId'] . '.meta');
if ($metaFile)
{
$metaInfo = json_decode($metaFile);
if (time() <= $metaInfo->lifetime)
{
return true;
}
}
return false;
}
/**
* Start the cache.
*
* @param integer $lifetime Cache lifetime.
*
* @return boolean
*/
public function start($lifetime)
{
$metaFile = $this->options['cacheDir'] . $this->options['cacheId'] . '.meta';
$metaInfo = json_encode(
(object) array(
'lifetime' => (time() + (int) $lifetime)
)
);
file_put_contents($metaFile, $metaInfo);
ob_start();
ob_implicit_flush(false);
return false;
}
/**
* Stop the cache.
*
* @return void
*/
public function end()
{
$data = ob_get_clean();
$this->save($data);
echo $data;
}
/**
* Load the cache.
*
* @return boolean
*/
public function load()
{
$result = false;
if (!is_file($this->options['cacheDir'] . $this->options['cacheId'])) {
return false;
}
$f = @fopen($this->options['cacheDir'] . $this->options['cacheId'], 'rb');
if ($f)
{
if ($this->options['fileLocking']) @flock($f, LOCK_SH);
$result = stream_get_contents($f);
if ($this->options['fileLocking']) @flock($f, LOCK_UN);
@fclose($f);
}
return $result;
}
/**
* Save some string data into a cache record.
*
* @param mixed $data Cache data.
*
* @return boolean
*/
public function save($data)
{
clearstatcache();
$result = false;
$f = @fopen($this->options['cacheDir'] . $this->options['cacheId'], 'ab+');
if ($f)
{
if ($this->options['fileLocking']) @flock($f, LOCK_EX);
fseek($f, 0);
ftruncate($f, 0);
$tmp = @fwrite($f, $data);
if (!($tmp === FALSE))
{
$result = true;
}
@fclose($f);
}
@chmod($this->options['cacheDir'] . $this->options['cacheId'], $this->options['cache_file_umask']);
return $result;
}
/**
* Remove cache item(s).
*
* @param string $id Cache ID.
*
* @return boolean
*/
public function remove($id)
{
clearstatcache();
$cacheId = !empty($this->options['cacheId']) ? $this->options['cacheId'] : $id;
if (!is_file($this->options['cacheDir'] . $cacheId)) {
return false;
}
if (!@unlink($this->options['cacheDir'] . $cacheId)) {
return false;
}
// Remove meta file.
@unlink($this->options['cacheDir'] . $cacheId . '.meta');
return true;
}
}