Development

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

You must first sign up to be able to contribute.

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

Revision 7605, 2.9 kB (checked in by fabien, 7 years ago)

reverted r7604

  • 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 = apc_fetch($this->getOption('prefix').$key);
46
47     return false === $value ? $default : $value;
48   }
49
50   /**
51    * @see sfCache
52    */
53   public function has($key)
54   {
55     return !(false === apc_fetch($this->getOption('prefix').$key));
56   }
57
58   /**
59    * @see sfCache
60    */
61   public function set($key, $data, $lifetime = null)
62   {
63     return apc_store($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
64   }
65
66   /**
67    * @see sfCache
68    */
69   public function remove($key)
70   {
71     return apc_delete($this->getOption('prefix').$key);
72   }
73
74   /**
75    * @see sfCache
76    */
77   public function clean($mode = sfCache::ALL)
78   {
79     if (sfCache::ALL === $mode)
80     {
81       return apc_clear_cache('user');
82     }
83   }
84
85   /**
86    * @see sfCache
87    */
88   public function getLastModified($key)
89   {
90     if ($info = $this->getCacheInfo($key))
91     {
92       return $info['creation_time'] + $info['ttl'] > time() ? $info['mtime'] : 0;
93     }
94
95     return 0;
96   }
97
98   /**
99    * @see sfCache
100    */
101   public function getTimeout($key)
102   {
103     if ($info = $this->getCacheInfo($key))
104     {
105       return $info['creation_time'] + $info['ttl'] > time() ? $info['creation_time'] + $info['ttl'] : 0;
106     }
107
108     return 0;
109   }
110
111   /**
112    * @see sfCache
113    */
114   public function removePattern($pattern)
115   {
116     $infos = apc_cache_info('user');
117     if (!is_array($infos['cache_list']))
118     {
119       return;
120     }
121
122     $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
123
124     foreach ($infos['cache_list'] as $info)
125     {
126       if (preg_match($regexp, $info['info']))
127       {
128         apc_delete($info['info']);
129       }
130     }
131   }
132
133   protected function getCacheInfo($key)
134   {
135     $infos = apc_cache_info('user');
136
137     if (is_array($infos['cache_list']))
138     {
139       foreach ($infos['cache_list'] as $info)
140       {
141         if ($this->getOption('prefix').$key == $info['info'])
142         {
143           return $info;
144         }
145       }
146     }
147
148     return null;
149   }
150 }
151
Note: See TracBrowser for help on using the browser.