[check] OOP cache class
Ik ben een beetje bezig geweest met maken van cache classe.
hebben jullie nog opmerkingen/ tips over deze classes?
CacheContoller:
Code (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
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
<?php
class CacheController {
private $sCacheDir;
private $sCacheIndex;
public function __construct($pDir = null) {
$this->sCacheDir = ($pDir == null) ? './cache' : $pDir;
$this->sCacheIndex = $this->sCacheDir.'/cache.index';
}
/**
* Function readIndex.
* This function read the index file. The function unserialize it and return the Array.
* If there is no index file he return a blank Array
*/
private function readIndex() {
if(file_exists($this->sCacheIndex) && filesize($this->sCacheIndex) > 0) {
$h=fopen($this->sCacheIndex,'r');
$c=fread($h, filesize($this->sCacheIndex));
fclose($h);
$s = (strlen($c)>0)? unserialize($c) : array();
return $s;
}
return array();
}
/**
* Function set.
* This function make the cache file.
* The expire time is in seconds. if you want 1 hour, than you make param $pTime 3600.
* @return boolean TRUE on success, or FALSE on error
*/
public function set($pVar, $pId, $pTime, $pCategory = null) {
$c = self::readIndex();
$scalar = is_scalar($pVar);
$c[md5($pId)] = array(
'scalar'=>$scalar,
'category'=>$pCategory,
'expire_time'=>time() + $pTime
);
// make the cache file
$cache_content = $scalar ? $pVar : serialize($pVar);
$h=fopen($this->sCacheDir.'/'.md5($pId).'.cache','w');
if(!fwrite($h,$cache_content)) return false;
fclose($h);
//build or make the index file.
$c=serialize($c);
$h=fopen($this->sCacheIndex, 'w');
if(!fwrite($h,$c)) return false;
fclose($h);
return true;
}
/**
* Function read.
* This function reads the cache file if it exists.
* @return contents of the cache file. if not exists or on failure, returns FALSE.
*/
public function read($pKey) {
if(!file_exists($this->sCacheDir.'/'.md5($pKey).'.cache') || filesize($this->sCacheDir.'/'.md5($pKey).'.cache') <= 0) return false;
$c = self::readIndex();
if (!isset($c[md5($pKey)])) return false;
if ($c[md5($pKey)]['expire_time'] < time()) {
$h = fopen($this->sCacheDir.'/'.md5($pKey).'.cache','r');
$content = fread($h, filesize($this->sCacheDir.'/'.md5($pKey).'.cache'));
fclose($h);
return ($c[md5($pKey)]['scalar'])? $content : unserialize($content);
}
return false;
}
}
?>
class CacheController {
private $sCacheDir;
private $sCacheIndex;
public function __construct($pDir = null) {
$this->sCacheDir = ($pDir == null) ? './cache' : $pDir;
$this->sCacheIndex = $this->sCacheDir.'/cache.index';
}
/**
* Function readIndex.
* This function read the index file. The function unserialize it and return the Array.
* If there is no index file he return a blank Array
*/
private function readIndex() {
if(file_exists($this->sCacheIndex) && filesize($this->sCacheIndex) > 0) {
$h=fopen($this->sCacheIndex,'r');
$c=fread($h, filesize($this->sCacheIndex));
fclose($h);
$s = (strlen($c)>0)? unserialize($c) : array();
return $s;
}
return array();
}
/**
* Function set.
* This function make the cache file.
* The expire time is in seconds. if you want 1 hour, than you make param $pTime 3600.
* @return boolean TRUE on success, or FALSE on error
*/
public function set($pVar, $pId, $pTime, $pCategory = null) {
$c = self::readIndex();
$scalar = is_scalar($pVar);
$c[md5($pId)] = array(
'scalar'=>$scalar,
'category'=>$pCategory,
'expire_time'=>time() + $pTime
);
// make the cache file
$cache_content = $scalar ? $pVar : serialize($pVar);
$h=fopen($this->sCacheDir.'/'.md5($pId).'.cache','w');
if(!fwrite($h,$cache_content)) return false;
fclose($h);
//build or make the index file.
$c=serialize($c);
$h=fopen($this->sCacheIndex, 'w');
if(!fwrite($h,$c)) return false;
fclose($h);
return true;
}
/**
* Function read.
* This function reads the cache file if it exists.
* @return contents of the cache file. if not exists or on failure, returns FALSE.
*/
public function read($pKey) {
if(!file_exists($this->sCacheDir.'/'.md5($pKey).'.cache') || filesize($this->sCacheDir.'/'.md5($pKey).'.cache') <= 0) return false;
$c = self::readIndex();
if (!isset($c[md5($pKey)])) return false;
if ($c[md5($pKey)]['expire_time'] < time()) {
$h = fopen($this->sCacheDir.'/'.md5($pKey).'.cache','r');
$content = fread($h, filesize($this->sCacheDir.'/'.md5($pKey).'.cache'));
fclose($h);
return ($c[md5($pKey)]['scalar'])? $content : unserialize($content);
}
return false;
}
}
?>
GarbageCollector:
Code (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
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
<?php
class GarbageCollector{
private $sCacheDir;
private $sCacheIndex;
public function __construct($pDir = null) {
$this->sCacheDir = ($pDir == null) ? './cache' : $pDir;
$this->sCacheIndex = $this->sCacheDir.'/cache.index';
}
/**
* Function readIndex.
* This function read the index file. The function unserialize it and return the Array.
* If there is no index file he return a blank Array
*/
private function readIndex() {
if(file_exists($this->sCacheIndex) && filesize($this->sCacheIndex) > 0) {
$h=fopen($this->sCacheIndex,'r');
$c=fread($h, filesize($this->sCacheIndex));
fclose($h);
$s = (strlen($c)>0)? unserialize($c) : array();
return $s;
}
return array();
}
/**
* Function clearAllCache.
* **WARNING** This function remove all the cache files in the directory.
* @return boolean TRUE on success, or FALSE on error
*/
public function clearAllCache() {
$dirCache = opendir($this->sCacheDir);
while(false !== ($file = readdir($dirCache))) {
if($file != "." && $file != ".." && $this->sCacheDir.'/'.$file != $this->sCacheIndex) {
chmod($this->sCacheDir.'/'.$file, 0777);
if(!is_dir($this->sCacheDir.'/'.$file)) {
if(!unlink($this->sCacheDir.'/'.$file)) return false;
}
}
}
closedir($dirCache);
$h = fopen($this->sCacheIndex, 'w');
if(!fwrite($h,'')) return false;
fclose($h);
return true;
}
/**
* Function clearCategory.
* This function remove all the cache files with a specific category.
* @return boolean TRUE on success, or FALSE on error
*/
public function clearCategory($pCategory) {
$c = parent::readIndex();
foreach($c as $key=>$value) {
if ($value['category'] == $pCategory) {
if(!unlink($this->sCacheDir.'/'.$key.'.cache')) return false;
unset($c[$key]);
}
}
$c=serialize($c);
$h = fopen($this->sCacheIndex, 'w');
if(!fwrite($h,$c)) return false;
fclose($h);
return true;
}
/**
* Function clearOverTime.
* This function remove all the cache files that are over time.
* @return boolean TRUE on success, or FALSE on error
*/
public function clearOverTime() {
$c = parent::readIndex();
foreach($c as $key=>$value) {
if ($value['expire_time'] < time()) {
if(!unlink($this->sCacheDir.'/'.$key.'.cache')) return false;
unset($c[$key]);
}
}
$c=serialize($c);
$h = fopen($this->sCacheIndex, 'w');
if(!fwrite($h,$c)) return false;
fclose($h);
return true;
}
}
?>
class GarbageCollector{
private $sCacheDir;
private $sCacheIndex;
public function __construct($pDir = null) {
$this->sCacheDir = ($pDir == null) ? './cache' : $pDir;
$this->sCacheIndex = $this->sCacheDir.'/cache.index';
}
/**
* Function readIndex.
* This function read the index file. The function unserialize it and return the Array.
* If there is no index file he return a blank Array
*/
private function readIndex() {
if(file_exists($this->sCacheIndex) && filesize($this->sCacheIndex) > 0) {
$h=fopen($this->sCacheIndex,'r');
$c=fread($h, filesize($this->sCacheIndex));
fclose($h);
$s = (strlen($c)>0)? unserialize($c) : array();
return $s;
}
return array();
}
/**
* Function clearAllCache.
* **WARNING** This function remove all the cache files in the directory.
* @return boolean TRUE on success, or FALSE on error
*/
public function clearAllCache() {
$dirCache = opendir($this->sCacheDir);
while(false !== ($file = readdir($dirCache))) {
if($file != "." && $file != ".." && $this->sCacheDir.'/'.$file != $this->sCacheIndex) {
chmod($this->sCacheDir.'/'.$file, 0777);
if(!is_dir($this->sCacheDir.'/'.$file)) {
if(!unlink($this->sCacheDir.'/'.$file)) return false;
}
}
}
closedir($dirCache);
$h = fopen($this->sCacheIndex, 'w');
if(!fwrite($h,'')) return false;
fclose($h);
return true;
}
/**
* Function clearCategory.
* This function remove all the cache files with a specific category.
* @return boolean TRUE on success, or FALSE on error
*/
public function clearCategory($pCategory) {
$c = parent::readIndex();
foreach($c as $key=>$value) {
if ($value['category'] == $pCategory) {
if(!unlink($this->sCacheDir.'/'.$key.'.cache')) return false;
unset($c[$key]);
}
}
$c=serialize($c);
$h = fopen($this->sCacheIndex, 'w');
if(!fwrite($h,$c)) return false;
fclose($h);
return true;
}
/**
* Function clearOverTime.
* This function remove all the cache files that are over time.
* @return boolean TRUE on success, or FALSE on error
*/
public function clearOverTime() {
$c = parent::readIndex();
foreach($c as $key=>$value) {
if ($value['expire_time'] < time()) {
if(!unlink($this->sCacheDir.'/'.$key.'.cache')) return false;
unset($c[$key]);
}
}
$c=serialize($c);
$h = fopen($this->sCacheIndex, 'w');
if(!fwrite($h,$c)) return false;
fclose($h);
return true;
}
}
?>
De cacheContoller zorgt voor het aanmaken/updaten en lezen van de cache files.
De GabageCollector zorgt ervoor dat bepaalde cache files ook echt verwijderd worden van de server.
alvast bedankt.
Groetjes,
Roy
Er zijn nog geen reacties op dit bericht.