PeclMemcached.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2008 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 
00020     final class PeclMemcached extends CachePeer
00021     {
00022         const DEFAULT_PORT      = 11211;
00023         const DEFAULT_HOST      = '127.0.0.1';
00024         
00025         private $instance = null;
00026         
00030         public static function create(
00031             $host = Memcached::DEFAULT_HOST,
00032             $port = Memcached::DEFAULT_PORT
00033         )
00034         {
00035             return new self($host, $port);
00036         }
00037         
00038         public function __construct(
00039             $host = Memcached::DEFAULT_HOST,
00040             $port = Memcached::DEFAULT_PORT
00041         )
00042         {
00043             $this->instance = new Memcache();
00044             
00045             try {
00046                 try {
00047                     $this->instance->pconnect($host, $port);
00048                 } catch (BaseException $e) {
00049                     $this->instance->connect($host, $port);
00050                 }
00051                 
00052                 $this->alive = true;
00053             } catch (BaseException $e) {
00054                 // bad luck.
00055             }
00056         }
00057         
00058         public function __destruct()
00059         {
00060             if ($this->alive) {
00061                 try {
00062                     $this->instance->close();
00063                 } catch (BaseException $e) {
00064                     // shhhh.
00065                 }
00066             }
00067         }
00068         
00072         public function clean()
00073         {
00074             try {
00075                 $this->instance->flush();
00076             } catch (BaseException $e) {
00077                 $this->alive = false;
00078             }
00079             
00080             return parent::clean();
00081         }
00082         
00083         public function increment($key, $value)
00084         {
00085             try {
00086                 return $this->instance->increment($key, $value);
00087             } catch (BaseException $e) {
00088                 return null;
00089             }
00090         }
00091         
00092         public function decrement($key, $value)
00093         {
00094             try {
00095                 return $this->instance->decrement($key, $value);
00096             } catch (BaseException $e) {
00097                 return null;
00098             }
00099         }
00100         
00101         public function getList($indexes)
00102         {
00103             return
00104                 ($return = $this->get($indexes))
00105                     ? $return
00106                     : array();
00107         }
00108         
00109         public function get($index)
00110         {
00111             try {
00112                 return $this->instance->get($index);
00113             } catch (BaseException $e) {
00114                 $this->alive = false;
00115                 
00116                 return null;
00117             }
00118             
00119             Assert::isUnreachable();
00120         }
00121         
00122         public function delete($index)
00123         {
00124             try {
00125                 // second parameter required, wrt new memcached protocol:
00126                 // delete key 0 (see process_delete_command in the memcached.c)
00127                 // Warning: it is workaround!
00128                 return $this->instance->delete($index, 0);
00129             } catch (BaseException $e) {
00130                 return $this->alive = false;
00131             }
00132             
00133             Assert::isUnreachable();
00134         }
00135         
00136         public function append($key, $data)
00137         {
00138             try {
00139                 return $this->instance->append($key, $data);
00140             } catch (BaseException $e) {
00141                 return $this->alive = false;
00142             }
00143             
00144             Assert::isUnreachable();
00145         }
00146         
00147         protected function store(
00148             $action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
00149         )
00150         {
00151             try {
00152                 return
00153                     $this->instance->$action(
00154                         $key,
00155                         $value,
00156                         $this->compress
00157                             ? MEMCACHE_COMPRESSED
00158                             : false,
00159                         $expires
00160                     );
00161             } catch (BaseException $e) {
00162                 return $this->alive = false;
00163             }
00164             
00165             Assert::isUnreachable();
00166         }
00167     }
00168 ?>