123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- class sfAPCCache extends sfCache
- {
-
- public function initialize($options = array())
- {
- parent::initialize($options);
- if (!function_exists('apc_store') || !ini_get('apc.enabled'))
- {
- throw new sfInitializationException('You must have APC installed and enabled to use sfAPCCache class.');
- }
- }
-
- public function get($key, $default = null)
- {
- $value = apc_fetch($this->getOption('prefix').$key);
- return false === $value ? $default : $value;
- }
-
- public function has($key)
- {
- return !(false === apc_fetch($this->getOption('prefix').$key));
- }
-
- public function set($key, $data, $lifetime = null)
- {
- return apc_store($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
- }
-
- public function remove($key)
- {
- return apc_delete($this->getOption('prefix').$key);
- }
-
- public function clean($mode = sfCache::ALL)
- {
- if (sfCache::ALL === $mode)
- {
- return apc_clear_cache('user');
- }
- }
-
- public function getLastModified($key)
- {
- if ($info = $this->getCacheInfo($key))
- {
- return $info['creation_time'] + $info['ttl'] > time() ? $info['mtime'] : 0;
- }
- return 0;
- }
-
- public function getTimeout($key)
- {
- if ($info = $this->getCacheInfo($key))
- {
- return $info['creation_time'] + $info['ttl'] > time() ? $info['creation_time'] + $info['ttl'] : 0;
- }
- return 0;
- }
-
- public function removePattern($pattern)
- {
- $infos = apc_cache_info('user');
- if (!is_array($infos['cache_list']))
- {
- return;
- }
- $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
- foreach ($infos['cache_list'] as $info)
- {
- if (preg_match($regexp, $info['info']))
- {
- apc_delete($info['info']);
- }
- }
- }
- protected function getCacheInfo($key)
- {
- $infos = apc_cache_info('user');
- if (is_array($infos['cache_list']))
- {
- foreach ($infos['cache_list'] as $info)
- {
- if ($this->getOption('prefix').$key == $info['info'])
- {
- return $info;
- }
- }
- }
- return null;
- }
- }
|