diff --git a/lib/Cake/Cache/Engine/FileEngine.php b/lib/Cake/Cache/Engine/FileEngine.php index eb25b02c9..c53c5ad33 100644 --- a/lib/Cake/Cache/Engine/FileEngine.php +++ b/lib/Cake/Cache/Engine/FileEngine.php @@ -128,6 +128,7 @@ class FileEngine extends CacheEngine { $this->_File->flock(LOCK_EX); } + $this->_File->rewind(); $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush(); if ($this->settings['lock']) { diff --git a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php index 9ddc660de..ecc995dd3 100644 --- a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php @@ -97,6 +97,23 @@ class FileEngineTest extends CakeTestCase { Cache::delete('test', 'file_test'); } +/** + * Test read/write on the same cache key. Ensures file handles are re-wound. + * + * @return void + */ + public function testConsecutiveReadWrite() { + Cache::write('rw', 'first write', 'file_test'); + $result = Cache::read('rw', 'file_test'); + + Cache::write('rw', 'second write', 'file_test'); + $result2 = Cache::read('rw', 'file_test'); + + Cache::delete('rw', 'file_test'); + $this->assertEquals('first write', $result); + $this->assertEquals('second write', $result2); + } + /** * testExpiry method *