00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00030 final class VoodooDaoWorker extends TransparentDaoWorker
00031 {
00032 protected $classKey = null;
00033 protected $handler = null;
00034
00035
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 public function cacheByQuery(
00062 SelectQuery $query, $object
00063 )
00064 {
00065 $queryId = $query->getId();
00066
00067 $key = $this->className.self::SUFFIX_QUERY.$queryId;
00068
00069 if ($this->handler->touch($this->keyToInt($key)))
00070 Cache::me()->mark($this->className)->
00071 add($key, $object, Cache::EXPIRES_FOREVER);
00072
00073 return $object;
00074 }
00075
00076 public function cacheListByQuery(SelectQuery $query, $array)
00077 {
00078 if ($array !== Cache::NOT_FOUND) {
00079 Assert::isArray($array);
00080 Assert::isTrue(current($array) instanceof Identifiable);
00081 }
00082
00083 $cache = Cache::me();
00084
00085 $key = $this->className.self::SUFFIX_LIST.$query->getId();
00086
00087 if ($this->handler->touch($this->keyToInt($key))) {
00088
00089 $cache->mark($this->className)->
00090 add($key, $array, Cache::EXPIRES_FOREVER);
00091
00092 if ($array !== Cache::NOT_FOUND)
00093 foreach ($array as $key => $object) {
00094 if (
00095 !$this->handler->ping(
00096 $this->keyToInt(
00097 $this->className.'_'.$object->getId()
00098 )
00099 )
00100 ) {
00101 $this->cacheById($object);
00102 }
00103 }
00104 }
00105
00106 return $array;
00107 }
00109
00111
00112 public function uncacheLists()
00113 {
00114 return $this->handler->drop();
00115 }
00117
00119
00120 protected function gentlyGetByKey($key)
00121 {
00122 if ($this->handler->ping($this->keyToInt($key)))
00123 return Cache::me()->mark($this->className)->get($key);
00124 else {
00125 Cache::me()->mark($this->className)->delete($key);
00126 return null;
00127 }
00128 }
00129
00130 protected function spawnHandler($classKey)
00131 {
00132 if (!self::$defaultHandler) {
00133 if (extension_loaded('sysvshm')) {
00134 $handlerName = 'SharedMemorySegmentHandler';
00135 } elseif (extension_loaded('sysvmsg')) {
00136 $handlerName = 'MessageSegmentHandler';
00137 } else {
00138 if (extension_loaded('eaccelerator')) {
00139 $handlerName = 'eAcceleratorSegmentHandler';
00140 } elseif (extension_loaded('apc')) {
00141 $handlerName = 'ApcSegmentHandler';
00142 } elseif (extension_loaded('xcache')) {
00143 $handlerName = 'XCacheSegmentHandler';
00144 } else {
00145 $handlerName = 'FileSystemSegmentHandler';
00146 }
00147 }
00148 } else {
00149 $handlerName = self::$defaultHandler;
00150 }
00151
00152 if (!self::$defaultHandler)
00153 self::$defaultHandler = $handlerName;
00154
00155 return new self::$defaultHandler($classKey);
00156 }
00158 }
00159 ?>