| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TAPCCache class file
|
|
|
4 |
*
|
|
|
5 |
* @author Alban Hanry <compte_messagerie@hotmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TAPCCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Caching
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TAPCCache class
|
|
|
15 |
*
|
|
|
16 |
* TAPCCache implements a cache application module based on {@link http://www.php.net/apc APC}.
|
|
|
17 |
*
|
|
|
18 |
* By definition, cache does not ensure the existence of a value
|
|
|
19 |
* even if it never expires. Cache is not meant to be an persistent storage.
|
|
|
20 |
*
|
|
|
21 |
* To use this module, the APC PHP extension must be loaded and set in the php.ini file the following:
|
|
|
22 |
* <code>
|
|
|
23 |
* apc.cache_by_default=0
|
|
|
24 |
* </code>
|
|
|
25 |
*
|
|
|
26 |
* Some usage examples of TAPCCache are as follows,
|
|
|
27 |
* <code>
|
|
|
28 |
* $cache=new TAPCCache; // TAPCCache may also be loaded as a Prado application module
|
|
|
29 |
* $cache->init(null);
|
|
|
30 |
* $cache->add('object',$object);
|
|
|
31 |
* $object2=$cache->get('object');
|
|
|
32 |
* </code>
|
|
|
33 |
*
|
|
|
34 |
* If loaded, TAPCCache will register itself with {@link TApplication} as the
|
|
|
35 |
* cache module. It can be accessed via {@link TApplication::getCache()}.
|
|
|
36 |
*
|
|
|
37 |
* TAPCCache may be configured in application configuration file as follows
|
|
|
38 |
* <code>
|
|
|
39 |
* <module id="cache" class="System.Caching.TAPCCache" />
|
|
|
40 |
* </code>
|
|
|
41 |
*
|
|
|
42 |
* @author Alban Hanry <compte_messagerie@hotmail.com>
|
|
|
43 |
* @author Knut Urdalen <knut.urdalen@gmail.com>
|
|
|
44 |
* @version $Id: TAPCCache.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
45 |
* @package System.Caching
|
|
|
46 |
* @since 3.0b
|
|
|
47 |
*/
|
|
|
48 |
class TAPCCache extends TCache
|
|
|
49 |
{
|
|
|
50 |
/**
|
|
|
51 |
* Initializes this module.
|
|
|
52 |
* This method is required by the IModule interface.
|
|
|
53 |
* @param TXmlElement configuration for this module, can be null
|
|
|
54 |
* @throws TConfigurationException if apc extension is not installed or not started, check your php.ini
|
|
|
55 |
*/
|
|
|
56 |
public function init($config)
|
|
|
57 |
{
|
|
|
58 |
if(!extension_loaded('apc'))
|
|
|
59 |
throw new TConfigurationException('apccache_extension_required');
|
|
|
60 |
|
|
|
61 |
if(ini_get('apc.enabled') == false)
|
|
|
62 |
throw new TConfigurationException('apccache_extension_not_enabled');
|
|
|
63 |
|
|
|
64 |
if(substr(php_sapi_name(), 0, 3) === 'cli' and ini_get('apc.enable_cli') == false)
|
|
|
65 |
throw new TConfigurationException('apccache_extension_not_enabled_cli');
|
|
|
66 |
|
|
|
67 |
parent::init($config);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Retrieves a value from cache with a specified key.
|
|
|
72 |
* This is the implementation of the method declared in the parent class.
|
|
|
73 |
* @param string a unique key identifying the cached value
|
|
|
74 |
* @return string the value stored in cache, false if the value is not in the cache or expired.
|
|
|
75 |
*/
|
|
|
76 |
protected function getValue($key)
|
|
|
77 |
{
|
|
|
78 |
return apc_fetch($key);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Stores a value identified by a key in cache.
|
|
|
83 |
* This is the implementation of the method declared in the parent class.
|
|
|
84 |
*
|
|
|
85 |
* @param string the key identifying the value to be cached
|
|
|
86 |
* @param string the value to be cached
|
|
|
87 |
* @param integer the number of seconds in which the cached value will expire. 0 means never expire.
|
|
|
88 |
* @return boolean true if the value is successfully stored into cache, false otherwise
|
|
|
89 |
*/
|
|
|
90 |
protected function setValue($key,$value,$expire)
|
|
|
91 |
{
|
|
|
92 |
return apc_store($key,$value,$expire);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Stores a value identified by a key into cache if the cache does not contain this key.
|
|
|
97 |
* This is the implementation of the method declared in the parent class.
|
|
|
98 |
*
|
|
|
99 |
* @param string the key identifying the value to be cached
|
|
|
100 |
* @param string the value to be cached
|
|
|
101 |
* @param integer the number of seconds in which the cached value will expire. 0 means never expire.
|
|
|
102 |
* @return boolean true if the value is successfully stored into cache, false otherwise
|
|
|
103 |
*/
|
|
|
104 |
protected function addValue($key,$value,$expire)
|
|
|
105 |
{
|
|
|
106 |
if(function_exists('apc_add')) {
|
|
|
107 |
return apc_add($key,$value,$expire);
|
|
|
108 |
} else {
|
|
|
109 |
throw new TNotSupportedException('apccache_add_unsupported');
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Deletes a value with the specified key from cache
|
|
|
115 |
* This is the implementation of the method declared in the parent class.
|
|
|
116 |
* @param string the key of the value to be deleted
|
|
|
117 |
* @return boolean if no error happens during deletion
|
|
|
118 |
*/
|
|
|
119 |
protected function deleteValue($key)
|
|
|
120 |
{
|
|
|
121 |
return apc_delete($key);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Deletes all values from cache.
|
|
|
126 |
* Be careful of performing this operation if the cache is shared by multiple applications.
|
|
|
127 |
*/
|
|
|
128 |
public function flush()
|
|
|
129 |
{
|
|
|
130 |
return apc_clear_cache('user');
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|