VoodooDaoWorker.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 
00029     final class VoodooDaoWorker extends TransparentDaoWorker
00030     {
00031         private $classKey = null;
00032         private $handler = null;
00033         
00034         // will trigger auto-detect
00035         private static $defaultHandler = null;
00036         
00037         public static function setDefaultHandler($handler)
00038         {
00039             Assert::classExists($handler);
00040             
00041             self::$defaultHandler = $handler;
00042         }
00043         
00044         public function __construct(GenericDAO $dao)
00045         {
00046             parent::__construct($dao);
00047             
00048             if (($cache = Cache::me()) instanceof WatermarkedPeer)
00049                 $watermark = $cache->mark($this->className)->getActualWatermark();
00050             else
00051                 $watermark = null;
00052             
00053             $this->classKey = $this->keyToInt($watermark.$this->className);
00054             
00055             $this->handler = $this->spawnHandler($this->classKey);
00056         }
00057         
00059 
00060         protected function cacheByQuery(
00061             SelectQuery $query,
00062             /* Identifiable */ $object,
00063             $expires = Cache::EXPIRES_FOREVER
00064         )
00065         {
00066             $key = $this->makeQueryKey($query, self::SUFFIX_QUERY);
00067             
00068             if ($this->handler->touch($this->keyToInt($key)))
00069                 Cache::me()->mark($this->className)->
00070                     add($key, $object, $expires);
00071             
00072             return $object;
00073         }
00074         
00075         protected function cacheListByQuery(
00076             SelectQuery $query,
00077             /* array || Cache::NOT_FOUND */ $array
00078         )
00079         {
00080             if ($array !== Cache::NOT_FOUND) {
00081                 Assert::isArray($array);
00082                 Assert::isTrue(current($array) instanceof Identifiable);
00083             }
00084             
00085             $cache = Cache::me();
00086             
00087             $key = $this->makeQueryKey($query, self::SUFFIX_LIST);
00088             
00089             if ($this->handler->touch($this->keyToInt($key))) {
00090                 $cache->mark($this->className)->
00091                     add($key, $array, Cache::EXPIRES_FOREVER);
00092             }
00093             
00094             return $array;
00095         }
00097         
00099 
00100         public function uncacheLists()
00101         {
00102             $this->handler->drop();
00103             
00104             return parent::uncacheLists();
00105         }
00107         
00109 
00110         protected function gentlyGetByKey($key)
00111         {
00112             if ($this->handler->ping($this->keyToInt($key)))
00113                 return Cache::me()->mark($this->className)->get($key);
00114             else {
00115                 Cache::me()->mark($this->className)->delete($key);
00116                 return null;
00117             }
00118         }
00119         
00120         private function spawnHandler($classKey)
00121         {
00122             if (!self::$defaultHandler) {
00123                 if (extension_loaded('sysvshm')) {
00124                     $handlerName = 'SharedMemorySegmentHandler';
00125                 } elseif (extension_loaded('sysvmsg')) {
00126                     $handlerName = 'MessageSegmentHandler';
00127                 } else {
00128                     if (extension_loaded('eaccelerator')) {
00129                         $handlerName = 'eAcceleratorSegmentHandler';
00130                     } elseif (extension_loaded('apc')) {
00131                         $handlerName = 'ApcSegmentHandler';
00132                     } elseif (extension_loaded('xcache')) {
00133                         $handlerName = 'XCacheSegmentHandler';
00134                     } else {
00135                         $handlerName = 'CacheSegmentHandler';
00136                     }
00137                 }
00138             } else {
00139                 $handlerName = self::$defaultHandler;
00140             }
00141             
00142             if (!self::$defaultHandler)
00143                 self::$defaultHandler = $handlerName;
00144             
00145             return new self::$defaultHandler($classKey);
00146         }
00148     }
00149 ?>