diff --git a/src/WorkerF/Http/Route.php b/src/WorkerF/Http/Route.php index bc8ac4e..fde8526 100644 --- a/src/WorkerF/Http/Route.php +++ b/src/WorkerF/Http/Route.php @@ -234,12 +234,10 @@ protected static function _variablePathParse($path, $method) */ protected static function _variableRouteCacheControl($value) { - // get route cache index count - $count = count(self::$_variable_route_cache_index); // is value in cache index list? if (FALSE !== ($index = array_search($value, self::$_variable_route_cache_index))) { // if value is tail, do nothing - if ($index == ($count - 1)) { + if ($index == (count(self::$_variable_route_cache_index) - 1)) { return; } // unset old value @@ -247,15 +245,15 @@ protected static function _variableRouteCacheControl($value) // reset array index self::$_variable_route_cache_index = array_values(self::$_variable_route_cache_index); } + // push value to tail + array_push(self::$_variable_route_cache_index, $value); // cache index list out of range? - if ($count >= self::$_variable_route_cache_limit) { + if (count(self::$_variable_route_cache_index) > self::$_variable_route_cache_limit) { // remove head value $remove_value = array_shift(self::$_variable_route_cache_index); // unset route cache, free memory unset(self::$_variable_route_cache[$remove_value]); - } - // push value to tail - array_push(self::$_variable_route_cache_index, $value); + } } /** diff --git a/tests/Http/RouteTest.php b/tests/Http/RouteTest.php index ae40f0f..24188b6 100644 --- a/tests/Http/RouteTest.php +++ b/tests/Http/RouteTest.php @@ -41,6 +41,11 @@ public static function getVariableRouteCacheIndex() return self::$_variable_route_cache_index; } + public static function setVariableRouteCacheIndex($arr) + { + self::$_variable_route_cache_index = $arr; + } + public static function getVariableRouteCacheLimit() { return self::$_variable_route_cache_limit; @@ -644,33 +649,53 @@ public function testVariableCache() public function testVariableRouteCacheControl() { - // // data not out of range - // $route_cache = []; - - // for ($i=0; $i < 65535; $i++) { - // $route_cache[$i] = 1; - // } - - // RouteFake::setVariableRouteCache($route_cache); - // RouteFake::clearVariableRouteCache(); - - // $route_cache = RouteFake::getVariableRouteCache(); - - // $this->assertEquals(65535, count($route_cache)); - - // // data out of range - // $route_cache = []; - - // for ($i=0; $i < 65536; $i++) { - // $route_cache[$i] = 1; - // } - - // RouteFake::setVariableRouteCache($route_cache); - // RouteFake::clearVariableRouteCache(); - - // $route_cache = RouteFake::getVariableRouteCache(); - - // $this->assertEquals(0, count($route_cache)); + // set cache limit + RouteFake::setVariableRouteCacheLimit(10); + + /* data not out of range */ + + $route_cache_index = [9, 1, 5, 6, 7, 2]; + RouteFake::setVariableRouteCacheIndex($route_cache_index); + RouteFake::setVariableRouteCache([9 => 'a', 7 => 'c', 1 => 'b']); + + // value add + RouteFake::variableRouteCacheControl(15); + $route_cache_index = RouteFake::getVariableRouteCacheIndex(); + $this->assertEquals([9, 1, 5, 6, 7, 2, 15], $route_cache_index); + + // value hit + RouteFake::variableRouteCacheControl(1); + $route_cache_index = RouteFake::getVariableRouteCacheIndex(); + $this->assertEquals([9, 5, 6, 7, 2, 15, 1], $route_cache_index); + RouteFake::variableRouteCacheControl(7); + $route_cache_index = RouteFake::getVariableRouteCacheIndex(); + $this->assertEquals([9, 5, 6, 2, 15, 1, 7], $route_cache_index); + $route_cache = RouteFake::getVariableRouteCache(); + $this->assertEquals([9 => 'a', 7 => 'c', 1 => 'b'], $route_cache); + + /* data out of range */ + + $route_cache_index = [9, 1, 5, 6, 7, 2, 11, 3, 15, 4]; + RouteFake::setVariableRouteCacheIndex($route_cache_index); + RouteFake::setVariableRouteCache([9 => 'a', 7 => 'c', 1 => 'b']); + + // value add + RouteFake::variableRouteCacheControl(18); + $route_cache_index = RouteFake::getVariableRouteCacheIndex(); + $route_cache = RouteFake::getVariableRouteCache(); + $this->assertEquals([1, 5, 6, 7, 2, 11, 3, 15, 4, 18], $route_cache_index); + $this->assertEquals([7 => 'c', 1 => 'b'], $route_cache); + RouteFake::variableRouteCacheControl(16); + $route_cache_index = RouteFake::getVariableRouteCacheIndex(); + $route_cache = RouteFake::getVariableRouteCache(); + $this->assertEquals([5, 6, 7, 2, 11, 3, 15, 4, 18, 16], $route_cache_index); + $this->assertEquals([7 => 'c'], $route_cache); + + // value hit + RouteFake::variableRouteCacheControl(2); + $route_cache_index = RouteFake::getVariableRouteCacheIndex(); + $this->assertEquals([5, 6, 7, 11, 3, 15, 4, 18, 16, 2], $route_cache_index); + $this->assertEquals([7 => 'c'], $route_cache); } public function testSetVariableRouteCacheLimit()