Showing
2 changed files
with
134 additions
and
1 deletions
@@ -3,10 +3,25 @@ | @@ -3,10 +3,25 @@ | ||
3 | require_once dirname(__FILE__) . '/../config.php'; | 3 | require_once dirname(__FILE__) . '/../config.php'; |
4 | require_once LIBDIR . 'initLocale.php'; | 4 | require_once LIBDIR . 'initLocale.php'; |
5 | require_once LIBDIR . 'c_personAdmin.php'; | 5 | require_once LIBDIR . 'c_personAdmin.php'; |
6 | +require_once LIBDIR . 'c_cache.php'; | ||
6 | 7 | ||
8 | +$cache = new c_cache (array ('a', 'b')); | ||
7 | $personAdmin = new c_personAdmin ($locale); | 9 | $personAdmin = new c_personAdmin ($locale); |
8 | 10 | ||
9 | ob_start ('ob_gzhandler'); | 11 | ob_start ('ob_gzhandler'); |
10 | -$personAdmin->show (); | 12 | + |
13 | +if (! $cache->check (60)) | ||
14 | +{ | ||
15 | + sleep (10); | ||
16 | + $personAdmin->show (); | ||
17 | + | ||
18 | + $cache->update (); | ||
19 | +} | ||
20 | +else | ||
21 | +{ | ||
22 | + print "<p>[kein update]<p>"; | ||
23 | +} | ||
24 | + | ||
25 | +ob_flush (); | ||
11 | 26 | ||
12 | ?> | 27 | ?> |
libs/c_cache.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +require_once LIBDIR . 'mutualExclusion.php'; | ||
4 | + | ||
5 | +class c_cache | ||
6 | +{ | ||
7 | + private $lock; | ||
8 | + private $cacheFile; | ||
9 | + | ||
10 | + // generiere den Cache Filename aus dem | ||
11 | + // 1. script Namen | ||
12 | + // 2. einem Hash ueber alle verwendeten: | ||
13 | + // - request parameter | ||
14 | + // - Werte in GLOBALS | ||
15 | + // - Sessionvariablen | ||
16 | + // ausserdem wird das cache Verzeichnis auch gleich angelegt wenn es noch | ||
17 | + // nicht existiert. | ||
18 | + function __construct ($params=NULL, $globals=NULL, $session=NULL) | ||
19 | + { | ||
20 | + $cacheDir = CACHEDIR . substr ($_SERVER['PHP_SELF'], 1); | ||
21 | + $cacheDirArr = explode ('/', $cacheDir); | ||
22 | + $cacheParms = array (); | ||
23 | + $cacheGlobals = array (); | ||
24 | + $cacheSession = array (); | ||
25 | + $cachaAllParm = array (); | ||
26 | + $parmStr = ''; | ||
27 | + | ||
28 | + // cache Verzeichnis anlegen. | ||
29 | + $_dir = ''; | ||
30 | + foreach ($cacheDirArr as $dir) | ||
31 | + if (! is_dir ($_dir .= '/' . $dir) && $_dir !== '') | ||
32 | + mkdir ($_dir, 0755); | ||
33 | + | ||
34 | + unset ($_dir, $dir, $cacheDirArr); | ||
35 | + | ||
36 | + // alle verwendeten req parameter holen | ||
37 | + if ($params !== NULL) | ||
38 | + foreach ($params as $parm) | ||
39 | + if (isset ($_REQUEST[$parm])) | ||
40 | + $cacheParms[$parm] = $_REQUEST[$parm]; | ||
41 | + else | ||
42 | + $cacheParms[$parm] = ''; | ||
43 | + ksort ($cacheParms); | ||
44 | + | ||
45 | + // alle verwendeten globals holen | ||
46 | + if ($globals !== NULL) | ||
47 | + foreach ($globals as $glob) | ||
48 | + if (isset ($GLOBALS[$glob])) | ||
49 | + $cacheGlobals[$glob] = $GLOBALS[$glob]; | ||
50 | + else | ||
51 | + $cacheGlobals[$glob] = ''; | ||
52 | + ksort ($cacheGlobals); | ||
53 | + | ||
54 | + // alle verwendeten session variablen holen | ||
55 | + if ($session !== NULL) | ||
56 | + foreach ($session as $sess) | ||
57 | + if (isset ($SESSION[$sess])) | ||
58 | + $cacheSession[$sess] = $SESSION[$sess]; | ||
59 | + else | ||
60 | + $cacheSession[$sess] = ''; | ||
61 | + ksort ($cacheSession); | ||
62 | + | ||
63 | + // und nu aus all denen einen md5 hash generieren. | ||
64 | + $cacheAllParm = array_merge ($cacheParms, $cacheGlobals, $cacheSession); | ||
65 | + | ||
66 | + unset ($cacheParms, $cacheGlobals, $cacheSession); | ||
67 | + | ||
68 | + foreach ($cacheAllParm as $name => $value) | ||
69 | + $parmStr .= '_' . $name . '=' . $value; | ||
70 | + | ||
71 | + $parmStr = md5 ($parmStr); | ||
72 | + | ||
73 | + $this->lock = NULL; | ||
74 | + $this->cacheFile = $cacheDir . '/cache_' . $parmStr . '.html'; | ||
75 | + } | ||
76 | + | ||
77 | + | ||
78 | + | ||
79 | + function check ($interval=0) | ||
80 | + { | ||
81 | + ob_start (); | ||
82 | + | ||
83 | + clearstatcache (); | ||
84 | + if (file_exists ($this->cacheFile) && | ||
85 | + time () - filemtime ($this->cacheFile) < $interval) | ||
86 | + { | ||
87 | + readfile ($this->cacheFile); | ||
88 | + return TRUE; | ||
89 | + } | ||
90 | + else | ||
91 | + { | ||
92 | + $this->lock = acquireLock ($this->cacheFile, '1'); | ||
93 | + | ||
94 | + // inszwischen koennte der cache von einem anderen aufruf | ||
95 | + // aktualisiert worden sein, also nochmal checken. | ||
96 | + clearstatcache (); | ||
97 | + if (file_exists ($this->cacheFile) && | ||
98 | + time () - filemtime ($this->cacheFile) < $interval) | ||
99 | + { | ||
100 | + releaseLock ($this->lock); | ||
101 | + readfile ($this->cacheFile); | ||
102 | + return TRUE; | ||
103 | + } | ||
104 | + | ||
105 | + return FALSE; | ||
106 | + } | ||
107 | + } | ||
108 | + | ||
109 | + function update () | ||
110 | + { | ||
111 | + $page = ob_get_flush (); | ||
112 | + file_put_contents ($this->cacheFile, $page); | ||
113 | + | ||
114 | + releaseLock ($this->lock); | ||
115 | + } | ||
116 | +}; | ||
117 | + | ||
118 | +?> |
Please
register
or
login
to post a comment