aliroCache.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * aliroCache emulates the PEAR light cache, but is much less code
00038  *
00039  */
00040 
00041 class aliroSimpleCache extends aliroBasicCache {
00042     protected $group = '';
00043     protected $idencoded = '';
00044     protected $caching = true;
00045     protected $timeout = _ALIRO_HTML_CACHE_TIME_LIMIT;
00046     protected $sizelimit = _ALIRO_HTML_CACHE_SIZE_LIMIT;
00047     
00048     public function __construct ($group) {
00049         if ($group) $this->group = $group;
00050         else trigger_error ('Cannot create cache without specifying group name');
00051         parent::__construct();
00052     }
00053 
00054     protected function getGroupPath () {
00055         $grouppath = $this->basepath."html/$this->group/";
00056         if (!file_exists($grouppath)) aliroFileManager::getInstance()->createDirectory ($grouppath);
00057         return $grouppath;
00058     }
00059 
00060     protected function getCachePath ($name) {
00061         return $this->getGroupPath().$name;
00062     }
00063 
00064     public function clean () {
00065         $path = $this->getGroupPath();
00066         $dir = new aliroDirectory($path);
00067         $files = $dir->listAll();
00068         foreach ($files as $file) if ('index.html' != $file AND '.' != $file[0]) unlink($path.$file);
00069     }
00070 
00071     public function get ($id) {
00072         $this->idencoded = $this->encodeID($id);
00073         return $this->retrieve ($this->idencoded);
00074     }
00075 
00076     public function save ($data, $id=null) {
00077         if ($id) $this->idencoded = $this->encodeID($id);
00078         return $this->store ($data, $this->idencoded);
00079     }
00080     
00081     private function encodeID ($id) {
00082         return md5(serialize($id));
00083     }
00084 }
00085 
00086 class aliroCache extends aliroSimpleCache {
00087 
00088     public function __construct ($group) {
00089         $this->caching = aliroCore::getInstance()->getCfg('caching') ? true : false;
00090         parent::__construct($group);
00091     }
00092 
00093     public function call () {
00094         $arguments = func_get_args();
00095         $cached = $this->caching ? $this->get($arguments) : null;
00096         if (!$cached) {
00097             ob_start();
00098             ob_implicit_flush(false);
00099             $function = array_shift($arguments);
00100             call_user_func_array($function, $arguments);
00101             $html = ob_get_contents();
00102             ob_end_clean();
00103             $cached = new stdClass();
00104             $cached->html = $html;
00105             if ($this->caching) {
00106                 aliroRequest::getInstance()->setMetadataInCache($cached);
00107                 $cached->pathway = aliroPathway::getInstance()->getPathway();
00108                 $this->save($cached);
00109             }
00110         }
00111         else {
00112             aliroRequest::getInstance()->setMetadataFromCache($cached);
00113             aliroPathway::getInstance()->setPathway($cached->pathway);
00114         }
00115         echo $cached->html;
00116     }
00117 
00118     private function encodeID ($id) {
00119         return md5(serialize($id).aliroRequest::getInstance()->getCfg('live_site'));
00120     }
00121 
00122 }
00123 
00128 class mosCache {
00132     static function getCache ($group) {
00133         return new aliroCache ($group);
00134     }
00138     public static function cleanCache ($group=false) {
00139         if (aliroCore::get('mosConfig_caching')) {
00140             $cache = mosCache::getCache( $group );
00141             $cache->clean ($group);
00142         }
00143     }
00144 }

Generated on Thu Apr 17 13:03:26 2008 for ALIRO by  doxygen 1.5.5