Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • ElggMemcache
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Memcache wrapper class.
  4:  *
  5:  * @package    Elgg.Core
  6:  * @subpackage Memcache
  7:  */
  8: class ElggMemcache extends ElggSharedMemoryCache {
  9:     /**
 10:      * Minimum version of memcached needed to run
 11:      *
 12:      */
 13:     private static $MINSERVERVERSION = '1.1.12';
 14: 
 15:     /**
 16:      * Memcache object
 17:      */
 18:     private $memcache;
 19: 
 20:     /**
 21:      * Expiry of saved items (default timeout after a day to prevent anything getting too stale)
 22:      */
 23:     private $expires = 86400;
 24: 
 25:     /**
 26:      * The version of memcache running
 27:      */
 28:     private $version = 0;
 29: 
 30:     /**
 31:      * Connect to memcache.
 32:      *
 33:      * @param string $namespace The namespace for this cache to write to -
 34:      * note, namespaces of the same name are shared!
 35:      *
 36:      * @throws ConfigurationException
 37:      */
 38:     function __construct($namespace = 'default') {
 39:         global $CONFIG;
 40: 
 41:         $this->setNamespace($namespace);
 42: 
 43:         // Do we have memcache?
 44:         if (!class_exists('Memcache')) {
 45:             throw new ConfigurationException('PHP memcache module not installed, you must install php5-memcache');
 46:         }
 47: 
 48:         // Create memcache object
 49:         $this->memcache = new Memcache;
 50: 
 51:         // Now add servers
 52:         if (!$CONFIG->memcache_servers) {
 53:             throw new ConfigurationException('No memcache servers defined, please populate the $CONFIG->memcache_servers variable');
 54:         }
 55: 
 56:         if (is_callable(array($this->memcache, 'addServer'))) {
 57:             foreach ($CONFIG->memcache_servers as $server) {
 58:                 if (is_array($server)) {
 59:                     $this->memcache->addServer(
 60:                         $server[0],
 61:                         isset($server[1]) ? $server[1] : 11211,
 62:                         isset($server[2]) ? $server[2] : FALSE,
 63:                         isset($server[3]) ? $server[3] : 1,
 64:                         isset($server[4]) ? $server[4] : 1,
 65:                         isset($server[5]) ? $server[5] : 15,
 66:                         isset($server[6]) ? $server[6] : TRUE
 67:                     );
 68: 
 69:                 } else {
 70:                     $this->memcache->addServer($server, 11211);
 71:                 }
 72:             }
 73:         } else {
 74:             // don't use elgg_echo() here because most of the config hasn't been loaded yet
 75:             // and it caches the language, which is hard coded in $CONFIG->language as en.
 76:             // overriding it with real values later has no effect because it's already cached.
 77:             elgg_log("This version of the PHP memcache API doesn't support multiple servers.", 'ERROR');
 78: 
 79:             $server = $CONFIG->memcache_servers[0];
 80:             if (is_array($server)) {
 81:                 $this->memcache->connect($server[0], $server[1]);
 82:             } else {
 83:                 $this->memcache->addServer($server, 11211);
 84:             }
 85:         }
 86: 
 87:         // Get version
 88:         $this->version = $this->memcache->getVersion();
 89:         if (version_compare($this->version, ElggMemcache::$MINSERVERVERSION, '<')) {
 90:             $msg = vsprintf('Memcache needs at least version %s to run, you are running %s',
 91:                 array(ElggMemcache::$MINSERVERVERSION,
 92:                 $this->version
 93:             ));
 94: 
 95:             throw new ConfigurationException($msg);
 96:         }
 97: 
 98:         // Set some defaults
 99:         if (isset($CONFIG->memcache_expires)) {
100:             $this->expires = $CONFIG->memcache_expires;
101:         }
102:     }
103: 
104:     /**
105:      * Set the default expiry.
106:      *
107:      * @param int $expires The lifetime as a unix timestamp or time from now. Defaults forever.
108:      *
109:      * @return void
110:      */
111:     public function setDefaultExpiry($expires = 0) {
112:         $this->expires = $expires;
113:     }
114: 
115:     /**
116:      * Combine a key with the namespace.
117:      * Memcache can only accept <250 char key. If the given key is too long it is shortened.
118:      *
119:      * @param string $key The key
120:      *
121:      * @return string The new key.
122:      */
123:     private function makeMemcacheKey($key) {
124:         $prefix = $this->getNamespace() . ":";
125: 
126:         if (strlen($prefix . $key) > 250) {
127:             $key = md5($key);
128:         }
129: 
130:         return $prefix . $key;
131:     }
132: 
133:     /**
134:      * Saves a name and value to the cache
135:      *
136:      * @param string  $key     Name
137:      * @param string  $data    Value
138:      * @param integer $expires Expires (in seconds)
139:      *
140:      * @return bool
141:      */
142:     public function save($key, $data, $expires = null) {
143:         $key = $this->makeMemcacheKey($key);
144: 
145:         if ($expires === null) {
146:             $expires = $this->expires;
147:         }
148: 
149:         $result = $this->memcache->set($key, $data, null, $expires);
150:         if ($result === false) {
151:             elgg_log("MEMCACHE: FAILED TO SAVE $key", 'ERROR');
152:         }
153: 
154:         return $result;
155:     }
156: 
157:     /**
158:      * Retrieves data.
159:      *
160:      * @param string $key    Name of data to retrieve
161:      * @param int    $offset Offset
162:      * @param int    $limit  Limit
163:      *
164:      * @return mixed
165:      */
166:     public function load($key, $offset = 0, $limit = null) {
167:         $key = $this->makeMemcacheKey($key);
168: 
169:         $result = $this->memcache->get($key);
170:         if ($result === false) {
171:             elgg_log("MEMCACHE: FAILED TO LOAD $key", 'ERROR');
172:         }
173: 
174:         return $result;
175:     }
176: 
177:     /**
178:      * Delete data
179:      *
180:      * @param string $key Name of data
181:      *
182:      * @return bool
183:      */
184:     public function delete($key) {
185:         $key = $this->makeMemcacheKey($key);
186: 
187:         return $this->memcache->delete($key, 0);
188:     }
189: 
190:     /**
191:      * Clears the entire cache?
192:      *
193:      * @todo write or remove.
194:      *
195:      * @return true
196:      */
197:     public function clear() {
198:         // DISABLE clearing for now - you must use delete on a specific key.
199:         return true;
200: 
201:         // @todo Namespaces as in #532
202:     }
203: }
204: 
API documentation generated by ApiGen 2.8.0