mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-13 13:09:49 +00:00
updating model Cache engine.
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5975 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
e3e02ba775
commit
3feac9a768
2 changed files with 72 additions and 23 deletions
cake
51
cake/libs/cache/model.php
vendored
51
cake/libs/cache/model.php
vendored
|
@ -32,13 +32,6 @@
|
||||||
* @subpackage cake.cake.libs.cache
|
* @subpackage cake.cake.libs.cache
|
||||||
*/
|
*/
|
||||||
class ModelEngine extends CacheEngine {
|
class ModelEngine extends CacheEngine {
|
||||||
/**
|
|
||||||
* Model instance.
|
|
||||||
*
|
|
||||||
* @var object
|
|
||||||
* @access private
|
|
||||||
*/
|
|
||||||
var $__Model = null;
|
|
||||||
/**
|
/**
|
||||||
* settings
|
* settings
|
||||||
* className = name of the model to use, default => Cache
|
* className = name of the model to use, default => Cache
|
||||||
|
@ -48,6 +41,21 @@ class ModelEngine extends CacheEngine {
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
var $settings = array();
|
var $settings = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model instance.
|
||||||
|
*
|
||||||
|
* @var object
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
var $__Model = null;
|
||||||
|
/**
|
||||||
|
* Model instance.
|
||||||
|
*
|
||||||
|
* @var object
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
var $__fields = array();
|
||||||
/**
|
/**
|
||||||
* Initialize the Cache Engine
|
* Initialize the Cache Engine
|
||||||
*
|
*
|
||||||
|
@ -60,13 +68,16 @@ class ModelEngine extends CacheEngine {
|
||||||
*/
|
*/
|
||||||
function init($settings) {
|
function init($settings) {
|
||||||
parent::init($settings);
|
parent::init($settings);
|
||||||
$defaults = array('className'=> 'Cache', 'fields'=> array('data', 'expires'));
|
$defaults = array('className'=> 'CacheModel', 'fields'=> array('data', 'expires'));
|
||||||
$this->settings = am($this->settings, $defaults, $settings);
|
$this->settings = am($this->settings, $defaults, $settings);
|
||||||
if (!class_exists($this->settings['className']) && !loadModel($this->settings['className'])) {
|
$className = $this->settings['className'];
|
||||||
$this->__Model = new $modelName();
|
$this->__fields = $this->settings['fields'];
|
||||||
|
if (class_exists($className) || loadModel($className)) {
|
||||||
|
$this->__Model = new $className();
|
||||||
} else {
|
} else {
|
||||||
$this->__Model = new Model(array('name' => $this->settings['className']));
|
$this->__Model = new Model(array('name' => $className));
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Garbage collection. Permanently remove all expired and deleted data
|
* Garbage collection. Permanently remove all expired and deleted data
|
||||||
|
@ -94,15 +105,15 @@ class ModelEngine extends CacheEngine {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$cache = array($this->__Model->alias => array(
|
$cache = array('id' => $key,
|
||||||
$this->__fields[0] => $data,
|
$this->__fields[0] => $data,
|
||||||
$this->__fields[1] => time() + $duration));
|
$this->__fields[1] => time() + $duration
|
||||||
|
);
|
||||||
$oldId = $this->__Model->id;
|
$result = false;
|
||||||
$this->__Model->id = $key;
|
if ($this->__Model->save($cache)) {
|
||||||
$res = $this->__Model->save($cache);
|
$result = true;
|
||||||
$this->__Model->id = $oldId;
|
}
|
||||||
return $res;
|
return $result;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Read a key from the cache
|
* Read a key from the cache
|
||||||
|
@ -117,7 +128,7 @@ class ModelEngine extends CacheEngine {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($this->settings['serialize'])) {
|
if (isset($this->settings['serialize'])) {
|
||||||
return unserialize($val);
|
return unserialize($data);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
42
cake/tests/cases/libs/cache/model.test.php
vendored
42
cake/tests/cases/libs/cache/model.test.php
vendored
|
@ -27,16 +27,50 @@
|
||||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||||
*/
|
*/
|
||||||
uses('cache', 'cache' . DS . 'model');
|
uses('cache', 'cache' . DS . 'model');
|
||||||
|
|
||||||
|
class CacheTestModel extends CakeTestModel {
|
||||||
|
|
||||||
|
var $name = 'CacheTestModel';
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Short description for class.
|
* Short description for class.
|
||||||
*
|
*
|
||||||
* @package cake.tests
|
* @package cake.tests
|
||||||
* @subpackage cake.tests.cases.libs.cache
|
* @subpackage cake.tests.cases.libs.cache
|
||||||
*/
|
*/
|
||||||
class ModelEngineTest extends UnitTestCase {
|
class ModelEngineTest extends CakeTestCase {
|
||||||
|
|
||||||
|
var $fixtures = array('core.cache_test_model');
|
||||||
|
|
||||||
function skip() {
|
function skip() {
|
||||||
$this->skipif (true, 'ModelEngineTest not implemented');
|
$this->skipif (false, 'ModelEngineTest not implemented');
|
||||||
|
}
|
||||||
|
function setUp() {
|
||||||
|
Cache::config('model');
|
||||||
|
}
|
||||||
|
function start() {
|
||||||
|
parent::start();
|
||||||
|
Cache::config('model', array('engine' => 'Model', //[required]
|
||||||
|
'duration'=> 3600, //[optional]
|
||||||
|
'probability'=> 100, //[optional]
|
||||||
|
'className' => 'CacheTestModel', //[optional]
|
||||||
|
'fields' => array('data', 'expires'), //[optional]
|
||||||
|
'serialize' => true, //[optional]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function testSettings() {
|
||||||
|
$settings = Cache::settings();
|
||||||
|
$expecting = array('duration'=> 3600, //[optional]
|
||||||
|
'probability'=> 100, //[optional]
|
||||||
|
'className' => 'CacheTestModel', //[optional]
|
||||||
|
'fields' => array('data', 'expires'), //[optional]
|
||||||
|
'serialize' => true, //[optional]
|
||||||
|
'engine' => 'Model'
|
||||||
|
);
|
||||||
|
$this->assertEqual($settings, $expecting);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testReadAndWriteCache() {
|
function testReadAndWriteCache() {
|
||||||
|
@ -83,5 +117,9 @@ class ModelEngineTest extends UnitTestCase {
|
||||||
$result = Cache::delete('delete_test');
|
$result = Cache::delete('delete_test');
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tearDown() {
|
||||||
|
Cache::config('default');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
Add table
Reference in a new issue