Development

/branches/1.4/lib/cache/sfAPCCache.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/cache/sfAPCCache.class.php

Revision 21990, 3.2 kB (checked in by FabianLange, 5 years ago)

[1.3] fixed coding standards

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Cache class that stores cached content in APC.
13  *
14  * @package    symfony
15  * @subpackage cache
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfAPCCache extends sfCache
20 {
21   /**
22    * Initializes this sfCache instance.
23    *
24    * Available options:
25    *
26    * * see sfCache for options available for all drivers
27    *
28    * @see sfCache
29    */
30   public function initialize($options = array())
31   {
32     parent::initialize($options);
33
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     }
38   }
39
40  /**
41   * @see sfCache
42   */
43   public function get($key, $default = null)
44   {
45     $value = $this->fetch($this->getOption('prefix').$key, $has);
46     return $has ? $value : $default;
47   }
48
49   /**
50    * @see sfCache
51    */
52   public function has($key)
53   {
54     $this->fetch($this->getOption('prefix').$key, $has);
55     return $has;
56   }
57
58   private function fetch($key, &$success)
59   {
60     $has = null;
61     $value = apc_fetch($key, $has);
62     // the second argument was added in APC 3.0.17. If it is still null we fall back to the value returned
63     if (null !== $has)
64     {
65       $success = $has;
66     }
67     else
68     {
69       $success = $value !== false;
70     }
71     return $value;
72   }
73  
74  
75   /**
76    * @see sfCache
77    */
78   public function set($key, $data, $lifetime = null)
79   {
80     return apc_store($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
81   }
82
83   /**
84    * @see sfCache
85    */
86   public function remove($key)
87   {
88     return apc_delete($this->getOption('prefix').$key);
89   }
90
91   /**
92    * @see sfCache
93    */
94   public function clean($mode = sfCache::ALL)
95   {
96     if (sfCache::ALL === $mode)
97     {
98       return apc_clear_cache('user');
99     }
100   }
101
102   /**
103    * @see sfCache
104    */
105   public function getLastModified($key)
106   {
107     if ($info = $this->getCacheInfo($key))
108     {
109       return $info['creation_time'] + $info['ttl'] > time() ? $info['mtime'] : 0;
110     }
111
112     return 0;
113   }
114
115   /**
116    * @see sfCache
117    */
118   public function getTimeout($key)
119   {
120     if ($info = $this->getCacheInfo($key))
121     {
122       return $info['creation_time'] + $info['ttl'] > time() ? $info['creation_time'] + $info['ttl'] : 0;
123     }
124
125     return 0;
126   }
127
128   /**
129    * @see sfCache
130    */
131   public function removePattern($pattern)
132   {
133     $infos = apc_cache_info('user');
134     if (!is_array($infos['cache_list']))
135     {
136       return;
137     }
138
139     $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
140
141     foreach ($infos['cache_list'] as $info)
142     {
143       if (preg_match($regexp, $info['info']))
144       {
145         apc_delete($info['info']);
146       }
147     }
148   }
149
150   protected function getCacheInfo($key)
151   {
152     $infos = apc_cache_info('user');
153
154     if (is_array($infos['cache_list']))
155     {
156       foreach ($infos['cache_list'] as $info)
157       {
158         if ($this->getOption('prefix').$key == $info['info'])
159         {
160           return $info;
161         }
162       }
163     }
164
165     return null;
166   }
167 }
168
Note: See TracBrowser for help on using the browser.