Apache Zeta Components Manual :: File Source for memcache_backend.php

Source for file memcache_backend.php

Documentation is available at memcache_backend.php

  1. <?php
  2. /**
  3.  * File containing the ezcCacheMemcacheBackend class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package Cache
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27.  
  28. /**
  29.  * This backend stores data in a Memcache.
  30.  *
  31.  * @apichange This class will be deprecated in the next major version of the
  32.  *             Cache component. Please do not use it directly, but use {@link }
  33.  *             ezcCacheStorageMemcache} instead.
  34.  *
  35.  * @package Cache
  36.  * @version //autogentag//
  37.  */
  38. {
  39.     /**
  40.      * The compress threshold.
  41.      *
  42.      * Nearly 1MB (48,576B less).
  43.      */
  44.     const COMPRESS_THRESHOLD = 1000000;
  45.  
  46.     /**
  47.      * Maximum length of a cache key for Memcached.
  48.      */
  49.     const MAX_KEY_LENGTH = 249;
  50.  
  51.     /**
  52.      * Holds an instance to a Memcache object.
  53.      *
  54.      * @var resource 
  55.      */
  56.     protected $memcache;
  57.  
  58.     /**
  59.      * Holds the options for this class.
  60.      *
  61.      * @var ezcCacheStorageMemcacheOptions 
  62.      */
  63.     protected $options;
  64.  
  65.     /**
  66.      * Stores the connections to Memcached.
  67.      *
  68.      * @var array(string=>Memcache) 
  69.      */
  70.     protected static $connections array();
  71.  
  72.     /**
  73.      * Keeps track of the number of backends using the same connection.
  74.      *
  75.      * This is to avoid that the dtor of a backend accedentally closes a
  76.      * connection that is still in used by another backend.
  77.      *
  78.      * @var array(string=>int) 
  79.      */
  80.     protected static $connectionCounter array();
  81.  
  82.     /**
  83.      * Stores the connection identifier.
  84.      *
  85.      * This is generated in the ctor and used in the dtor.
  86.      * 
  87.      * @var string 
  88.      */
  89.     protected $connectionIdentifier;
  90.  
  91.     /**
  92.      * Constructs a new ezcCacheMemcacheBackend object.
  93.      *
  94.      * For options for this backend see {@link ezcCacheStorageMemcacheOptions}.
  95.      *
  96.      * @throws ezcBaseExtensionNotFoundException
  97.      *          If the PHP memcache and zlib extensions are not installed.
  98.      * @throws ezcCacheMemcacheException
  99.      *          If the connection to the Memcache host did not succeed.
  100.      *
  101.      * @param array(string=>mixed) $options 
  102.      */
  103.     public function __constructarray $options array() )
  104.     {
  105.         if !ezcBaseFeatures::hasExtensionSupport'memcache' ) )
  106.         {
  107.             throw new ezcBaseExtensionNotFoundException'memcache'null"PHP does not have Memcache support." );
  108.         }
  109.  
  110.         if !ezcBaseFeatures::hasExtensionSupport'zlib' ) )
  111.         {
  112.             throw new ezcBaseExtensionNotFoundException'zlib'null"PHP not configured with --with-zlib." );
  113.         }
  114.  
  115.         $this->options = new ezcCacheStorageMemcacheOptions$options );
  116.  
  117.         $this->connectionIdentifier = $this->options->host ':' $this->options->port;
  118.         if !issetself::$connections[$this->connectionIdentifier) )
  119.         {
  120.             self::$connections[$this->connectionIdentifier]     new Memcache();
  121.             // Currently 0 backends use the connection
  122.             self::$connectionCounter[$this->connectionIdentifier0;
  123.         }
  124.  
  125.         $this->memcache = self::$connections[$this->connectionIdentifier];
  126.         // Now 1 backend uses it
  127.         self::$connectionCounter[$this->connectionIdentifier]++;
  128.         if $this->options->persistent === true )
  129.         {
  130.             if !@$this->memcache->pconnect$this->options->host$this->options->port$this->options->ttl ) )
  131.             {
  132.                 throw new ezcCacheMemcacheException'Could not connect to Memcache using a persistent connection.' );
  133.             }
  134.         }
  135.         else
  136.         {
  137.             if !@$this->memcache->connect$this->options->host$this->options->port$this->options->ttl ) )
  138.             {
  139.                 throw new ezcCacheMemcacheException'Could not connect to Memcache.' );
  140.             }
  141.         }
  142.  
  143.         $this->memcache->setCompressThresholdself::COMPRESS_THRESHOLD );
  144.     }
  145.  
  146.     /**
  147.      * Destructor for the Memcache backend.
  148.      */
  149.     public function __destruct()
  150.     {
  151.         self::$connectionCounter[$this->connectionIdentifier]--;
  152.         // Save to ignore persistent connections, since close() does not affect them
  153.         if self::$connectionCounter[$this->connectionIdentifier=== )
  154.         {
  155.             $this->memcache->close();
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Adds the $var data to the cache under the key $key. Returns true or
  161.      * false depending on the success of the operation.
  162.      *
  163.      * @param string $key 
  164.      * @param mixed $var 
  165.      * @param int $expire 
  166.      * @return bool 
  167.      */
  168.     public function store$key$var$expire )
  169.     {
  170.         if strlen$key self::MAX_KEY_LENGTH )
  171.         {
  172.             throw new ezcCacheInvalidKeyException$key'Length > ' self::MAX_KEY_LENGTH '.' );
  173.         }
  174.  
  175.         // protect our data by wrapping it in an object
  176.         $data new ezcCacheMemoryVarStruct$key$var$expire );
  177.         $compressed $this->options->compressed === true MEMCACHE_COMPRESSED false;
  178.         return $this->memcache->set$key$data$compressed$expire );
  179.     }
  180.  
  181.     /**
  182.      * Returns the data from the cache associated with key $key.
  183.      *
  184.      * @param mixed $key 
  185.      * @return mixed 
  186.      */
  187.     public function fetch$key )
  188.     {
  189.         if strlen$key self::MAX_KEY_LENGTH )
  190.         {
  191.             throw new ezcCacheInvalidKeyException$key'Length > ' self::MAX_KEY_LENGTH '.' );
  192.         }
  193.  
  194.         $data $this->memcache->get$key );
  195.         return is_object$data ) ) $data->var false;
  196.     }
  197.  
  198.     /**
  199.      * Deletes the data from the cache associated with key $key. Returns true or
  200.      * false depending on the success of the operation.
  201.      *
  202.      * The value $timeout specifies the timeout period in seconds for the delete
  203.      * command.
  204.      *
  205.      * @param string $key 
  206.      * @param int $timeout 
  207.      * @return bool 
  208.      */
  209.     public function delete$key$timeout )
  210.     {
  211.         if strlen$key self::MAX_KEY_LENGTH )
  212.         {
  213.             throw new ezcCacheInvalidKeyException$key'Length > ' self::MAX_KEY_LENGTH '.' );
  214.         }
  215.  
  216.         return $this->memcache->delete$key$timeout );
  217.     }
  218.  
  219.     /**
  220.      * Resets the complete backend.
  221.      *
  222.      * Marked private to not expose more of this interface to the user, since
  223.      * this will be removed in future versions.
  224.      * 
  225.      * @return void 
  226.      * @access private
  227.      */
  228.     public function reset()
  229.     {
  230.         // Kills whole memcache content
  231.         $this->memcache->flush();
  232.     }
  233.  
  234.     /**
  235.      * Acquires a lock on the given $key.
  236.      *
  237.      * @param string $key 
  238.      * @param int $waitTime usleep()
  239.      * @param int $maxTime seconds
  240.      */
  241.     public function acquireLock$key$waitTime$maxTime )
  242.     {
  243.         if strlen$key self::MAX_KEY_LENGTH )
  244.         {
  245.             throw new ezcCacheInvalidKeyException$key'Length > ' self::MAX_KEY_LENGTH '.' );
  246.         }
  247.  
  248.         // add() does not replace and returns true on success. $maxTime is
  249.         // obeyed by Memcache expiry.
  250.         while $this->memcache->add$key$keyfalse$maxTime === false )
  251.         {
  252.             // Wait for next check
  253.             usleep$waitTime );
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Releases a lock on the given $key.
  259.      * 
  260.      * @param string $key 
  261.      * @return void 
  262.      */
  263.     public function releaseLock$key )
  264.     {
  265.         if strlen$key self::MAX_KEY_LENGTH )
  266.         {
  267.             throw new ezcCacheInvalidKeyException$key'Length > ' self::MAX_KEY_LENGTH '.' );
  268.         }
  269.  
  270.         $this->memcache->delete$key );
  271.     }
  272.  
  273. }
  274. ?>
Documentation generated by phpDocumentor 1.4.3