PeclMemcached.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: PeclMemcached.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00021     final class PeclMemcached extends CachePeer
00022     {
00023         const DEFAULT_PORT      = 11211;
00024         const DEFAULT_HOST      = '127.0.0.1';
00025         
00026         private $instance = null;
00027         
00031         public static function create(
00032             $host = Memcached::DEFAULT_HOST,
00033             $port = Memcached::DEFAULT_PORT
00034         )
00035         {
00036             return new self($host, $port);
00037         }
00038         
00039         public function __construct(
00040             $host = Memcached::DEFAULT_HOST,
00041             $port = Memcached::DEFAULT_PORT
00042         )
00043         {
00044             $this->instance = new Memcache();
00045             
00046             try {
00047                 try {
00048                     $this->instance->pconnect($host, $port);
00049                 } catch (BaseException $e) {
00050                     $this->instance->connect($host, $port);
00051                 }
00052                 
00053                 $this->alive = true;
00054             } catch (BaseException $e) {
00055                 // bad luck.
00056             }
00057         }
00058         
00059         public function __destruct()
00060         {
00061             if ($this->alive) {
00062                 try {
00063                     $this->instance->close();
00064                 } catch (BaseException $e) {
00065                     // shhhh.
00066                 }
00067             }
00068         }
00069         
00073         public function clean()
00074         {
00075             try {
00076                 $this->instance->flush();
00077             } catch (BaseException $e) {
00078                 $this->alive = false;
00079             }
00080             
00081             return parent::clean();
00082         }
00083         
00084         public function get($index)
00085         {
00086             try {
00087                 return $this->instance->get($index);
00088             } catch (BaseException $e) {
00089                 $this->alive = false;
00090                 
00091                 return null;
00092             }
00093             
00094             Assert::isUnreachable();
00095         }
00096         
00097         public function delete($index)
00098         {
00099             try {
00100                 return $this->instance->delete($index);
00101             } catch (BaseException $e) {
00102                 return $this->alive = false;
00103             }
00104             
00105             Assert::isUnreachable();
00106         }
00107         
00108         protected function store(
00109             $action, $key, &$value, $expires = Cache::EXPIRES_MEDIUM
00110         )
00111         {
00112             try {
00113                 return
00114                     $this->instance->$action(
00115                         $key,
00116                         $value,
00117                         $this->compress
00118                             ? MEMCACHE_COMPRESSED
00119                             : false,
00120                         $expires
00121                     );
00122             } catch (BaseException $e) {
00123                 return $this->alive = false;
00124             }
00125             
00126             Assert::isUnreachable();
00127         }
00128     }
00129 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4