1 |
6daefa8c
|
Petr Lukašík
|
<?php
|
2 |
|
|
/* SVN FILE: $Id: cache.php 4450 2007-02-05 05:18:05Z phpnut $ */
|
3 |
|
|
/**
|
4 |
|
|
* Caching for Cake.
|
5 |
|
|
*
|
6 |
|
|
*
|
7 |
|
|
* PHP versions 4 and 5
|
8 |
|
|
*
|
9 |
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
10 |
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
11 |
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
12 |
|
|
* Las Vegas, Nevada 89104
|
13 |
|
|
*
|
14 |
|
|
* Licensed under The MIT License
|
15 |
|
|
* Redistributions of files must retain the above copyright notice.
|
16 |
|
|
*
|
17 |
|
|
* @filesource
|
18 |
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
19 |
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
20 |
|
|
* @package cake
|
21 |
|
|
* @subpackage cake.cake.libs
|
22 |
|
|
* @since CakePHP(tm) v 0.2.9
|
23 |
|
|
* @version $Revision: 4450 $
|
24 |
|
|
* @modifiedby $LastChangedBy: phpnut $
|
25 |
|
|
* @lastmodified $Date: 2007-02-04 23:18:05 -0600 (Sun, 04 Feb 2007) $
|
26 |
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
27 |
|
|
*/
|
28 |
|
|
/**
|
29 |
|
|
* Included libraries.
|
30 |
|
|
*
|
31 |
|
|
*/
|
32 |
|
|
if (!class_exists('Model')) {
|
33 |
|
|
uses(DS . 'model' . DS . 'model');
|
34 |
|
|
}
|
35 |
|
|
/**
|
36 |
|
|
* Caching for Cake.
|
37 |
|
|
*
|
38 |
|
|
*
|
39 |
|
|
* @package cake
|
40 |
|
|
* @subpackage cake.cake.libs
|
41 |
|
|
*/
|
42 |
|
|
class Cache extends Model{
|
43 |
|
|
/**
|
44 |
|
|
* Identifier. Either an MD5 string or NULL.
|
45 |
|
|
*
|
46 |
|
|
* @var string
|
47 |
|
|
*/
|
48 |
|
|
var $id = null;
|
49 |
|
|
/**
|
50 |
|
|
* Content container for cache data.
|
51 |
|
|
*
|
52 |
|
|
* @var unknown_type
|
53 |
|
|
*/
|
54 |
|
|
var $data = null;
|
55 |
|
|
/**
|
56 |
|
|
* Content to be cached.
|
57 |
|
|
*
|
58 |
|
|
* @var unknown_type
|
59 |
|
|
*/
|
60 |
|
|
var $for_caching = null;
|
61 |
|
|
/**
|
62 |
|
|
* Name of the database table used for caching.
|
63 |
|
|
*
|
64 |
|
|
* @var string
|
65 |
|
|
*/
|
66 |
|
|
var $useTable = 'cache';
|
67 |
|
|
/**
|
68 |
|
|
* Constructor. Generates an md5'ed id for internal use. Calls the constructor on Model as well.
|
69 |
|
|
*
|
70 |
|
|
* @param unknown_type $id
|
71 |
|
|
*/
|
72 |
|
|
function __construct($id) {
|
73 |
|
|
$this->id = (md5($id));
|
74 |
|
|
parent::__construct($this->id);
|
75 |
|
|
}
|
76 |
|
|
/**
|
77 |
|
|
* Returns this object's id after setting it. If called without parameters the current object's id is returned.
|
78 |
|
|
*
|
79 |
|
|
* @param unknown_type $id
|
80 |
|
|
* @return unknown
|
81 |
|
|
*/
|
82 |
|
|
function id($id = null) {
|
83 |
|
|
if (!$id) {
|
84 |
|
|
return $this->id;
|
85 |
|
|
}
|
86 |
|
|
return ($this->id = $id);
|
87 |
|
|
}
|
88 |
|
|
/**
|
89 |
|
|
* Store given content in cache database.
|
90 |
|
|
*
|
91 |
|
|
* @param string $content Content to keep in cache.
|
92 |
|
|
* @param int $keep_for Number of seconds to keep data in cache.
|
93 |
|
|
* @return boolean Success
|
94 |
|
|
*/
|
95 |
|
|
function remember($content, $keep_for = CACHE_PAGES_FOR) {
|
96 |
|
|
$data = addslashes($this->for_caching . $content);
|
97 |
|
|
$expire = date("Y-m-d H:i:s", time() + ($keep_for > 0 ? $keep_for : 999999999));
|
98 |
|
|
return $this->query("REPLACE {$this->useTable} (id,data,expire) VALUES ('{$this->id}', '{$data}', '{$expire}')");
|
99 |
|
|
}
|
100 |
|
|
/**
|
101 |
|
|
* Returns content from the Cache object itself, if the Cache object has a non-empty data property.
|
102 |
|
|
* Else from the database cache.
|
103 |
|
|
*
|
104 |
|
|
* @return unknown
|
105 |
|
|
*/
|
106 |
|
|
function restore() {
|
107 |
|
|
if (empty($this->data['data'])) {
|
108 |
|
|
return $this->find("id='{$this->id}' AND expire>NOW()");
|
109 |
|
|
}
|
110 |
|
|
return $this->data['data'];
|
111 |
|
|
}
|
112 |
|
|
/**
|
113 |
|
|
* Returns true if the cache data property has current (non-stale) content for given id.
|
114 |
|
|
*
|
115 |
|
|
* @return boolean
|
116 |
|
|
*/
|
117 |
|
|
function has() {
|
118 |
|
|
return is_array($this->data = $this->find("id='{$this->id}' AND expire>NOW()"));
|
119 |
|
|
}
|
120 |
|
|
/**
|
121 |
|
|
* Appends $string to the for_caching property of the Cache object.
|
122 |
|
|
*
|
123 |
|
|
* @param string $string
|
124 |
|
|
*/
|
125 |
|
|
function append($string) {
|
126 |
|
|
$this->for_caching .= $string;
|
127 |
|
|
}
|
128 |
|
|
/**
|
129 |
|
|
* Clears the cache database table.
|
130 |
|
|
*
|
131 |
|
|
* @return unknown
|
132 |
|
|
*/
|
133 |
|
|
function clear() {
|
134 |
|
|
return $this->query("DELETE FROM {$this->useTable}");
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
?>
|