vendor/doctrine-common/lib/Doctrine/Common/Cache/MemcacheCache.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 /*
       
     3  *  $Id$
       
     4  *
       
     5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
     6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
     7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
     8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
     9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    16  *
       
    17  * This software consists of voluntary contributions made by many individuals
       
    18  * and is licensed under the LGPL. For more information, see
       
    19  * <http://www.doctrine-project.org>.
       
    20  */
       
    21 
       
    22 namespace Doctrine\Common\Cache;
       
    23 
       
    24 use \Memcache;
       
    25 
       
    26 /**
       
    27  * Memcache cache driver.
       
    28  *
       
    29  * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
       
    30  * @link    www.doctrine-project.org
       
    31  * @since   2.0
       
    32  * @version $Revision: 3938 $
       
    33  * @author  Benjamin Eberlei <kontakt@beberlei.de>
       
    34  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
       
    35  * @author  Jonathan Wage <jonwage@gmail.com>
       
    36  * @author  Roman Borschel <roman@code-factory.org>
       
    37  * @author  David Abdemoulaie <dave@hobodave.com>
       
    38  */
       
    39 class MemcacheCache extends AbstractCache
       
    40 {
       
    41     /**
       
    42      * @var Memcache
       
    43      */
       
    44     private $_memcache;
       
    45 
       
    46     /**
       
    47      * Sets the memcache instance to use.
       
    48      *
       
    49      * @param Memcache $memcache
       
    50      */
       
    51     public function setMemcache(Memcache $memcache)
       
    52     {
       
    53         $this->_memcache = $memcache;
       
    54     }
       
    55 
       
    56     /**
       
    57      * Gets the memcache instance used by the cache.
       
    58      *
       
    59      * @return Memcache
       
    60      */
       
    61     public function getMemcache()
       
    62     {
       
    63         return $this->_memcache;
       
    64     }
       
    65 
       
    66     /**
       
    67      * {@inheritdoc}
       
    68      */
       
    69     public function getIds()
       
    70     {
       
    71         $keys = array();
       
    72         $allSlabs = $this->_memcache->getExtendedStats('slabs');
       
    73 
       
    74         foreach ($allSlabs as $server => $slabs) {
       
    75             if (is_array($slabs)) {
       
    76                 foreach (array_keys($slabs) as $slabId) {
       
    77                     $dump = $this->_memcache->getExtendedStats('cachedump', (int) $slabId);
       
    78 
       
    79                     if ($dump) {
       
    80                         foreach ($dump as $entries) {
       
    81                             if ($entries) {
       
    82                                 $keys = array_merge($keys, array_keys($entries));
       
    83                             }
       
    84                         }
       
    85                     }
       
    86                 }
       
    87             }
       
    88         }
       
    89         return $keys;
       
    90     }
       
    91 
       
    92     /**
       
    93      * {@inheritdoc}
       
    94      */
       
    95     protected function _doFetch($id)
       
    96     {
       
    97         return $this->_memcache->get($id);
       
    98     }
       
    99 
       
   100     /**
       
   101      * {@inheritdoc}
       
   102      */
       
   103     protected function _doContains($id)
       
   104     {
       
   105         return (bool) $this->_memcache->get($id);
       
   106     }
       
   107 
       
   108     /**
       
   109      * {@inheritdoc}
       
   110      */
       
   111     protected function _doSave($id, $data, $lifeTime = 0)
       
   112     {
       
   113         return $this->_memcache->set($id, $data, 0, (int) $lifeTime);
       
   114     }
       
   115 
       
   116     /**
       
   117      * {@inheritdoc}
       
   118      */
       
   119     protected function _doDelete($id)
       
   120     {
       
   121         return $this->_memcache->delete($id);
       
   122     }
       
   123 }