Friday, October 28, 2011

Cache FileEngine

Hello,

I made an increment for file engine system.
My problem was that in APC cache, the cache is not shared between CLI
and the web app... then I did that with fileEngine using flock... it's
not workink on windows.


<?php

/**
* FileEngine with inc/dec
*
* @author Sandreu
*
* @property SplFileObject $_File
*/

App::import('Cache/Engine', 'FileEngine');

class FileIncEngine extends FileEngine {

public function increment($key, $offset = 1) {
if ($this->settings['isWindows']) throw new
CacheException(__d('cake_dev', 'Files cannot be atomically decremented
on windows.'));
if (!$this->_init || $this->_setKey($key) === false) {
if (!$this->write($key, $offset, $this-
>settings['duration'])) return false;
return $offset;
}

$lock = $this->settings['lock'];
$this->settings['lock'] = false;

$this->_File->flock(LOCK_EX);

$val = $this->read($key);
if (!is_integer($val)) {
$this->_File->flock(LOCK_UN);
return false;
} else {
$val += $offset;
}

$this->_File->rewind();
if (!$this->write($key, $val, $this->settings['duration']))
{echo 'error'; return false; }

$this->_File->flock(LOCK_UN);

$this->settings['lock'] = $lock;
return $val;
}

public function decrement($key, $offset = 1) {
return $this->increment($key, 0-$offset);
}
}

?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: