PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_MyMemCache.php

Documentation is available at PMO_MyMemCache.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_MyMemCache class
  4.  *
  5.  * This file is part of the PhpMyObject project.
  6.  * 
  7.  * For questions, help, comments, discussion, etc., please join our
  8.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  9.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  10.  *
  11.  * PhpMyObject is free software: you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation, either version 3 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23.  *
  24.  * @package    PhpMyObject
  25.  * @subpackage PMO_Core
  26.  * @author     Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  27.  * @link       http://pmo.developpez.com/
  28.  * @since      PhpMyObject v0.1x
  29.  * @version    $Revision: $
  30.  * @copyright  Copyright (C) 2007-2008 Nicolas Boiteux
  31.  * @license    GPLv3 {@link http://www.gnu.org/licenses/gpl}
  32.  * @filesource
  33.  *
  34.  */ 
  35.  
  36. /** requires the interface */
  37. require_once(dirname(__FILE__).'/PMO_MemCache.php');
  38.  
  39. /**
  40.  * this class provides for memcaching PMO_Objects
  41.  *
  42.  * @package     PhpMyObject
  43.  * @subpackage PMO_Core
  44.  * @todo         make a tutorial to document this with usage and memcache installation
  45.  */
  46. class PMO_MyMemCache implements PMO_MemCache{
  47.  
  48.     /**
  49.      * holds the PMO_MyMemcache object
  50.      * @var object 
  51.      * @static
  52.      */
  53.     protected static $INSTANCE;
  54.  
  55.     /**
  56.      * holds the Memcache object
  57.      * @var object 
  58.      */
  59.     protected $memcache;
  60.  
  61.     /**
  62.      * instanciates the class and stores it to $this->memcache
  63.      */
  64.     private function __construct(){
  65.         $this->memcache = new Memcache();
  66.     }
  67.  
  68.     /**
  69.      * return the current instance of PMO_MyMemCache
  70.      *
  71.      * the instance will be created on the first call to factory()
  72.      *
  73.      * @return object             the PMO_MuMecache instance
  74.      */
  75.     public function factory(){
  76.         if(!isset(self::$INSTANCE))
  77.             self::$INSTANCE new PMO_MyMemCache();
  78.         
  79.         return self::$INSTANCE;        
  80.     }
  81.  
  82.     /**
  83.      * establishes a connection with the memcache server
  84.      *
  85.      * parameters are taken from your {@link PMO_MyConfig} configuration
  86.      *
  87.      * @return bool                 TRUE if the connection succeeded
  88.      * @throw Exception             if the connection could not be established
  89.      */
  90.     public function connect(){
  91.         if(!$this->memcache->connect(PMO_MyConfig::factory()->get('PMO_MyMemCache.HOST'),PMO_MyConfig::factory()->get('PMO_MyMemCache.PORT')))
  92.             throw new Exception("Error: connexion to memcache failed!");
  93.             
  94.         return TRUE;
  95.     }
  96.  
  97.     /**
  98.      * closes the connection to the memcache server
  99.      */
  100.     public function close(){
  101.         $this->memcache->close();
  102.     }
  103.  
  104.     /**
  105.      * loads a PMO_Object from the cache
  106.      *
  107.      * @param object $object         the PMO_Object to load
  108.      * @return PMO_Object 
  109.      */
  110.     public function get(PMO_Object $object){
  111.         $sign sha1($object);
  112.         return $this->memcache->get($sign);
  113.     }
  114.     
  115.     /**
  116.      * replaces an existing PMO_Object into the cache
  117.      *
  118.      * @param object $object         the PMO_Object to replace with
  119.      */
  120.     public function replace(PMO_Object $object){
  121.         $sign sha1($object);
  122.         $this->memcache->replace($sign$object);
  123.     }
  124.  
  125.     /**
  126.      * inserts a new PMO_Object into the cache
  127.      *
  128.      * @param object $object         the PMO_Object to insert
  129.      */
  130.     public function set($keyPMO_Object $object){
  131.         $sign sha1($object);
  132.         $this->memcache->set($sign$objectPMO_MyConfig::factory()->get('PMO_MyMemCache.TIMEOUT'));
  133.     }
  134.  
  135.     /**
  136.      * deletes a PMO_Object from the cache
  137.      *
  138.      * @param object $object         the PMO_Object to be deleted
  139.      */
  140.     public function delete(PMO_Object $object){
  141.         $sign sha1($object);
  142.         $this->memcache->delete($sign0);
  143.     }
  144.     
  145.     /**
  146.      * invalidates all objects from the cache
  147.      */
  148.     public function flush(){
  149.         $this->memcache->flush();
  150.     }
  151.  
  152. }
  153.  
  154. ?>

Documentation generated on Wed, 15 Oct 2008 09:17:21 -0400 by phpDocumentor 1.4.1