| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TSqlMapCacheModel, TSqlMapCacheTypes and TSqlMapCacheKey classes file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TSqlMapCacheModel.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap.Configuration
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TSqlMapCacheModel corresponds to the <cacheModel> sql mapping configuration tag.
|
|
|
15 |
*
|
|
|
16 |
* The results from a query Mapped Statement can be cached simply by specifying
|
|
|
17 |
* the {@link CacheModel TSqlMapStatement::setCacheModel()} property in <statement> tag.
|
|
|
18 |
* A cache model is a configured cache that is defined within the sql map
|
|
|
19 |
* configuration file. Cache models are configured using the <cacheModel> element.
|
|
|
20 |
*
|
|
|
21 |
* The cache model uses a pluggable framework for supporting different types of
|
|
|
22 |
* caches. The choice of cache is specified by the {@link Implementation setImplementation()}
|
|
|
23 |
* property. The class name specified must be one of {@link TSqlMapCacheTypes}.
|
|
|
24 |
*
|
|
|
25 |
* The cache implementations, LRU and FIFO cache below do not persist across
|
|
|
26 |
* requests. That is, once the request is complete, all cache data is lost.
|
|
|
27 |
* These caches are useful queries that results in the same repeated data during
|
|
|
28 |
* the current request.
|
|
|
29 |
*
|
|
|
30 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
31 |
* @version $Id: TSqlMapCacheModel.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
32 |
* @package System.Data.SqlMap.Configuration
|
|
|
33 |
* @since 3.1
|
|
|
34 |
*/
|
|
|
35 |
class TSqlMapCacheModel extends TComponent
|
|
|
36 |
{
|
|
|
37 |
private $_cache;
|
|
|
38 |
private $_hits = 0;
|
|
|
39 |
private $_requests = 0;
|
|
|
40 |
private $_id;
|
|
|
41 |
private $_implementation='basic';
|
|
|
42 |
private $_properties = array();
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* @return string unique cache model identifier.
|
|
|
46 |
*/
|
|
|
47 |
public function getID()
|
|
|
48 |
{
|
|
|
49 |
return $this->_id;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* @param string unique cache model identifier.
|
|
|
54 |
*/
|
|
|
55 |
public function setID($value)
|
|
|
56 |
{
|
|
|
57 |
$this->_id = $value;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* @return string cache implements of TSqlMapCacheTypes, either 'Basic', 'LRU' or 'FIFO'.
|
|
|
62 |
*/
|
|
|
63 |
public function getImplementation()
|
|
|
64 |
{
|
|
|
65 |
return $this->_implementation;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* @param string cache implements of TSqlMapCacheTypes, either 'Basic', 'LRU' or 'FIFO'.
|
|
|
70 |
*/
|
|
|
71 |
public function setImplementation($value)
|
|
|
72 |
{
|
|
|
73 |
$this->_implementation = TPropertyValue::ensureEnum($value,'TSqlMapCacheTypes');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Initialize the cache implementation, sets the actual cache contain if supplied.
|
|
|
78 |
* @param ISqLMapCache cache implementation instance.
|
|
|
79 |
*/
|
|
|
80 |
public function initialize($cache=null)
|
|
|
81 |
{
|
|
|
82 |
if(is_null($cache))
|
|
|
83 |
$this->_cache= Prado::createComponent($this->getImplementationClass());
|
|
|
84 |
else
|
|
|
85 |
$this->_cache=$cache;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* @return string cache implementation class name.
|
|
|
90 |
*/
|
|
|
91 |
public function getImplementationClass()
|
|
|
92 |
{
|
|
|
93 |
switch(TPropertyValue::ensureEnum($this->_implementation,'TSqlMapCacheTypes'))
|
|
|
94 |
{
|
|
|
95 |
case TSqlMapCacheTypes::FIFO: return 'TSqlMapFifoCache';
|
|
|
96 |
case TSqlMapCacheTypes::LRU : return 'TSqlMapLruCache';
|
|
|
97 |
case TSqlMapCacheTypes::Basic : return 'TSqlMapApplicationCache';
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Register a mapped statement that will trigger a cache flush.
|
|
|
103 |
* @param TMappedStatement mapped statement that may flush the cache.
|
|
|
104 |
*/
|
|
|
105 |
public function registerTriggerStatement($mappedStatement)
|
|
|
106 |
{
|
|
|
107 |
$mappedStatement->attachEventHandler('OnExecuteQuery',array($this, 'flush'));
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Clears the cache.
|
|
|
112 |
*/
|
|
|
113 |
public function flush()
|
|
|
114 |
{
|
|
|
115 |
$this->_cache->flush();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* @param TSqlMapCacheKey|string cache key
|
|
|
120 |
* @return mixed cached value.
|
|
|
121 |
*/
|
|
|
122 |
public function get($key)
|
|
|
123 |
{
|
|
|
124 |
if($key instanceof TSqlMapCacheKey)
|
|
|
125 |
$key = $key->getHash();
|
|
|
126 |
|
|
|
127 |
//if flush ?
|
|
|
128 |
$value = $this->_cache->get($key);
|
|
|
129 |
$this->_requests++;
|
|
|
130 |
if(!is_null($value))
|
|
|
131 |
$this->_hits++;
|
|
|
132 |
return $value;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* @param TSqlMapCacheKey|string cache key
|
|
|
137 |
* @param mixed value to be cached.
|
|
|
138 |
*/
|
|
|
139 |
public function set($key, $value)
|
|
|
140 |
{
|
|
|
141 |
if($key instanceof TSqlMapCacheKey)
|
|
|
142 |
$key = $key->getHash();
|
|
|
143 |
|
|
|
144 |
if(!is_null($value))
|
|
|
145 |
$this->_cache->set($key, $value);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* @return float cache hit ratio.
|
|
|
150 |
*/
|
|
|
151 |
public function getHitRatio()
|
|
|
152 |
{
|
|
|
153 |
if($this->_requests != 0)
|
|
|
154 |
return $this->_hits / $this->_requests;
|
|
|
155 |
else
|
|
|
156 |
return 0;
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* TSqlMapCacheTypes enumerable class.
|
|
|
162 |
*
|
|
|
163 |
* Implemented cache are 'Basic', 'FIFO' and 'LRU'.
|
|
|
164 |
*
|
|
|
165 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
166 |
* @version $Id: TSqlMapCacheModel.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
167 |
* @package System.Data.SqlMap.Configuration
|
|
|
168 |
* @since 3.1
|
|
|
169 |
*/
|
|
|
170 |
class TSqlMapCacheTypes extends TEnumerable
|
|
|
171 |
{
|
|
|
172 |
const Basic='Basic';
|
|
|
173 |
const FIFO='FIFO';
|
|
|
174 |
const LRU='LRU';
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* TSqlMapCacheKey class.
|
|
|
179 |
*
|
|
|
180 |
* Provides a hash of the object to be cached.
|
|
|
181 |
*
|
|
|
182 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
183 |
* @version $Id: TSqlMapCacheModel.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
184 |
* @package System.Data.SqlMap.Configuration
|
|
|
185 |
* @since 3.1
|
|
|
186 |
*/
|
|
|
187 |
class TSqlMapCacheKey
|
|
|
188 |
{
|
|
|
189 |
private $_key;
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* @param mixed object to be cached.
|
|
|
193 |
*/
|
|
|
194 |
public function __construct($object)
|
|
|
195 |
{
|
|
|
196 |
$this->_key = $this->generateKey(serialize($object));
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* @param string serialized object
|
|
|
201 |
* @return string crc32 hash of the serialized object.
|
|
|
202 |
*/
|
|
|
203 |
protected function generateKey($string)
|
|
|
204 |
{
|
|
|
205 |
return sprintf('%x',crc32($string));
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* @return string object hash.
|
|
|
210 |
*/
|
|
|
211 |
public function getHash()
|
|
|
212 |
{
|
|
|
213 |
return $this->_key;
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|