Skip to content
Permalink
Browse files

Implementing cache lockig in FileEngine using SplFileObject

  • Loading branch information...
lorenzo authored and predominant committed Apr 27, 2010
1 parent 977f897 commit d3b257fffb1cb082630a89146a92d2a6a8d13d04
Showing with 22 additions and 9 deletions.
  1. +22 −9 cake/libs/cache/file.php
@@ -19,9 +19,6 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
if (!class_exists('File')) {
require LIBS . 'file.php';
}
/**
* File Storage engine for cache
*
@@ -32,9 +29,9 @@
class FileEngine extends CacheEngine {
/**
* Instance of File class
* Instance of SplFileObject class
*
* @var File
* @var _File
* @access protected
*/
protected $_File = null;
@@ -126,11 +123,17 @@ public function write($key, $data, $duration) {
}
if ($this->settings['lock']) {
//$this->_File->lock = true;
$this->_File->flock(LOCK_EX);
}
$expires = time() + $duration;
$contents = $expires . $lineBreak . $data . $lineBreak;
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents);
if ($this->settings['lock']) {
$this->_File->flock(LOCK_EX);
}
return $success;
}
@@ -144,9 +147,11 @@ public function read($key) {
if (!$this->_init || $this->_setKey($key) === false) {
return false;
}
if ($this->settings['lock']) {
//$this->_File->lock = true;
$this->_File->flock(LOCK_SH);
}
$this->_File->rewind();
$time = time();
$cachetime = intval($this->_File->current());
@@ -161,6 +166,11 @@ public function read($key) {
$data .= $this->_File->current();
$this->_File->next();
}
if ($this->settings['lock']) {
$this->_File->flock(LOCK_SH);
}
$data = trim($data);
if ($data !== '' && !empty($this->settings['serialize'])) {
@@ -244,10 +254,11 @@ public function increment($key, $offset = 1) {
}
/**
* Get absolute file for a given key
* Sets the current cache key this class is managing
*
* @param string $key The key
* @return mixed Absolute cache file for the given key or false if erroneous
* @param boolean $createKey Whether the key should be created if it doesn't exists, or not
* @return boolean true if the cache key could be set, false otherwise
* @access protected
*/
protected function _setKey($key, $createKey = false) {
@@ -260,6 +271,8 @@ protected function _setKey($key, $createKey = false) {
if (empty($this->_File) || $this->_File->getBaseName() !== $key) {
$this->_File = $path->openFile('a+');
}
return true;
}
/**

0 comments on commit d3b257f

Please sign in to comment.
You can’t perform that action at this time.