Changeset 804
- Timestamp:
- 08/13/07 15:57:30 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/util/cache/stubFileCacheContainer.php
r741 r804 44 44 public function setCacheDirectory($directory) 45 45 { 46 $this->cacheDirectory = $directory . '/'. $this->id;47 if (file_exists($this->cacheDirectory) == false) {46 $this->cacheDirectory = $directory . DIRECTORY_SEPARATOR . $this->id; 47 if (file_exists($this->cacheDirectory) === false) { 48 48 mkdir($this->cacheDirectory, 0700, true); 49 49 } … … 61 61 protected function doPut($key, $data) 62 62 { 63 $bytes = file_put_contents($this->cacheDirectory . '/' . $key. '.cache', $data);63 $bytes = file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache', $data); 64 64 if (false === $bytes) { 65 65 return false; … … 85 85 protected function doHas($key) 86 86 { 87 return file_exists($this->cacheDirectory . '/' . $key. '.cache');87 return file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'); 88 88 } 89 89 … … 99 99 { 100 100 if ($this->doHas($key) == true) { 101 return file_get_contents($this->cacheDirectory . '/' . $key. '.cache');101 return file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'); 102 102 } 103 103 … … 114 114 { 115 115 if ($this->doHas($key) == true) { 116 return (time() - filemtime($this->cacheDirectory . '/' . $key. '.cache'));116 return (time() - filemtime($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache')); 117 117 } 118 118 … … 132 132 } 133 133 134 return filesize($this->cacheDirectory . '/' . $key. '.cache');134 return filesize($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'); 135 135 } 136 136 … … 151 151 152 152 $key = str_replace('.cache', '', $file->getFilename()); 153 $this->size[$key] = filesize($this->cacheDirectory . '/'. $key . '.cache');153 $this->size[$key] = filesize($this->cacheDirectory . DIRECTORY_SEPARATOR . $key . '.cache'); 154 154 } 155 155 } … … 207 207 $key = str_replace('.cache', '', $file->getFilename()); 208 208 if ($this->strategy->isExpired($this, $key) == true) { 209 unlink($this->cacheDirectory . '/'. $key . '.cache');209 unlink($this->cacheDirectory . DIRECTORY_SEPARATOR . $key . '.cache'); 210 210 if (null !== $this->size) { 211 211 unset($this->size[$key]); … … 214 214 } 215 215 } 216 217 /** 218 * escapes the cache key 219 * 220 * @param string $key 221 * @return string 222 */ 223 protected function escapeKey($key) 224 { 225 return str_replace(DIRECTORY_SEPARATOR, '', $key); 226 } 216 227 } 217 228 ?> trunk/src/test/php/net/stubbles/util/cache/stubFileCacheContainerTestCase.php
r742 r804 41 41 public function setUp() 42 42 { 43 $this->cacheDirectory = TEST_SRC_PATH . '/tmp/util_cache';43 $this->cacheDirectory = TEST_SRC_PATH . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'util_cache'; 44 44 $this->mockCacheStrategy = new MockstubCacheStrategy(); 45 45 $this->cacheContainer = new stubFileCacheContainer('foo'); 46 46 $this->cacheContainer->setStrategy($this->mockCacheStrategy); 47 47 $this->cacheContainer->setCacheDirectory($this->cacheDirectory); 48 $this->cacheDirectory .= '/foo';48 $this->cacheDirectory .= DIRECTORY_SEPARATOR . 'foo'; 49 49 } 50 50 … … 79 79 $this->assertEqual($this->cacheContainer->put('foo', 'bar'), 3); 80 80 $this->assertEqual($this->cacheContainer->put('baz', 'bar'), false); 81 $this->assertEqual(file_get_contents($this->cacheDirectory . '/foo.cache'), 'bar');82 $this->assertFalse(file_exists($this->cacheDirectory . '/baz.cache'));81 $this->assertEqual(file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache'), 'bar'); 82 $this->assertFalse(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'baz.cache')); 83 83 84 84 $this->assertEqual($this->cacheContainer->put('foo', 'baz'), 3); 85 $this->assertEqual(file_get_contents($this->cacheDirectory . '/foo.cache'), 'baz');85 $this->assertEqual(file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache'), 'baz'); 86 86 } 87 87 … … 91 91 public function testHas() 92 92 { 93 file_put_contents($this->cacheDirectory . '/foo.cache', 'bar');93 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 94 94 $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 95 95 $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); … … 105 105 public function testGet() 106 106 { 107 file_put_contents($this->cacheDirectory . '/foo.cache', 'bar');107 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 108 108 $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 109 109 $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); … … 119 119 public function testGetSize() 120 120 { 121 file_put_contents($this->cacheDirectory . '/foo.cache', 'bar');121 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 122 122 $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 123 123 $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); … … 133 133 public function testGetUsedSpace() 134 134 { 135 file_put_contents($this->cacheDirectory . '/foo.cache', 'bar');135 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 136 136 $this->assertEqual($this->cacheContainer->getUsedSpace(), 3); 137 137 $this->mockCacheStrategy->setReturnValueAt(0, 'isCachable', true); … … 145 145 public function testGetKeys() 146 146 { 147 file_put_contents($this->cacheDirectory . '/foo.cache', 'bar');147 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 148 148 $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 149 149 $this->assertEqual($this->cacheContainer->getKeys(), array('foo' => 'foo')); … … 163 163 public function testGc() 164 164 { 165 file_put_contents($this->cacheDirectory . '/foo.cache', 'bar');165 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache', 'bar'); 166 166 $this->mockCacheStrategy->setReturnValue('shouldRunGc', true); 167 167 $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 168 168 $this->cacheContainer->gc(); 169 $this->assertTrue(file_exists($this->cacheDirectory . '/foo.cache'));169 $this->assertTrue(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache')); 170 170 171 171 $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', true); 172 172 $this->cacheContainer->gc(); 173 $this->assertFalse(file_exists($this->cacheDirectory . '/foo.cache'));173 $this->assertFalse(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'foo.cache')); 174 174 $this->mockCacheStrategy->setReturnValueAt(2, 'isExpired', false); 175 175 $this->mockCacheStrategy->setReturnValueAt(3, 'isExpired', false); … … 179 179 $this->assertEqual($this->cacheContainer->getSize('foo'), 0); 180 180 } 181 182 /** 183 * test a key that contains a directory seperator 184 */ 185 public function testKeyContainingDirectorySeperator() 186 { 187 $this->mockCacheStrategy->setReturnValue('isCachable', true); 188 $this->assertEqual($this->cacheContainer->put('bar' . DIRECTORY_SEPARATOR . 'foo', 'bar'), 3); 189 $this->assertEqual(file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'barfoo.cache'), 'bar'); 190 $this->mockCacheStrategy->setReturnValueAt(0, 'isExpired', false); 191 $this->mockCacheStrategy->setReturnValueAt(1, 'isExpired', false); 192 $this->mockCacheStrategy->setReturnValueAt(2, 'isExpired', false); 193 $this->mockCacheStrategy->setReturnValueAt(3, 'isExpired', false); 194 $this->mockCacheStrategy->setReturnValueAt(4, 'isExpired', true); 195 $this->assertTrue($this->cacheContainer->has('bar' . DIRECTORY_SEPARATOR . 'foo')); 196 $this->assertEqual($this->cacheContainer->get('bar' . DIRECTORY_SEPARATOR . 'foo'), 'bar'); 197 $this->assertEqual($this->cacheContainer->getKeys(), array('barfoo' => 'barfoo')); 198 $this->assertEqual($this->cacheContainer->getSize('bar' . DIRECTORY_SEPARATOR . 'foo'), 3); 199 $this->assertEqual($this->cacheContainer->getUsedSpace(), 3); 200 $this->mockCacheStrategy->setReturnValue('shouldRunGc', true); 201 $this->cacheContainer->gc(); 202 $this->assertFalse(file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . 'barfoo.cache')); 203 } 204 205 /** 206 * test a key that contains a directory seperator 207 */ 208 public function testtestKeyContainingDirectorySeperatorAndExistingCacheFile() 209 { 210 file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . 'barfoo.cache', 'bar'); 211 $this->assertTrue($this->cacheContainer->has('bar' . DIRECTORY_SEPARATOR . 'foo')); 212 $this->assertEqual($this->cacheContainer->get('bar' . DIRECTORY_SEPARATOR . 'foo'), 'bar'); 213 $this->assertEqual($this->cacheContainer->getKeys(), array('barfoo' => 'barfoo')); 214 $this->assertEqual($this->cacheContainer->getSize('bar' . DIRECTORY_SEPARATOR . 'foo'), 3); 215 $this->assertEqual($this->cacheContainer->getUsedSpace(), 3); 216 } 181 217 } 182 218 ?>
