cache.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: cache_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Caching for Cake.
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
00022  * @since           CakePHP(tm) v 0.2.9
00023  * @version         $Revision: 675 $
00024  * @modifiedby      $LastChangedBy: gwoo $
00025  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00026  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00027  */
00028 /**
00029  * Included libraries.
00030  *
00031  */
00032     if (!class_exists('Model')) {
00033         uses(DS . 'model' . DS . 'model');
00034     }
00035 /**
00036  * Caching for Cake.
00037  *
00038  *
00039  * @package     cake
00040  * @subpackage  cake.cake.libs
00041  */
00042 class Cache extends Model{
00043 /**
00044  * Identifier. Either an MD5 string or NULL.
00045  *
00046  * @var string
00047  */
00048     var $id = null;
00049 /**
00050  * Content container for cache data.
00051  *
00052  * @var unknown_type
00053  */
00054     var $data = null;
00055 /**
00056  * Content to be cached.
00057  *
00058  * @var unknown_type
00059  */
00060     var $for_caching = null;
00061 /**
00062  * Name of the database table used for caching.
00063  *
00064  * @var string
00065  */
00066     var $useTable = 'cache';
00067 /**
00068  * Constructor. Generates an md5'ed id for internal use. Calls the constructor on Model as well.
00069  *
00070  * @param unknown_type $id
00071  */
00072     function __construct($id) {
00073         $this->id = (md5($id));
00074         parent::__construct($this->id);
00075     }
00076 /**
00077  * Returns this object's id after setting it. If called without parameters the current object's id is returned.
00078  *
00079  * @param unknown_type $id
00080  * @return unknown
00081  */
00082     function id($id = null) {
00083         if (!$id) {
00084             return $this->id;
00085         }
00086         return ($this->id = $id);
00087     }
00088 /**
00089  * Store given content in cache database.
00090  *
00091  * @param string $content Content to keep in cache.
00092  * @param int $keep_for Number of seconds to keep data in cache.
00093  * @return boolean              Success
00094  */
00095     function remember($content, $keep_for = CACHE_PAGES_FOR) {
00096         $data = addslashes($this->for_caching . $content);
00097         $expire = date("Y-m-d H:i:s", time() + ($keep_for > 0 ? $keep_for : 999999999));
00098         return $this->query("REPLACE {$this->useTable} (id,data,expire) VALUES ('{$this->id}', '{$data}', '{$expire}')");
00099     }
00100 /**
00101  * Returns content from the Cache object itself, if the Cache object has a non-empty data property.
00102  * Else from the database cache.
00103  *
00104  * @return unknown
00105  */
00106     function restore() {
00107         if (empty($this->data['data'])) {
00108             return $this->find("id='{$this->id}' AND expire>NOW()");
00109         }
00110         return $this->data['data'];
00111     }
00112 /**
00113  * Returns true if the cache data property has current (non-stale) content for given id.
00114  *
00115  * @return boolean
00116  */
00117     function has() {
00118         return is_array($this->data = $this->find("id='{$this->id}' AND expire>NOW()"));
00119     }
00120 /**
00121  * Appends $string to the for_caching property of the Cache object.
00122  *
00123  * @param string $string
00124  */
00125     function append($string) {
00126         $this->for_caching .= $string;
00127     }
00128 /**
00129  * Clears the cache database table.
00130  *
00131  * @return unknown
00132  */
00133     function clear() {
00134         return $this->query("DELETE FROM {$this->useTable}");
00135     }
00136 }
00137 ?>