Skip to content

Commit

Permalink
Http/Route add setVariableRouteCacheLimit method
Browse files Browse the repository at this point in the history
  • Loading branch information
wazsmwazsm committed Jul 24, 2018
1 parent d06f201 commit dc7fc4e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/WorkerF/Http/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions tests/Http/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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/');
Expand Down

0 comments on commit dc7fc4e

Please sign in to comment.