onPHP

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

 

TransparentDaoWorker.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: TransparentDaoWorker.class.php 5374 2008-08-03 14:15:27Z voxus $ */
00012 
00021     abstract class TransparentDaoWorker extends CommonDaoWorker
00022     {
00023         abstract protected function gentlyGetByKey($key);
00024         
00026 
00027         public function getById($id)
00028         {
00029             try {
00030                 return parent::getById($id, Cache::EXPIRES_FOREVER);
00031             } catch (CachedObjectNotFoundException $e) {
00032                 throw $e;
00033             } catch (ObjectNotFoundException $e) {
00034                 $this->cacheNullById($id);
00035                 throw $e;
00036             }
00037         }
00038         
00039         public function getByLogic(LogicalObject $logic)
00040         {
00041             return parent::getByLogic($logic, Cache::EXPIRES_FOREVER);
00042         }
00043         
00044         public function getByQuery(SelectQuery $query)
00045         {
00046             try {
00047                 return parent::getByQuery($query, Cache::EXPIRES_FOREVER);
00048             } catch (CachedObjectNotFoundException $e) {
00049                 throw $e;
00050             } catch (ObjectNotFoundException $e) {
00051                 $this->cacheByQuery($query, Cache::NOT_FOUND);
00052                 throw $e;
00053             }
00054         }
00055         
00056         public function getCustom(SelectQuery $query)
00057         {
00058             try {
00059                 return parent::getCustom($query, Cache::EXPIRES_FOREVER);
00060             } catch (CachedObjectNotFoundException $e) {
00061                 throw $e;
00062             } catch (ObjectNotFoundException $e) {
00063                 $this->cacheByQuery($query, Cache::NOT_FOUND);
00064                 throw $e;
00065             }
00066         }
00068         
00070 
00071         public function getListByIds(array $ids)
00072         {
00073             $list = array();
00074             $toFetch = array();
00075             $prefixed = array();
00076             
00077             $proto = $this->dao->getProtoClass();
00078             
00079             $proto->beginPrefetch();
00080             
00081             // dupes, if any, will be resolved later @ ArrayUtils::regularizeList
00082             $ids = array_unique($ids);
00083             
00084             foreach ($ids as $id)
00085                 $prefixed[$id] = $this->className.'_'.$id;
00086             
00087             if (
00088                 $cachedList
00089                     = Cache::me()->mark($this->className)->getList($prefixed)
00090             ) {
00091                 foreach ($cachedList as $cached) {
00092                     if ($cached && ($cached !== Cache::NOT_FOUND)) {
00093                         $list[] = $this->dao->completeObject($cached);
00094                         
00095                         unset($prefixed[$cached->getId()]);
00096                     }
00097                 }
00098             }
00099             
00100             $toFetch += array_keys($prefixed);
00101             
00102             if ($toFetch) {
00103                 $remainList = array();
00104                 
00105                 foreach ($toFetch as $id) {
00106                     try {
00107                         $remainList[] = $this->getById($id);
00108                     } catch (ObjectNotFoundException $e) {/*_*/}
00109                 }
00110                 
00111                 $list = array_merge($list, $remainList);
00112             }
00113             
00114             $proto->endPrefetch($list);
00115             
00116             return $list;
00117         }
00118         
00119         public function getListByQuery(SelectQuery $query)
00120         {
00121             $list = $this->getCachedList($query);
00122             
00123             if ($list) {
00124                 if ($list === Cache::NOT_FOUND)
00125                     throw new ObjectNotFoundException();
00126                 else
00127                     return $list;
00128             } else {
00129                 if ($list = $this->fetchList($query))
00130                     return $this->cacheListByQuery($query, $list);
00131                 else {
00132                     $this->cacheListByQuery($query, Cache::NOT_FOUND);
00133                     throw new ObjectNotFoundException();
00134                 }
00135             }
00136             
00137             Assert::isUnreachable();
00138         }
00139         
00140         public function getListByLogic(LogicalObject $logic)
00141         {
00142             return parent::getListByLogic($logic, Cache::EXPIRES_FOREVER);
00143         }
00144         
00145         public function getPlainList()
00146         {
00147             return parent::getPlainList(Cache::EXPIRES_FOREVER);
00148         }
00150         
00152 
00153         public function getCustomList(SelectQuery $query)
00154         {
00155             try {
00156                 return parent::getCustomList($query, Cache::EXPIRES_FOREVER);
00157             } catch (CachedObjectNotFoundException $e) {
00158                 throw $e;
00159             } catch (ObjectNotFoundException $e) {
00160                 $this->cacheByQuery($query, Cache::NOT_FOUND);
00161                 throw $e;
00162             }
00163         }
00164         
00165         public function getCustomRowList(SelectQuery $query)
00166         {
00167             try {
00168                 return parent::getCustomRowList($query, Cache::EXPIRES_FOREVER);
00169             } catch (CachedObjectNotFoundException $e) {
00170                 throw $e;
00171             } catch (ObjectNotFoundException $e) {
00172                 $this->cacheByQuery($query, Cache::NOT_FOUND);
00173                 throw $e;
00174             }
00175         }
00177         
00179 
00180         public function getQueryResult(SelectQuery $query)
00181         {
00182             return parent::getQueryResult($query, Cache::EXPIRES_FOREVER);
00183         }
00185 
00187 
00188         protected function cacheById(
00189             Identifiable $object,
00190             $expires = Cache::EXPIRES_FOREVER)
00191         {
00192             Cache::me()->mark($this->className)->
00193                 add(
00194                     $this->className.'_'.$object->getId(),
00195                     $object,
00196                     $expires
00197                 );
00198             
00199             return $object;
00200         }
00202         
00204 
00205         protected function getCachedByQuery(SelectQuery $query)
00206         {
00207             return
00208                 $this->gentlyGetByKey(
00209                     $this->className.self::SUFFIX_QUERY.$query->getId()
00210                 );
00211         }
00212         
00213         protected function getCachedList(SelectQuery $query)
00214         {
00215             return
00216                 $this->gentlyGetByKey(
00217                     $this->className.self::SUFFIX_LIST.$query->getId()
00218                 );
00219         }
00220         
00221         protected function cacheNullById($id)
00222         {
00223             return
00224                 Cache::me()->mark($this->className)->
00225                     add(
00226                         $this->className.'_'.$id,
00227                         Cache::NOT_FOUND,
00228                         Cache::EXPIRES_FOREVER
00229                     );
00230         }
00231         
00232         protected function keyToInt($key)
00233         {
00234             // 7 == strlen(dechex(x86 PHP_INT_MAX)) - 1
00235             return hexdec(substr(md5($key), 0, 7)) + strlen($key);
00236         }
00238     }
00239 ?>

generated by doxygen-1.5.5
for onPHP at Thu Aug 7 18:24:24 2008