php-cachingsclasse
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
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
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
<?php
class cache
{
// door Wim Mariƫn
// http://www.gdx.be/
// VARS /
// directory with .cache files
private $dir = './cache/';
private $filename = NULL;
protected $caching = false;
// __construct /
// PUBLIC (duh!)
// cache::__construct()
// AUTOMATIC
public function __construct ($dir=NULL)
{
if(isset($dir))
$this->dir = $dir;
if(substr($this->dir, -1) != '/')
$this->dir .= '/';
}
// __destruct /
// PUBLIC (duh!)
// cache::__destruct()
// AUTOMATIC
public function __destruct ()
{
$this->dir = NULL;
}
// start /
// PUBLIC
// cache::start([string] filename)
public function start ($filename)
{
$this->filename = $filename;
if(isset($filename) && !is_file($this->dir.$filename.'.cache'))
{
ob_start();
$this->caching = true;
}
else
{
echo $this->readCache($filename);
exit();
}
}
// write /
// PUBLIC
// cache::write ([string] filename)
public function write ($file='')
{
if(empty($file) && !empty($this->filename))
$file = $this->filename;
elseif(empty($file) && empty($this->filename))
return false;
if($this->caching)
{
$content = ob_get_contents();
ob_end_clean();
$this->updateCache($file, $content);
return $content;
}
}
// updateCache /
// PRIVATE
// cache::updateCache ([string] filename, [string] content)
// write a .cache file (by update)
private function updateCache ($file, $content)
{
$cachefile = fopen($this->dir.$file.'.cache', 'w');
fwrite($cachefile, $content);
fclose($cachefile);
return true;
}
// readCache /
// PUBLIC
// cache::readCache([string] filename)
// read a .cache file
public function readCache($file)
{
$cachefile = fopen($this->dir.$file.'.cache', "r");
while(!feof($cachefile))
{
$content .= fgets($cachefile, 4096);
}
fclose($cachefile);
return $content;
}
// deleteCache
// PUBLIC
// cache::deleteCache([string] filename)
// delete a .cache file
public function deleteCache($file)
{
if(unlink($this->dir.$file.'.cache'))
return true;
else
return false;
}
}
?>
class cache
{
// door Wim Mariƫn
// http://www.gdx.be/
// VARS /
// directory with .cache files
private $dir = './cache/';
private $filename = NULL;
protected $caching = false;
// __construct /
// PUBLIC (duh!)
// cache::__construct()
// AUTOMATIC
public function __construct ($dir=NULL)
{
if(isset($dir))
$this->dir = $dir;
if(substr($this->dir, -1) != '/')
$this->dir .= '/';
}
// __destruct /
// PUBLIC (duh!)
// cache::__destruct()
// AUTOMATIC
public function __destruct ()
{
$this->dir = NULL;
}
// start /
// PUBLIC
// cache::start([string] filename)
public function start ($filename)
{
$this->filename = $filename;
if(isset($filename) && !is_file($this->dir.$filename.'.cache'))
{
ob_start();
$this->caching = true;
}
else
{
echo $this->readCache($filename);
exit();
}
}
// write /
// PUBLIC
// cache::write ([string] filename)
public function write ($file='')
{
if(empty($file) && !empty($this->filename))
$file = $this->filename;
elseif(empty($file) && empty($this->filename))
return false;
if($this->caching)
{
$content = ob_get_contents();
ob_end_clean();
$this->updateCache($file, $content);
return $content;
}
}
// updateCache /
// PRIVATE
// cache::updateCache ([string] filename, [string] content)
// write a .cache file (by update)
private function updateCache ($file, $content)
{
$cachefile = fopen($this->dir.$file.'.cache', 'w');
fwrite($cachefile, $content);
fclose($cachefile);
return true;
}
// readCache /
// PUBLIC
// cache::readCache([string] filename)
// read a .cache file
public function readCache($file)
{
$cachefile = fopen($this->dir.$file.'.cache', "r");
while(!feof($cachefile))
{
$content .= fgets($cachefile, 4096);
}
fclose($cachefile);
return $content;
}
// deleteCache
// PUBLIC
// cache::deleteCache([string] filename)
// delete a .cache file
public function deleteCache($file)
{
if(unlink($this->dir.$file.'.cache'))
return true;
else
return false;
}
}
?>