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 a memcache installation.
  4:  *
  5:  * Copyright 2006-2007 Duck <duck@obala.net>
  6:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (LGPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 10:  *
 11:  * @author   Duck <duck@obala.net>
 12:  * @author   Michael Slusarz <slusarz@horde.org>
 13:  * @category Horde
 14:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 15:  * @package  Cache
 16:  */
 17: class Horde_Cache_Storage_Memcache extends Horde_Cache_Storage_Base implements Serializable
 18: {
 19:     /**
 20:      * Cache results of expire() calls (since we will get the entire object
 21:      * on an expire() call anyway).
 22:      *
 23:      * @var array
 24:      */
 25:     protected $_expirecache = array();
 26: 
 27:     /**
 28:      * Memcache object.
 29:      *
 30:      * @var Horde_Memcache
 31:      */
 32:     protected $_memcache;
 33: 
 34:     /**
 35:      * Construct a new Horde_Cache_Memcache object.
 36:      *
 37:      * @param array $params  Parameter array:
 38:      * <pre>
 39:      * 'memcache' - (Horde_Memcache) [REQUIRED] A Horde_Memcache object.
 40:      * 'prefix' - (string) The prefix to use for the cache keys.
 41:      *            DEFAULT: ''
 42:      * </pre>
 43:      *
 44:      * @throws InvalidArgumentException
 45:      */
 46:     public function __construct(array $params = array())
 47:     {
 48:         if (!isset($params['memcache'])) {
 49:             throw new InvalidArgumentException('Missing memcache object');
 50:         }
 51: 
 52:         $this->_memcache = $params['memcache'];
 53:         unset($params['memcache']);
 54: 
 55:         parent::__construct(array_merge(array(
 56:             'prefix' => '',
 57:         ), $params));
 58:     }
 59: 
 60:     /**
 61:      */
 62:     public function get($key, $lifetime = 0)
 63:     {
 64:         $original_key = $key;
 65:         $key = $this->_params['prefix'] . $key;
 66:         if (isset($this->_expirecache[$key])) {
 67:             return $this->_expirecache[$key];
 68:         }
 69: 
 70:         $key_list = array($key);
 71:         if (!empty($lifetime)) {
 72:             $key_list[] = $key . '_e';
 73:         }
 74: 
 75:         $res = $this->_memcache->get($key_list);
 76: 
 77:         if ($res === false) {
 78:             unset($this->_expirecache[$key]);
 79:         } else {
 80:             // If we can't find the expire time, assume we have exceeded it.
 81:             if (empty($lifetime) ||
 82:                 (($res[$key . '_e'] !== false) && ($res[$key . '_e'] + $lifetime > time()))) {
 83:                 $this->_expirecache[$key] = $res[$key];
 84:             } else {
 85:                 $res[$key] = false;
 86:                 $this->expire($original_key);
 87:             }
 88:         }
 89: 
 90:         return $res[$key];
 91:     }
 92: 
 93:     /**
 94:      */
 95:     public function set($key, $data, $lifetime = 0)
 96:     {
 97:         $key = $this->_params['prefix'] . $key;
 98: 
 99:         if ($this->_memcache->set($key . '_e', time(), $lifetime) !== false) {
100:             $this->_memcache->set($key, $data, $lifetime);
101:         }
102:     }
103: 
104:     /**
105:      */
106:     public function exists($key, $lifetime = 0)
107:     {
108:         return ($this->get($key, $lifetime) !== false);
109:     }
110: 
111:     /**
112:      */
113:     public function expire($key)
114:     {
115:         $key = $this->_params['prefix'] . $key;
116:         unset($this->_expirecache[$key]);
117:         $this->_memcache->delete($key . '_e');
118: 
119:         return $this->_memcache->delete($key);
120:     }
121: 
122:     /**
123:      */
124:     public function clear()
125:     {
126:         $this->_memcache->flush();
127:     }
128: 
129:     /* Serializable methods. */
130: 
131:     /**
132:      */
133:     public function serialize()
134:     {
135:         return serialize(array(
136:             $this->_memcache,
137:             $this->_params
138:         ));
139:     }
140: 
141:     /**
142:      */
143:     public function unserialize($data)
144:     {
145:         list($this->_memcache, $this->_params) = unserialize($data);
146:     }
147: 
148: }
149: 
API documentation generated by ApiGen