onPHP

main/DAOs/Workers/VoodooDaoWorker.class.php Source File

 

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

generated by doxygen-1.5.5
for onPHP at Mon Mar 24 20:29:09 2008