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