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 Xcache.
  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_Xcache 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:         $result = xcache_get($key);
 40: 
 41:         return empty($result)
 42:             ? false
 43:             : $result;
 44:     }
 45: 
 46:     /**
 47:      */
 48:     public function set($key, $data, $lifetime = 0)
 49:     {
 50:         $key = $this->_params['prefix'] . $key;
 51:         if (xcache_set($key . '_expire', time(), $lifetime)) {
 52:             xcache_set($key, $data, $lifetime);
 53:         }
 54:     }
 55: 
 56:     /**
 57:      */
 58:     public function exists($key, $lifetime = 0)
 59:     {
 60:         $key = $this->_params['prefix'] . $key;
 61:         $this->_setExpire($key, $lifetime);
 62:         return xcache_isset($key);
 63:     }
 64: 
 65:     /**
 66:      */
 67:     public function expire($key)
 68:     {
 69:         $key = $this->_params['prefix'] . $key;
 70:         xcache_unset($key . '_expire');
 71:         return xcache_unset($key);
 72:     }
 73: 
 74:     /**
 75:      */
 76:     public function clear()
 77:     {
 78:         // xcache_clear_cache() won't work because it requires HTTP Auth.
 79:         throw new Horde_Cache_Exception('Not supported');
 80:     }
 81: 
 82:     /**
 83:      * Set expire time on each call since memcache sets it on cache creation.
 84:      *
 85:      * @param string $key        Cache key to expire.
 86:      * @param integer $lifetime  Lifetime of the data in seconds.
 87:      */
 88:     protected function _setExpire($key, $lifetime)
 89:     {
 90:         if ($lifetime == 0) {
 91:             // don't expire
 92:             return;
 93:         }
 94:         $key = $this->_params['prefix'] . $key;
 95:         $expire = xcache_get($key . '_expire');
 96: 
 97:         // set prune period
 98:         if ($expire + $lifetime < time()) {
 99:             // Expired
100:             xcache_unset($key . '_expire');
101:             xcache_unset($key);
102:         }
103:     }
104: 
105: }
106: 
API documentation generated by ApiGen