Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TSqlMapCache class file contains FIFO, LRU, and GLOBAL cache implementations.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TSqlMapCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data.SqlMap
11
 */
12
 
13
/**
14
 * Allow different implementation of caching strategy. See <tt>TSqlMapFifoCache</tt>
15
 * for a first-in-first-out implementation. See <tt>TSqlMapLruCache</tt> for
16
 * a least-recently-used cache implementation.
17
 *
18
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
19
 * @version $Id: TSqlMapCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
20
 * @package System.Data.SqlMap
21
 * @since 3.1
22
 */
23
abstract class TSqlMapCache implements ICache
24
{
25
	protected $_keyList;
26
	protected $_cache;
27
	protected $_cacheSize = 100;
28
 
29
	/**
30
	 * Create a new cache with limited cache size.
31
	 * @param integer maxium number of items to cache.
32
	 */
33
	public function __construct($cacheSize=100)
34
	{
35
		$this->_cache = new TMap;
36
		$this->_cacheSize = intval($cacheSize);
37
		$this->_keyList = new TList;
38
	}
39
 
40
	/**
41
	 * Maximum number of items to cache. Default size is 100.
42
	 * @param int cache size.
43
	 */
44
	public function setCacheSize($value)
45
	{
46
		$this->_cacheSize=TPropertyValue::ensureInteger($value,100);
47
	}
48
 
49
	/**
50
	 * @return int cache size.
51
	 */
52
	public function getCacheSize()
53
	{
54
		return $this->_cacheSize;
55
	}
56
 
57
	/**
58
	 * @return object the object removed if exists, null otherwise.
59
	 */
60
	public function delete($key)
61
	{
62
		$object = $this->get($key);
63
		$this->_cache->remove($key);
64
		$this->_keyList->remove($key);
65
		return $object;
66
	}
67
 
68
	/**
69
	 * Clears the cache.
70
	 */
71
	public function flush()
72
	{
73
		$this->_keyList->clear();
74
		$this->_cache->clear();
75
	}
76
 
77
	/**
78
	 * @throws TSqlMapException not implemented.
79
	 */
80
	public function add($id,$value,$expire=0,$dependency=null)
81
	{
82
		throw new TSqlMapException('sqlmap_use_set_to_store_cache');
83
	}
84
}
85
 
86
/**
87
 * First-in-First-out cache implementation, removes
88
 * object that was first added when the cache is full.
89
 *
90
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
91
 * @version $Id: TSqlMapCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
92
 * @package System.Data.SqlMap
93
 * @since 3.1
94
 */
95
class TSqlMapFifoCache extends TSqlMapCache
96
{
97
	/**
98
	 * @return mixed Gets a cached object with the specified key.
99
	 */
100
	public function get($key)
101
	{
102
		return $this->_cache->itemAt($key);
103
	}
104
 
105
	/**
106
	 * Stores a value identified by a key into cache.
107
	 * The expire and dependency parameters are ignored.
108
	 * @param string cache key
109
	 * @param mixed value to cache.
110
	 */
111
	public function set($key, $value,$expire=0,$dependency=null)
112
	{
113
		$this->_cache->add($key, $value);
114
		$this->_keyList->add($key);
115
		if($this->_keyList->getCount() > $this->_cacheSize)
116
		{
117
			$oldestKey = $this->_keyList->removeAt(0);
118
			$this->_cache->remove($oldestKey);
119
		}
120
	}
121
}
122
 
123
/**
124
 * Least recently used cache implementation, removes
125
 * object that was accessed last when the cache is full.
126
 *
127
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
128
 * @version $Id: TSqlMapCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
129
 * @package System.Data.SqlMap
130
 * @since 3.1
131
 */
132
class TSqlMapLruCache extends TSqlMapCache
133
{
134
	/**
135
	 * @return mixed Gets a cached object with the specified key.
136
	 */
137
	public function get($key)
138
	{
139
		if($this->_keyList->contains($key))
140
		{
141
			$this->_keyList->remove($key);
142
			$this->_keyList->add($key);
143
			return $this->_cache->itemAt($key);
144
		}
145
	}
146
 
147
	/**
148
	 * Stores a value identified by a key into cache.
149
	 * The expire and dependency parameters are ignored.
150
	 * @param string the key identifying the value to be cached
151
	 * @param mixed the value to be cached
152
	 */
153
	public function set($key, $value,$expire=0,$dependency=null)
154
	{
155
		$this->_cache->add($key, $value);
156
		$this->_keyList->add($key);
157
		if($this->_keyList->getCount() > $this->_cacheSize)
158
		{
159
			$oldestKey = $this->_keyList->removeAt(0);
160
			$this->_cache->remove($oldestKey);
161
		}
162
	}
163
}
164
 
165
/**
166
 * TSqlMapApplicationCache uses the default Prado application cache for
167
 * caching SqlMap results.
168
 *
169
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
170
 * @version $Id: TSqlMapCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
171
 * @package System.Data.SqlMap
172
 * @since 3.1
173
 */
174
class TSqlMapApplicationCache implements ICache
175
{
176
	/**
177
	 * @param string item to be deleted.
178
	 */
179
	public function delete($key)
180
	{
181
		$this->getCache()->delete($key);
182
	}
183
 
184
	/**
185
	 * Deletes all items in the cache.
186
	 */
187
	public function flush()
188
	{
189
		$this->getCache()->flush();
190
	}
191
 
192
	/**
193
	 * @return mixed Gets a cached object with the specified key.
194
	 */
195
	public function get($key)
196
	{
197
		$result = $this->getCache()->get($key);
198
		return $result === false ? null : $result;
199
	}
200
 
201
	/**
202
	 * Stores a value identified by a key into cache.
203
	 * @param string the key identifying the value to be cached
204
	 * @param mixed the value to be cached
205
	 */
206
	public function set($key, $value,$expire=0,$dependency=null)
207
	{
208
		$this->getCache()->set($key, $value, $expire,$dependency);
209
	}
210
 
211
	/**
212
	 * @return ICache Application cache instance.
213
	 */
214
	protected function getCache()
215
	{
216
		return Prado::getApplication()->getCache();
217
	}
218
 
219
	/**
220
	 * @throws TSqlMapException not implemented.
221
	 */
222
	public function add($id,$value,$expire=0,$dependency=null)
223
	{
224
		throw new TSqlMapException('sqlmap_use_set_to_store_cache');
225
	}
226
}
227