CacheDaoWorker.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 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 
00021     class CacheDaoWorker extends TransparentDaoWorker
00022     {
00023         const MAX_RANDOM_ID = 134217728;
00024         
00026 
00027         protected function cacheByQuery(
00028             SelectQuery $query,
00029             /* Identifiable */ $object,
00030             $expires = Cache::EXPIRES_FOREVER
00031         )
00032         {
00033             Cache::me()->mark($this->className)->
00034                 add(
00035                     $this->makeQueryKey($query, self::SUFFIX_QUERY),
00036                     $object,
00037                     $expires
00038                 );
00039             
00040             return $object;
00041         }
00042         
00043         protected function cacheListByQuery(
00044             SelectQuery $query,
00045             /* array || Cache::NOT_FOUND */ $array
00046         )
00047         {
00048             if ($array !== Cache::NOT_FOUND) {
00049                 Assert::isArray($array);
00050                 Assert::isTrue(current($array) instanceof Identifiable);
00051             }
00052             
00053             Cache::me()->mark($this->className)->
00054                 add(
00055                     $this->makeQueryKey($query, self::SUFFIX_LIST),
00056                     $array,
00057                     Cache::EXPIRES_FOREVER
00058                 );
00059             
00060             return $array;
00061         }
00063         
00065 
00066         public function uncacheLists()
00067         {
00068             if (
00069                 !Cache::me()->
00070                     mark($this->className)->
00071                     increment($this->className, 1)
00072             )
00073                 Cache::me()->mark($this->className)->delete($this->className);
00074             
00075             return true;
00076         }
00078         
00080 
00081         protected function gentlyGetByKey($key)
00082         {
00083             return Cache::me()->mark($this->className)->get($key);
00084         }
00085         
00086         protected function getLayerId()
00087         {
00088             if (
00089                 !$result =
00090                     Cache::me()->mark($this->className)->get($this->className)
00091             ) {
00092                 $result = mt_rand(1, self::MAX_RANDOM_ID);
00093                 
00094                 Cache::me()->
00095                 mark($this->className)->
00096                 set(
00097                     $this->className,
00098                     $result,
00099                     Cache::EXPIRES_FOREVER
00100                 );
00101             }
00102             
00103             return '@'.$result;
00104         }
00105         
00106         protected function makeQueryKey(SelectQuery $query, $suffix)
00107         {
00108             return parent::makeQueryKey($query, $suffix).$this->getLayerId();
00109         }
00111     }
00112 ?>