Overview

Packages

  • Cache

Classes

  • Horde_Cache
  • Horde_Cache_Exception
  • Horde_Cache_Storage_Apc
  • Horde_Cache_Storage_Base
  • Horde_Cache_Storage_Eaccelerator
  • Horde_Cache_Storage_File
  • Horde_Cache_Storage_Memcache
  • Horde_Cache_Storage_Mock
  • Horde_Cache_Storage_Null
  • Horde_Cache_Storage_Session
  • Horde_Cache_Storage_Sql
  • Horde_Cache_Storage_Stack
  • Horde_Cache_Storage_Xcache
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * This class provides cache storage in the Alternative PHP Cache.
  4:  *
  5:  * Copyright 2006-2007 Duck <duck@obala.net>
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Duck <duck@obala.net>
 11:  * @category Horde
 12:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 13:  * @package  Cache
 14:  */
 15: class Horde_Cache_Storage_Apc extends Horde_Cache_Storage_Base
 16: {
 17:     /**
 18:      * Constructor.
 19:      *
 20:      * @param array $params  Optional parameters:
 21:      * <pre>
 22:      * 'prefix' - (string) The prefix to use for the cache keys.
 23:      *            DEFAULT: ''
 24:      * </pre>
 25:      */
 26:     public function __construct(array $params = array())
 27:     {
 28:         parent::__construct(array_merge(array(
 29:             'prefix' => '',
 30:         ), $params));
 31:     }
 32: 
 33:     /**
 34:      */
 35:     public function get($key, $lifetime = 0)
 36:     {
 37:         $key = $this->_params['prefix'] . $key;
 38:         $this->_setExpire($key, $lifetime);
 39:         return apc_fetch($key);
 40:     }
 41: 
 42:     /**
 43:      */
 44:     public function set($key, $data, $lifetime = 0)
 45:     {
 46:         $key = $this->_params['prefix'] . $key;
 47:         if (apc_store($key . '_expire', time(), $lifetime)) {
 48:             apc_store($key, $data, $lifetime);
 49:         }
 50:     }
 51: 
 52:     /**
 53:      */
 54:     public function exists($key, $lifetime = 0)
 55:     {
 56:         $key = $this->_params['prefix'] . $key;
 57:         $this->_setExpire($key, $lifetime);
 58:         return (apc_fetch($key) !== false);
 59:     }
 60: 
 61:     /**
 62:      */
 63:     public function expire($key)
 64:     {
 65:         $key = $this->_params['prefix'] . $key;
 66:         apc_delete($key . '_expire');
 67:         return apc_delete($key);
 68:     }
 69: 
 70:     /**
 71:      */
 72:     public function clear()
 73:     {
 74:         if (!apc_clear_cache('user')) {
 75:             throw new Horde_Cache_Exception('Clearing APC cache failed');
 76:         }
 77:     }
 78: 
 79:     /**
 80:      * Set expire time on each call since APC sets it on cache creation.
 81:      *
 82:      * @param string $key        Cache key to expire.
 83:      * @param integer $lifetime  Lifetime of the data in seconds.
 84:      */
 85:     protected function _setExpire($key, $lifetime)
 86:     {
 87:         $key = $this->_params['prefix'] . $key;
 88:         if ($lifetime == 0) {
 89:             // Don't expire.
 90:             return;
 91:         }
 92: 
 93:         $expire = apc_fetch($key . '_expire');
 94: 
 95:         // Set prune period.
 96:         if ($expire + $lifetime < time()) {
 97:             // Expired
 98:             apc_delete($key);
 99:             apc_delete($key . '_expire');
100:         }
101:     }
102: 
103: }
104: 
API documentation generated by ApiGen