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