/
Output.php
95 lines (85 loc) · 2.86 KB
/
Output.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
<?php
namespace mcanan\framework;
class Output
{
private $escribirCache=false;
private $archivo="";
private $html="";
public function setHtml($html)
{
$this->html = $html;
}
public function hasContent()
{
return ($this->html!="");
}
private function put($filename, $buffer)
{
$cachedir = './app/cache/';
$tempfilename = $cachedir.$filename.getmypid();
if (($fp = fopen($tempfilename, "w")) == false) {
return false;
}
fwrite($fp, $buffer);
fclose($fp);
rename($tempfilename, $cachedir.$filename);
return true;
}
private function get($filename, $expiration = false)
{
// $expiration es en segundos.
$cachedir = './app/cache/';
if ($expiration) {
$stat = @stat($cachedir.$filename);
if ($stat[9]) {
$time = time();
$benchmark =& getBenchmarkInstance();
$benchmark->mark("cache_age: ".($time-$stat[9]));
if ($time > $stat[9] + $expiration) {
@unlink($cachedir.$filename);
return false;
}
}
}
return @file_get_contents($cachedir.$filename);
}
public function display()
{
$benchmark =& getBenchmarkInstance();
$benchmark->mark("display_start");
if (!headers_sent()) {
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: text/html; charset=utf-8');
}
if ($this->escribirCache) {
$this->put($this->archivo, $this->html);
}
$benchmark->mark("display_end");
echo str_replace("%BENCHMARK%", $benchmark->getTimestampsAsHtmlComment(), $this->html);
}
public function displayFromCache($archivo, $tiempo)
{
$benchmark =& getBenchmarkInstance();
$benchmark->mark("displayFromCache_start");
$retorno=false;
$this->archivo = $archivo;
if ($html=$this->get($archivo, $tiempo)) {
if (!headers_sent()) {
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: text/html; charset=utf-8');
}
$benchmark->mark("displayFromCache_end");
echo str_replace("%BENCHMARK%", $benchmark->getTimestampsAsHtmlComment(), $html);
$retorno=true;
} else {
$benchmark->mark("cache old");
$this->escribirCache = true;
}
$benchmark->mark("displayFromCache_end");
return $retorno;
}
}