diff --git a/src/WorkerF/Http/Route.php b/src/WorkerF/Http/Route.php index 4c39a27..bc8ac4e 100644 --- a/src/WorkerF/Http/Route.php +++ b/src/WorkerF/Http/Route.php @@ -369,6 +369,20 @@ protected static function _runDispatch(Requests $request, $callback, $middleware } } + /** + * Set variable route cache length limit. + * + * @param int $value + * @return void + */ + public static function setVariableRouteCacheLimit($value) + { + if ( ! (is_numeric($value) && $value > 0)) { + throw new \InvalidArgumentException("The route cache limit need a numeric greater than 0 !"); + } + self::$_variable_route_cache_limit = $value; + } + /** * dispatch route. * @@ -414,7 +428,7 @@ public static function dispatch(Requests $request) $callback = $path_info['callback']; $params = $path_info['params']; $middleware_symbols = $path_info['middleware']; - // clear route cache if data out of range + // clear route cache with LRU if data out of range self::_variableRouteCacheControl($path); // save variable route to cache self::$_variable_route_cache[$path][$method]['callback'] = $callback; diff --git a/tests/Http/RouteTest.php b/tests/Http/RouteTest.php index 41ed0e7..ae40f0f 100644 --- a/tests/Http/RouteTest.php +++ b/tests/Http/RouteTest.php @@ -36,6 +36,16 @@ public static function setVariableRouteCache($arr) self::$_variable_route_cache = $arr; } + public static function getVariableRouteCacheIndex() + { + return self::$_variable_route_cache_index; + } + + public static function getVariableRouteCacheLimit() + { + return self::$_variable_route_cache_limit; + } + public static function variableRouteCacheControl($value) { self::_variableRouteCacheControl($value); @@ -663,6 +673,29 @@ public function testVariableRouteCacheControl() // $this->assertEquals(0, count($route_cache)); } + public function testSetVariableRouteCacheLimit() + { + RouteFake::setVariableRouteCacheLimit(552); + + $this->assertEquals(552, RouteFake::getVariableRouteCacheLimit()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSetVariableRouteCacheLimitException() + { + RouteFake::setVariableRouteCacheLimit(-1); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSetVariableRouteCacheLimitException2() + { + RouteFake::setVariableRouteCacheLimit(['a' => 1, 'b' => 2]); + } + public function testGetRedirectUrl() { Config::set('app.base_url', 'http://test.com/');