Alpha Framework alpha--util--cache
[ class tree: alpha--util--cache ] [ index: alpha--util--cache ] [ all elements ]

Source for file AlphaCacheProviderMemcache.inc

Documentation is available at AlphaCacheProviderMemcache.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/util/cache/AlphaCacheProviderInterface.inc';
  4.  
  5. /**
  6.  *
  7.  * An implementation of the AlphaCacheProviderInterface interface that uses Memcache as the
  8.  * target store.
  9.  * 
  10.  * @package alpha::util::cache
  11.  * @since 1.1
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: AlphaCacheProviderMemcache.inc 1454 2011-12-04 15:14:05Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class AlphaCacheProviderMemcache implements AlphaCacheProviderInterface {
  52.     /**
  53.      * Trace logger
  54.      * 
  55.      * @var Logger 
  56.      * @since 1.1
  57.      */
  58.     private static $logger null;
  59.     
  60.     /**
  61.      * Constructor
  62.      * 
  63.      * @since 1.1
  64.      */
  65.     public function __construct({
  66.         self::$logger new Logger('AlphaCacheProviderMemcache');
  67.     }
  68.     
  69.     /**
  70.      * (non-PHPdoc)
  71.      * @see alpha/util/cache/AlphaCacheProviderInterface::get()
  72.      * @since 1.1
  73.      */
  74.     public function get($key{
  75.         self::$logger->debug('>>get(key=['.$key.'])');
  76.         
  77.         global $config;
  78.  
  79.           try {
  80.               $memcache new Memcache();
  81.               $memcache->connect($config->get('sysMemcachedHost')11211);
  82.     
  83.               $value $memcache->get($key);
  84.               
  85.               self::$logger->debug('<<get: ['.print_r($valuetrue).'])');
  86.               return $value;
  87.           }catch(Exception $e{
  88.               self::$logger->error('Error while attempting to load a business object from Memcached instance: ['.$e->getMessage().']');
  89.               self::$logger->debug('<<get: [false])');
  90.             return false;
  91.           }
  92.     }
  93.     
  94.     /**
  95.      * (non-PHPdoc)
  96.      * @see alpha/util/cache/AlphaCacheProviderInterface::set()
  97.      * @since 1.1
  98.      */
  99.     public function set($key$value$expiry=0{
  100.         global $config;
  101.         
  102.         try {
  103.               $memcache new Memcache();
  104.             $memcache->connect($config->get('sysMemcachedHost')11211);
  105.         
  106.             if($expiry 0)
  107.                 $memcache->set($key$valueMEMCACHE_COMPRESSED$expiry);
  108.             else
  109.                 $memcache->set($key$valueMEMCACHE_COMPRESSED);
  110.  
  111.           }catch(Exception $e{
  112.               self::$logger->error('Error while attempting to store a value to Memcached instance: ['.$e->getMessage().']');
  113.           }
  114.     }
  115.     
  116.     /**
  117.      * (non-PHPdoc)
  118.      * @see alpha/util/cache/AlphaCacheProviderInterface::delete()
  119.      * @since 1.1
  120.      */
  121.     public function delete($key{
  122.         global $config;
  123.  
  124.         try {
  125.               $memcache new Memcache();
  126.               $memcache->connect($config->get('sysMemcachedHost')11211);
  127.               $memcache->delete($key);
  128.         }catch(Exception $e{
  129.               self::$logger->error('Error while attempting to remove a value from Memcached instance: ['.$e->getMessage().']');
  130.           }
  131.     }
  132. }

Documentation generated on Tue, 13 Dec 2011 20:25:32 +0000 by phpDocumentor 1.4.3