Minahito
minah****@users*****
2006年 9月 24日 (日) 23:05:19 JST
Index: xoops2jp/html/modules/base/kernel/Legacy_CacheInformation.class.php diff -u /dev/null xoops2jp/html/modules/base/kernel/Legacy_CacheInformation.class.php:1.1.2.1 --- /dev/null Sun Sep 24 23:05:19 2006 +++ xoops2jp/html/modules/base/kernel/Legacy_CacheInformation.class.php Sun Sep 24 23:05:19 2006 @@ -0,0 +1,222 @@ +<?php +/** + * @package Legacy + * @version Id$ + */ + +if (!defined('XOOPS_ROOT_PATH')) exit(); + +/** + * The structure which have a policy and an information of a module, which + * Legacy_Controller must know. In the later version, this class may be + * replaced with just array. + * + * For a performance, this class has reset() to reuse a object. + */ +class Legacy_AbstractCacheInformation +{ + /** + * Array of uid. This is an information for cache store program to generate + * an unique file name. Uid isn't must. Sets identity data. + * + * @access public + * @var Array of uid + */ + var $mIdentityArr = array(); + + /** + * Array of groupid. This is an information for cache store program to + * generate an unique file name. + * + * @access public + * @var Array of groupid + */ + var $mGroupArr = array(); + + /** + * Boolean flag indicating whether this object asks caching to the + * controller. + * + * @access private + * @var bool + */ + var $_mEnableCache = null; + + function Legacy_AbstractCacheInformation() + { + } + + /** + * Gets a value indicating whether someone has tried to set a flag to this + * object. + * @return bool + */ + function hasSetEnable() + { + return $this->_mEnableCache !== false; + } + + /** + * Sets a flag indicating whether this object decides executing cache. + * @param bool $flag + */ + function setEnableCache($flag) + { + $this->_mEnableCache = $flag; + } + + /** + * Gets a flag indicating whether this object decides executing cache. + * @return bool + */ + function isEnableCache() + { + return $this->_mEnableCache; + } + + /** + * Gets a cache file path. + * @return string full file path to store a cache file. + */ + function getFilePath() + { + return XOOPS_CACHE_PATH . "/" . $this->getCacheId() . ".cache.html"; + } + + /** + * [Abstract] Gets an identity token string. + * @return string + */ + function getCacheId() + { + } + + /** + * Resets member properties to reuse this object. + */ + function reset() + { + $this->mIdentityArr = array(); + $this->mGroupArr = array(); + $this->_mEnableCache = null; + } + + /** + * Saves a buffer of the render target to a cache file. + * @param XCube_RenderTarget $renderTarget + */ + function save(&$renderTarget) + { + $filepath = $this->getFilePath(); + $fp = fopen($filepath, "wb"); + if ($fp) { + fwrite($fp, $renderTarget->getResult()); + fclose($fp); + } + } + + /** + * Loads a contents of a cache file, and return it. + * @return string + */ + function load() + { + return file_get_contents($this->getFilePath()); + } + + /** + * Gets a value indicating whether a cache file exists. + */ + function isExistCache() + { + return file_exists($this->getFilePath()); + } + + /** + * Gets a value indicating wheter a cache file keeps life time. + * @param int $cachetime + */ + function isExpireCache($cachetime) + { + if ($cachetime == 0 || !$this->isExistCache()) { + return false; + } + + return ((time() - filemtime($this->getFilePath())) <= $cachetime ); + } +} + +class Legacy_ModuleCacheInformation extends Legacy_AbstractCacheInformation +{ + /** + * [READ ONLY] Xoops Module Object. + * + * @access protected + * @var XoopsModule + */ + var $_mModule = null; + + /** + * The current URL used as a base for a cache file name. This should be + * modified by modules to not make extra cache files. + * + * @access public + * @var string + */ + var $mURL = null; + + /** + * Sets a module object. + * @param XoopsModule $module + */ + function setModule(&$module) + { + $this->_mModule =& $module; + } + + function getCacheId() + { + return md5(XOOPS_SALT . $this->mURL . implode("_", $this->mGroupArr)); + } + + function reset() + { + parent::reset(); + $this->_mModule = null; + $this->mURL = null; + } +} + +class Legacy_BlockCacheInformation extends Legacy_AbstractCacheInformation +{ + /** + * [READ ONLY] Xoops Block Object. + * + * @access protected + * @var XoopsBlock + */ + var $_mBlock = null; + + /** + * Sets a block object. + * + * @param XoopsBlock $block + */ + function setBlock(&$block) + { + $this->_mBlock =& $block; + } + + function getCacheId() + { + return md5(XOOPS_SALT . 'block' . $this->_mBlock->get('bid') . implode("_", $this->mGroupArr)); + } + + function reset() + { + parent::reset(); + $this->_mBlock = null; + } +} + +?> \ No newline at end of file