Skip to content

Commit

Permalink
Fix issue writing to file cache
Browse files Browse the repository at this point in the history
Reading/writing to the same file cache key multiple times
in a row during a single request would result in failed reads.

Fixes #2114
  • Loading branch information
markstory committed Oct 19, 2011
1 parent d7155d3 commit 95737d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Cake/Cache/Engine/FileEngine.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function write($key, $data, $duration) {
$this->_File->flock(LOCK_EX); $this->_File->flock(LOCK_EX);
} }


$this->_File->rewind();
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush(); $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();


if ($this->settings['lock']) { if ($this->settings['lock']) {
Expand Down
17 changes: 17 additions & 0 deletions lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ public function testReadAndWriteCache() {
Cache::delete('test', 'file_test'); 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 * testExpiry method
* *
Expand Down

0 comments on commit 95737d7

Please sign in to comment.