Changeset 741
- Timestamp:
- 06/21/07 11:46:08 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/util/cache/stubAbstractCacheContainer.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/util/cache/stubCacheContainer.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/util/cache/stubCacheStrategy.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/util/cache/stubDefaultCacheStrategy.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/util/cache/stubFileCacheContainer.php (modified) (7 diffs)
- trunk/src/test/php/net/stubbles/util/UtilTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/util/cache (added)
- trunk/src/test/php/net/stubbles/util/cache/stubAbstractCacheContainerTestCase.php (added)
- trunk/src/test/php/net/stubbles/util/cache/stubCacheTestCase.php (added)
- trunk/src/test/php/net/stubbles/util/cache/stubDefaultCacheStrategyTestCase.php (added)
- trunk/src/test/php/net/stubbles/util/cache/stubFileCacheContainerTestCase.php (added)
- trunk/src/test/tmp (added)
- trunk/src/test/tmp/util_cache (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/util/cache/stubAbstractCacheContainer.php
r738 r741 18 18 * the strategy used for decisions about caching 19 19 * 20 * @var stubCacheStra gey20 * @var stubCacheStrategy 21 21 */ 22 22 protected $strategy; … … 35 35 36 36 /** 37 * constructor 38 * 39 * @param string $id id of the container 40 * @param stubCacheStrategy $strategy strategy the container should use 41 */ 42 public function __construct($id, stubCacheStrategy $strategy) 43 { 44 $this->id = $id; 45 $this->strategy = $strategy; 46 } 47 48 /** 37 49 * sets the id of the container 38 50 * … … 57 69 * sets the strategy the container should use 58 70 * 59 * @param stubCacheStra gey $strategy60 */ 61 public function setStrategy(stubCacheStra gey $strategy)71 * @param stubCacheStrategy $strategy 72 */ 73 public function setStrategy(stubCacheStrategy $strategy) 62 74 { 63 75 $this->strategy = $strategy; … … 75 87 public function put($key, $data) 76 88 { 77 if ($this->strategy->isCach eable($this, $key, $data) == false) {89 if ($this->strategy->isCachable($this, $key, $data) == false) { 78 90 return false; 79 91 } trunk/src/main/php/net/stubbles/util/cache/stubCacheContainer.php
r738 r741 7 7 * @subpackage util_cache 8 8 */ 9 stubClassLoader::load('net.stubbles.util.cache.stubCacheStra gey');9 stubClassLoader::load('net.stubbles.util.cache.stubCacheStrategy'); 10 10 /** 11 11 * Interface for cache containers. … … 33 33 * sets the strategy the container should use 34 34 * 35 * @param stubCacheStra gey $strategy35 * @param stubCacheStrategy $strategy 36 36 */ 37 public function setStrategy(stubCacheStra gey $strategy);37 public function setStrategy(stubCacheStrategy $strategy); 38 38 39 39 /** … … 75 75 76 76 /** 77 * returns the unix timestamp when the data associated with $key was cached77 * returns the time in seconds how long the data associated with $key is cached 78 78 * 79 79 * @param string $key trunk/src/main/php/net/stubbles/util/cache/stubCacheStrategy.php
r738 r741 24 24 * @return bool 25 25 */ 26 public function isCach eable(stubCacheContainer $container, $key, $data);26 public function isCachable(stubCacheContainer $container, $key, $data); 27 27 28 28 /** trunk/src/main/php/net/stubbles/util/cache/stubDefaultCacheStrategy.php
r739 r741 31 31 protected $maxSize; 32 32 /** 33 * probability of a garba ce collection run33 * probability of a garbage collection run 34 34 * 35 35 * Should be a value between 0 and 100 where 0 means never and 100 means always. … … 43 43 * 44 44 * @param int $ttl time to live for single cached data 45 * @param string $maxSize maximum size of cache 45 * @param string $maxSize maximum size of cache, -1 for infinite size 46 46 * @param int $gcProbability probability of a garbace collection run 47 47 */ … … 61 61 * @return bool 62 62 */ 63 public function isCach eable(stubCacheContainer $container, $key, $data)63 public function isCachable(stubCacheContainer $container, $key, $data) 64 64 { 65 65 if (-1 == $this->maxSize) { … … 83 83 public function isExpired(stubCacheContainer $container, $key) 84 84 { 85 $cacheTime = $container->getCacheTime($key); 86 return (($cacheTime + $this->ttl) < time()); 85 return ($container->getCacheTime($key) > $this->ttl); 87 86 } 88 87 trunk/src/main/php/net/stubbles/util/cache/stubFileCacheContainer.php
r739 r741 44 44 public function setCacheDirectory($directory) 45 45 { 46 $this->cacheDirectory = $directory; 46 $this->cacheDirectory = $directory . '/' . $this->id; 47 if (file_exists($this->cacheDirectory) == false) { 48 mkdir($this->cacheDirectory, 0700, true); 49 } 47 50 } 48 51 … … 59 62 { 60 63 $bytes = file_put_contents($this->cacheDirectory . '/' . $key . '.cache', $data); 64 if (false === $bytes) { 65 return false; 66 } 67 61 68 if (null !== $this->keys) { 62 69 $this->keys[$key] = $key; … … 99 106 100 107 /** 101 * returns the unix timestamp when the data associated with $key was cached108 * returns the time in seconds how long the data associated with $key is cached 102 109 * 103 110 * @param string $key … … 106 113 public function getCacheTime($key) 107 114 { 108 return filemtime($this->cacheDirectory . '/' . $key . '.cache'); 115 if ($this->doHas($key) == true) { 116 return (time() - filemtime($this->cacheDirectory . '/' . $key . '.cache')); 117 } 118 119 return 0; 109 120 } 110 121 … … 117 128 protected function doGetSize($key) 118 129 { 130 if (null !== $this->size) { 131 return $this->size[$key]; 132 } 133 119 134 return filesize($this->cacheDirectory . '/' . $key . '.cache'); 120 135 } … … 158 173 } 159 174 175 $key = str_replace('.cache', '', $file->getFilename()); 160 176 if ($this->strategy->isExpired($this, $key) == true) { 161 177 continue; 162 178 } 163 179 164 $key = str_replace('.cache', '', $file->getFilename());165 180 $this->keys[$key] = $key; 166 181 } 182 } else { 183 foreach ($this->keys as $key) { 184 if ($this->strategy->isExpired($this, $key) == true) { 185 unset($this->keys[$key]); 186 if (null !== $this->size) { 187 unset($this->size[$key]); 188 } 189 } 190 } 167 191 } 168 192 … … 173 197 * runs the garbage collection 174 198 */ 175 protected function gc()199 protected function doGc() 176 200 { 177 201 $dirIt = new DirectoryIterator($this->cacheDirectory); trunk/src/test/php/net/stubbles/util/UtilTestSuite.php
r719 r741 25 25 $this->addTestFile($dir . '/stubRegistryTestCase.php'); 26 26 $this->addTestFile($dir . '/stubRegistryXJConfInitializerTestCase.php'); 27 28 // cache 29 $this->addTestFile($dir . '/cache/stubCacheTestCase.php'); 30 $this->addTestFile($dir . '/cache/stubDefaultCacheStrategyTestCase.php'); 31 $this->addTestFile($dir . '/cache/stubAbstractCacheContainerTestCase.php'); 32 $this->addTestFile($dir . '/cache/stubFileCacheContainerTestCase.php'); 27 33 28 34 // datespan
