00001 <?php 00002 /**************************************************************************** 00003 * Copyright (C) 2010 by Evgeny V. Kokovikhin * 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 00018 final class SimpleAggregateCache extends AggregateCache 00019 { 00020 private $peerAmount = null; 00021 private $labels = null; 00022 00026 public static function create() 00027 { 00028 return new self; 00029 } 00030 00031 public function addPeer( 00032 $label, CachePeer $peer, $level = self::LEVEL_NORMAL 00033 ) 00034 { 00035 parent::addPeer($label, $peer, $level); 00036 00037 return $this->dropHelpers(); 00038 } 00039 00040 public function dropPeer($label) 00041 { 00042 parent::dropPeer($label); 00043 00044 return $this->dropHelpers(); 00045 } 00046 00047 public function checkAlive() 00048 { 00049 parent::checkAlive(); 00050 00051 return $this->dropHelpers(); 00052 } 00053 00057 protected function guessLabel($key) 00058 { 00059 if ($this->peerAmount === null) 00060 $this->peerAmount = count($this->peers); 00061 00062 if ($this->labels === null) 00063 $this->labels = array_keys($this->peers); 00064 00065 Assert::isGreaterOrEqual($this->peerAmount, 1); 00066 00067 return 00068 $this->labels[ord(substr($key, -1)) % $this->peerAmount]; 00069 } 00070 00071 private function dropHelpers() 00072 { 00073 $this->peerAmount = null; 00074 $this->labels = null; 00075 00076 return $this; 00077 } 00078 } 00079 ?>