Skip to content

Commit 5a0adce

Browse files
committed
Added possibility to use sfAPCCache without APC (same as sfNoCache)
1 parent 863e65e commit 5a0adce

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

lib/cache/sfAPCCache.class.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
@@ -18,6 +18,8 @@
1818
*/
1919
class sfAPCCache extends sfCache
2020
{
21+
protected $enabled;
22+
2123
/**
2224
* Initializes this sfCache instance.
2325
*
@@ -31,18 +33,21 @@ public function initialize($options = array())
3133
{
3234
parent::initialize($options);
3335

34-
if (!function_exists('apc_store') || !ini_get('apc.enabled'))
35-
{
36-
throw new sfInitializationException('You must have APC installed and enabled to use sfAPCCache class.');
37-
}
36+
$this->enabled = function_exists('apc_store') && ini_get('apc.enabled');
3837
}
3938

4039
/**
4140
* @see sfCache
4241
*/
4342
public function get($key, $default = null)
4443
{
44+
if (!$this->enabled)
45+
{
46+
return $default;
47+
}
48+
4549
$value = $this->fetch($this->getOption('prefix').$key, $has);
50+
4651
return $has ? $value : $default;
4752
}
4853

@@ -51,7 +56,13 @@ public function get($key, $default = null)
5156
*/
5257
public function has($key)
5358
{
59+
if (!$this->enabled)
60+
{
61+
return false;
62+
}
63+
5464
$this->fetch($this->getOption('prefix').$key, $has);
65+
5566
return $has;
5667
}
5768

@@ -68,15 +79,21 @@ private function fetch($key, &$success)
6879
{
6980
$success = $value !== false;
7081
}
82+
7183
return $value;
7284
}
73-
74-
85+
86+
7587
/**
7688
* @see sfCache
7789
*/
7890
public function set($key, $data, $lifetime = null)
7991
{
92+
if (!$this->enabled)
93+
{
94+
return true;
95+
}
96+
8097
return apc_store($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
8198
}
8299

@@ -85,6 +102,11 @@ public function set($key, $data, $lifetime = null)
85102
*/
86103
public function remove($key)
87104
{
105+
if (!$this->enabled)
106+
{
107+
return true;
108+
}
109+
88110
return apc_delete($this->getOption('prefix').$key);
89111
}
90112

@@ -93,6 +115,11 @@ public function remove($key)
93115
*/
94116
public function clean($mode = sfCache::ALL)
95117
{
118+
if (!$this->enabled)
119+
{
120+
return true;
121+
}
122+
96123
if (sfCache::ALL === $mode)
97124
{
98125
return apc_clear_cache('user');
@@ -130,6 +157,11 @@ public function getTimeout($key)
130157
*/
131158
public function removePattern($pattern)
132159
{
160+
if (!$this->enabled)
161+
{
162+
return true;
163+
}
164+
133165
$infos = apc_cache_info('user');
134166
if (!is_array($infos['cache_list']))
135167
{
@@ -147,8 +179,20 @@ public function removePattern($pattern)
147179
}
148180
}
149181

182+
/**
183+
* Gets the cache info
184+
*
185+
* @param string $key The cache key
186+
*
187+
* @return string
188+
*/
150189
protected function getCacheInfo($key)
151190
{
191+
if (!$this->enabled)
192+
{
193+
return false;
194+
}
195+
152196
$infos = apc_cache_info('user');
153197

154198
if (is_array($infos['cache_list']))

0 commit comments

Comments
 (0)