From e39771330fe23f73012d528e2ef7de8f6424357c Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 15 Oct 2007 15:22:49 +0000 Subject: [PATCH] cache eingebaut --- components/personAdmin.php | 17 +++++- libs/c_cache.php | 118 +++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 libs/c_cache.php diff --git a/components/personAdmin.php b/components/personAdmin.php index e976317..fbc41dd 100644 --- a/components/personAdmin.php +++ b/components/personAdmin.php @@ -3,10 +3,25 @@ require_once dirname(__FILE__) . '/../config.php'; require_once LIBDIR . 'initLocale.php'; require_once LIBDIR . 'c_personAdmin.php'; +require_once LIBDIR . 'c_cache.php'; +$cache = new c_cache (array ('a', 'b')); $personAdmin = new c_personAdmin ($locale); ob_start ('ob_gzhandler'); -$personAdmin->show (); + +if (! $cache->check (60)) +{ + sleep (10); + $personAdmin->show (); + + $cache->update (); +} +else +{ + print "

[kein update]

"; +} + +ob_flush (); ?> diff --git a/libs/c_cache.php b/libs/c_cache.php new file mode 100644 index 0000000..17dcb96 --- /dev/null +++ b/libs/c_cache.php @@ -0,0 +1,118 @@ + $value) + $parmStr .= '_' . $name . '=' . $value; + + $parmStr = md5 ($parmStr); + + $this->lock = NULL; + $this->cacheFile = $cacheDir . '/cache_' . $parmStr . '.html'; + } + + + + function check ($interval=0) + { + ob_start (); + + clearstatcache (); + if (file_exists ($this->cacheFile) && + time () - filemtime ($this->cacheFile) < $interval) + { + readfile ($this->cacheFile); + return TRUE; + } + else + { + $this->lock = acquireLock ($this->cacheFile, '1'); + + // inszwischen koennte der cache von einem anderen aufruf + // aktualisiert worden sein, also nochmal checken. + clearstatcache (); + if (file_exists ($this->cacheFile) && + time () - filemtime ($this->cacheFile) < $interval) + { + releaseLock ($this->lock); + readfile ($this->cacheFile); + return TRUE; + } + + return FALSE; + } + } + + function update () + { + $page = ob_get_flush (); + file_put_contents ($this->cacheFile, $page); + + releaseLock ($this->lock); + } +}; + +?>