2005-07-04 01:07:14 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* Caching for Cake.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-05-26 05:29:17 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @filesource
|
2006-05-26 05:29:17 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Included libraries.
|
|
|
|
*
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!class_exists('Model')) {
|
|
|
|
uses(DS . 'model' . DS . 'model');
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* Caching for Cake.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
|
|
|
*
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
class Cache extends Model{
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Identifier. Either an MD5 string or NULL.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $id = null;
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Content container for cache data.
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $data = null;
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Content to be cached.
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $for_caching = null;
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Name of the database table used for caching.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $useTable = 'cache';
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Constructor. Generates an md5'ed id for internal use. Calls the constructor on Model as well.
|
|
|
|
*
|
|
|
|
* @param unknown_type $id
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function __construct($id) {
|
|
|
|
$this->id = (md5($id));
|
|
|
|
parent::__construct($this->id);
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Returns this object's id after setting it. If called without parameters the current object's id is returned.
|
|
|
|
*
|
|
|
|
* @param unknown_type $id
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function id($id = null) {
|
|
|
|
if (!$id) {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
return ($this->id = $id);
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Store given content in cache database.
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param string $content Content to keep in cache.
|
|
|
|
* @param int $keep_for Number of seconds to keep data in cache.
|
|
|
|
* @return boolean Success
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function remember($content, $keep_for = CACHE_PAGES_FOR) {
|
|
|
|
$data = addslashes($this->for_caching . $content);
|
|
|
|
$expire = date("Y-m-d H:i:s", time() + ($keep_for > 0 ? $keep_for : 999999999));
|
|
|
|
return $this->query("REPLACE {$this->useTable} (id,data,expire) VALUES ('{$this->id}', '{$data}', '{$expire}')");
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Returns content from the Cache object itself, if the Cache object has a non-empty data property.
|
|
|
|
* Else from the database cache.
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function restore() {
|
|
|
|
if (empty($this->data['data'])) {
|
|
|
|
return $this->find("id='{$this->id}' AND expire>NOW()");
|
|
|
|
}
|
|
|
|
return $this->data['data'];
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the cache data property has current (non-stale) content for given id.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function has() {
|
|
|
|
return is_array($this->data = $this->find("id='{$this->id}' AND expire>NOW()"));
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Appends $string to the for_caching property of the Cache object.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function append($string) {
|
|
|
|
$this->for_caching .= $string;
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Clears the cache database table.
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function clear() {
|
|
|
|
return $this->query("DELETE FROM {$this->useTable}");
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
?>
|