Changeset 1109
- Timestamp:
- 12/04/07 16:28:10 (10 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/websites/cache/stubAbstractWebsiteCache.php (added)
- trunk/src/main/php/net/stubbles/websites/cache/stubDefaultWebsiteCache.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/websites/cache/stubGzipWebsiteCache.php (added)
- trunk/src/main/php/net/stubbles/websites/cache/stubWebsiteCache.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/websites/cache/stubDefaultWebsiteCache.php
r1099 r1109 1 1 <?php 2 2 /** 3 * Cachefor websites.3 * Default cache implementation for websites. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage websites 7 * @subpackage websites_cache 8 8 */ 9 stubClassLoader::load('net.stubbles.lang.stubMode', 10 'net.stubbles.util.cache.stubCache', 11 'net.stubbles.util.log.log', 12 'net.stubbles.websites.cache.stubWebsiteCache' 13 ); 9 stubClassLoader::load('net.stubbles.websites.cache.stubAbstractWebsiteCache'); 14 10 /** 15 * Cachefor websites.11 * Default cache implementation for websites. 16 12 * 17 13 * @package stubbles 18 * @subpackage websites 14 * @subpackage websites_cache 19 15 */ 20 class stubDefaultWebsiteCache extends stub BaseObject implements stubWebsiteCache16 class stubDefaultWebsiteCache extends stubAbstractWebsiteCache 21 17 { 22 18 /** 23 * reason why cache is missing 24 * 25 * @var string 26 */ 27 protected $missReason = ''; 28 /** 29 * list of keys for the cache 19 * list of variables for the cache 30 20 * 31 21 * @var array<string,scalar> 32 22 */ 33 protected $cache Keys = array();23 protected $cacheVars = array(); 34 24 /** 35 25 * list of files used when generating the page … … 43 33 * @var bool 44 34 */ 45 protected $checkFiles = true; 46 /** 47 * the real cache 48 * 49 * @var stubCacheContainer 50 */ 51 protected $cache; 35 protected $checkFiles = false; 52 36 53 37 /** … … 72 56 73 57 /** 74 * adds a key to the list of cache keys58 * adds a variable to the list of cache variables 75 59 * 76 * @param string $ key60 * @param string $name 77 61 * @param scalar $value 78 62 */ 79 public function addCache Key($key, $value)63 public function addCacheVar($name, $value) 80 64 { 81 $this->cacheKeys[$key] = $value; 65 $this->cacheVars[$name] = $value; 66 } 67 68 /** 69 * adds a list of variables to the list of cache variables 70 * 71 * @param array<string,scalar> 72 */ 73 public function addCacheVars(array $cacheVars) 74 { 75 foreach ($cacheVars as $name => $value) { 76 $this->cacheVars[$name] = $value; 77 } 82 78 } 83 79 … … 93 89 94 90 /** 95 * retrieves data from cache and puts it into response91 * adds a list of files to the list of used files 96 92 * 93 * @param array<string> $file 94 */ 95 public function addUsedFiles(array $files) 96 { 97 foreach ($files as $file) { 98 $this->usedFiles[$file] = $file; 99 } 100 } 101 102 /** 103 * does the real retrieve 104 * 105 * @param stubRequest $request 97 106 * @param stubResponse $response 98 * @param string $ page name of the page to be cached99 * @return bool true if data was retrieved from cache, else false107 * @param string $cacheKey 108 * @return bool true if successfully retrieved, else false 100 109 */ 101 p ublic function retrieve(stubResponse $response, $page)110 protected function doRetrieve(stubRequest $request, stubResponse $response, $cacheKey) 102 111 { 103 $cacheKey = $this->generateCacheKey($page);104 if ($this->isCached($cacheKey) === false) {105 $this->log($cacheKey, 'miss');106 return false;107 }108 109 if (stubMode::$CURRENT->name() !== 'PROD') {110 $response->addHeader('X-Cached', $this->getClassName());111 }112 113 112 $response->write($this->cache->get($cacheKey)); 114 $this->log($cacheKey, 'hit');115 113 return true; 116 114 } 117 115 118 116 /** 119 * stores the data from the response in cche117 * does the real storage 120 118 * 121 * @param stubResponse $response 122 * @param string $page name of the page to be cached 119 * @param stubRequest $request 120 * @param stubResponse $response 121 * @param string $cacheKey 122 * @return bool true if successfully stored, else false 123 123 */ 124 p ublic function store(stubResponse $response, $page)124 protected function doStore(stubRequest $request, stubResponse $response, $cacheKey) 125 125 { 126 if (stubMode::$CURRENT->isCacheEnabled() === false) { 127 return; 128 } 129 130 $cacheKey = $this->generateCacheKey($page); 131 $this->cache->put($cacheKey, $response->getData()); 126 return (bool) $this->cache->put($cacheKey, $response->getData()); 132 127 } 133 128 134 129 /** 135 * checks whether data is cached or not130 * returns true if used files check is enabled 136 131 * 137 132 * @return bool 138 133 */ 139 protected function isCached($cacheKey)134 protected abstract function isUsedFilesCheckEnabled() 140 135 { 141 if (stubMode::$CURRENT->isCacheEnabled() === false) { 142 $this->missReason = 'disabled'; 143 return false; 144 } 145 146 if ($this->cache->has($cacheKey) === false) { 147 $this->missReason = 'no cache file'; 148 return false; 149 } 150 151 if (false === $this->checkFiles) { 152 return true; 153 } 154 155 $cacheTime = $this->cache->getStoreTime($cacheKey); 156 clearstatcache(); 157 foreach ($this->getUsedFiles as $fileName) { 158 if (is_readable($filename) === false) { 159 continue; 160 } 161 162 if (filemtime($fileName) > $cacheTime) { 163 $this->missReason = $fileName . ' is newer'; 164 return false; 165 } 166 } 167 168 return true; 136 return $this->checkFiles; 169 137 } 170 138 171 139 /** 172 * helper method to log cache acticity140 * returns the list of used files 173 141 * 174 * @param string $cacheKey key for cache data 175 * @param string $type 'hit' or 'miss' 142 * @return array<string> 176 143 */ 177 protected function log($cacheKey, $type)144 protected function getUsedFiles() 178 145 { 179 $logData = stubLogDataFactory::create('cache', stubLogger::LEVEL_INFO); 180 $logData->addData($this->page); 181 $logData->addData($type); 182 $logData->addData(compression); 183 $logData->addData($this->missReason); 184 $logData->addData($cacheKey); 185 stubLogger::logToAll($logData); 146 return $this->usedFiles; 186 147 } 187 148 188 149 /** 189 * generates the cache key from given list of cache keys150 * returns the list of cache variables 190 151 * 191 * @param string $page name of the page to be cached 192 * @return string 152 * @return array<string,scalar> 193 153 */ 194 protected function ge nerateCacheKey($page)154 protected function getCacheVars() 195 155 { 196 $baseKey = $page . '?'; 197 foreach ($this->cacheKeys as $key => $value) { 198 $baseKey .= '&' . $key . '=' . $value; 199 } 200 201 return md5($baseKey); 156 return $this->cacheVars; 202 157 } 203 158 } trunk/src/main/php/net/stubbles/websites/cache/stubWebsiteCache.php
r1099 r1109 5 5 * @author Frank Kleine <mikey@stubbles.net> 6 6 * @package stubbles 7 * @subpackage websites 7 * @subpackage websites_cache 8 8 */ 9 stubClassLoader::load('net.stubbles.ipo.response.stubResponse'); 9 stubClassLoader::load('net.stubbles.ipo.request.stubRequest', 10 'net.stubbles.ipo.response.stubResponse' 11 ); 10 12 /** 11 13 * Cache for websites. 12 14 * 13 15 * @package stubbles 14 * @subpackage websites 16 * @subpackage websites_cache 15 17 */ 16 18 interface stubWebsiteCache extends stubObject 17 19 { 20 /** 21 * cache hit 22 */ 23 const HIT = 'hit'; 24 /** 25 * cache miss 26 */ 27 const MISS = 'miss'; 18 28 19 29 /** … … 25 35 26 36 /** 27 * adds a key to the list of cache keys37 * adds a variable to the list of cache variables 28 38 * 29 * @param string $ key39 * @param string $name 30 40 * @param scalar $value 31 41 */ 32 public function addCache Key($key, $value);42 public function addCacheVar($name, $value); 33 43 34 44 /** 35 * list of used files 45 * adds a list of variables to the list of cache variables 46 * 47 * @param array<string,scalar> 48 */ 49 public function addCacheVars(array $cacheVars); 50 51 /** 52 * adds a file to the list of used files 36 53 * 37 54 * @param string $file … … 40 57 41 58 /** 59 * adds a list of files to the list of used files 60 * 61 * @param array<string> $file 62 */ 63 public function addUsedFiles(array $files); 64 65 /** 66 * returns the cache container used by the implementation 67 * 68 * @return stubCacheContainer 69 */ 70 public function getCacheContainer(); 71 72 /** 42 73 * retrieves data from cache and puts it into response 43 74 * 75 * @param stubRequest $request 44 76 * @param stubResponse $response 45 77 * @param string $page name of the page to be cached 46 78 * @return bool true if data was retrieved from cache, else false 47 79 */ 48 public function retrieve(stubRe sponse $response, $page);80 public function retrieve(stubRequest $request, stubResponse $response, $page); 49 81 50 82 /** 51 83 * stores the data from the response in cche 52 84 * 53 * @param stubResponse $response 54 * @param string $page name of the page to be cached 85 * @param stubRequest $request 86 * @param stubResponse $response 87 * @param string $page name of the page to be cached 88 * @return bool true if successfully stored, else false 55 89 */ 56 public function store(stubRe sponse $response, $page);90 public function store(stubRequest $request, stubResponse $response, $page); 57 91 } 58 92 ?>
