model.php
Go to the documentation of this file.00001 <?php 00002 /* SVN FILE: $Id: libs_2cache_2model_8php-source.html 550 2008-05-22 14:52:12Z gwoo $ */ 00003 /** 00004 * Database Storage engine for cache 00005 * 00006 * 00007 * PHP versions 4 and 5 00008 * 00009 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 00010 * Copyright 2005-2008, Cake Software Foundation, Inc. 00011 * 1785 E. Sahara Avenue, Suite 490-204 00012 * Las Vegas, Nevada 89104 00013 * 00014 * Licensed under The MIT License 00015 * Redistributions of files must retain the above copyright notice. 00016 * 00017 * @filesource 00018 * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. 00019 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 00020 * @package cake 00021 * @subpackage cake.cake.libs.cache 00022 * @since CakePHP(tm) v 1.2.0.4933 00023 * @version $Revision: 550 $ 00024 * @modifiedby $LastChangedBy: gwoo $ 00025 * @lastmodified $Date: 2008-05-22 09:52:12 -0500 (Thu, 22 May 2008) $ 00026 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 00027 */ 00028 /** 00029 * Database Storage engine for cache 00030 * 00031 * @package cake 00032 * @subpackage cake.cake.libs.cache 00033 */ 00034 class ModelEngine extends CacheEngine { 00035 /** 00036 * settings 00037 * className = name of the model to use, default => Cache 00038 * fields = database fields that hold data and ttl, default => data, expires 00039 * 00040 * @var array 00041 * @access public 00042 */ 00043 var $settings = array(); 00044 00045 /** 00046 * Model instance. 00047 * 00048 * @var object 00049 * @access private 00050 */ 00051 var $__Model = null; 00052 /** 00053 * Model instance. 00054 * 00055 * @var object 00056 * @access private 00057 */ 00058 var $__fields = array(); 00059 /** 00060 * Initialize the Cache Engine 00061 * 00062 * Called automatically by the cache frontend 00063 * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array()); 00064 * 00065 * @param array $setting array of setting for the engine 00066 * @return boolean True if the engine has been successfully initialized, false if not 00067 * @access public 00068 */ 00069 function init($settings) { 00070 parent::init($settings); 00071 $defaults = array('className'=> 'CacheModel', 'fields'=> array('data', 'expires')); 00072 $this->settings = array_merge($this->settings, $defaults, $settings); 00073 $className = $this->settings['className']; 00074 $this->__fields = $this->settings['fields']; 00075 if (App::import($className)) { 00076 $this->__Model = ClassRegistry::init($className); 00077 } else { 00078 $this->__Model = new Model(array('name' => $className)); 00079 } 00080 return true; 00081 } 00082 /** 00083 * Garbage collection. Permanently remove all expired and deleted data 00084 * 00085 * @access public 00086 */ 00087 function gc() { 00088 return $this->__Model->deleteAll(array($this->__fields[1] => '<= '.time())); 00089 } 00090 /** 00091 * Write data for key into cache 00092 * 00093 * @param string $key Identifier for the data 00094 * @param mixed $data Data to be cached 00095 * @param integer $duration How long to cache the data, in seconds 00096 * @return boolean True if the data was succesfully cached, false on failure 00097 * @access public 00098 */ 00099 function write($key, &$data, $duration) { 00100 if (isset($this->settings['serialize'])) { 00101 $data = serialize($data); 00102 } 00103 00104 if (!$data) { 00105 return false; 00106 } 00107 00108 $cache = array('id' => $key, 00109 $this->__fields[0] => $data, 00110 $this->__fields[1] => time() + $duration 00111 ); 00112 $result = false; 00113 if ($this->__Model->save($cache)) { 00114 $result = true; 00115 } 00116 return $result; 00117 } 00118 /** 00119 * Read a key from the cache 00120 * 00121 * @param string $key Identifier for the data 00122 * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it 00123 * @access public 00124 */ 00125 function read($key) { 00126 $data = $this->__Model->field($this->__fields[0], array($this->__Model->primaryKey => $key, $this->__fields[1] => '> '.time())); 00127 if (!$data) { 00128 return false; 00129 } 00130 if (isset($this->settings['serialize'])) { 00131 return unserialize($data); 00132 } 00133 return $data; 00134 } 00135 /** 00136 * Delete a key from the cache 00137 * 00138 * @param string $key Identifier for the data 00139 * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed 00140 * @access public 00141 */ 00142 function delete($key) { 00143 return $this->__Model->del($key); 00144 } 00145 /** 00146 * Delete all keys from the cache 00147 * 00148 * @return boolean True if the cache was succesfully cleared, false otherwise 00149 * @access public 00150 */ 00151 function clear() { 00152 return $this->__Model->deleteAll('1=1'); 00153 } 00154 00155 } 00156 ?>