blob: de17d5271f1dd5156c0b98ac6100d4f86e0cb413 (
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
/**
* Cache with file system
*/
class FileCache implements CacheInterface {
protected $path;
protected $param;
public function loadData(){
if(file_exists($this->getCacheFile())) {
return unserialize(file_get_contents($this->getCacheFile()));
}
}
public function saveData($datas){
// Notice: We use plain serialize() here to reduce memory footprint on
// large input data.
$writeStream = file_put_contents($this->getCacheFile(), serialize($datas));
if($writeStream === false) {
throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
}
return $this;
}
public function getTime(){
$cacheFile = $this->getCacheFile();
if(file_exists($cacheFile)) {
return filemtime($cacheFile);
}
return false;
}
public function purgeCache($duration){
$cachePath = $this->getPath();
if(file_exists($cachePath)) {
$cacheIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cachePath),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach($cacheIterator as $cacheFile) {
if(in_array($cacheFile->getBasename(), array('.', '..', '.gitkeep')))
continue;
elseif($cacheFile->isFile()) {
if(filemtime($cacheFile->getPathname()) < time() - $duration)
unlink($cacheFile->getPathname());
}
}
}
}
/**
* Set cache path
* @return self
*/
public function setPath($path){
if(is_null($path) || !is_string($path)) {
throw new \Exception('The given path is invalid!');
}
$this->path = $path;
// Make sure path ends with '/' or '\'
$lastchar = substr($this->path, -1, 1);
if($lastchar !== '/' && $lastchar !== '\\')
$this->path .= '/';
if(!is_dir($this->path))
mkdir($this->path, 0755, true);
return $this;
}
/**
* Set HTTP GET parameters
* @return self
*/
public function setParameters(array $param){
$this->param = array_map('strtolower', $param);
return $this;
}
/**
* Return cache path (and create if not exist)
* @return string Cache path
*/
protected function getPath(){
if(is_null($this->path)) {
throw new \Exception('Call "setPath" first!');
}
return $this->path;
}
/**
* Get the file name use for cache store
* @return string Path to the file cache
*/
protected function getCacheFile(){
return $this->getPath() . $this->getCacheName();
}
/**
* Determines file name for store the cache
* return string
*/
protected function getCacheName(){
if(is_null($this->param)) {
throw new \Exception('Call "setParameters" first!');
}
// Change character when making incompatible changes to prevent loading
// errors due to incompatible file contents \|/
return hash('md5', http_build_query($this->param) . 'A') . '.cache';
}
}
|