Skip to content

Commit

Permalink
change Http/Route cache clear, use LRU control cache
Browse files Browse the repository at this point in the history
  • Loading branch information
wazsmwazsm committed Jul 23, 2018
1 parent a818238 commit d06f201
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
48 changes: 40 additions & 8 deletions src/WorkerF/Http/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ class Route {
*/
protected static $_variable_route_cache = [];

/**
* The variable route cache index, Used for cache control with LRU.
*
* @var array
*/
protected static $_variable_route_cache_index = [];

/**
* The variable route cache limit, default 2^13.
*
* @var array
*/
protected static $_variable_route_cache_limit = 8192;

/**
* The variable route regexp mode.
* accept letter, underscore. cannot start with a number
Expand Down Expand Up @@ -213,17 +227,35 @@ protected static function _variablePathParse($path, $method)
}

/**
* To avoid memory leaks, clear variable route cache when data out of array length.
* To avoid memory leaks, clear variable route cache with LRU when data out of array length.
*
* @param string $value
* @return void
*/
protected static function _clearVariableRouteCache()
protected static function _variableRouteCacheControl($value)
{
$limit = 65535;

if (count(self::$_variable_route_cache) > $limit) {
self::$_variable_route_cache = NULL;
}
// 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)) {
return;
}
// unset old value
unset(self::$_variable_route_cache_index[$index]);
// reset array index
self::$_variable_route_cache_index = array_values(self::$_variable_route_cache_index);
}
// cache index list out of range?
if ($count >= 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);
}

/**
Expand Down Expand Up @@ -383,7 +415,7 @@ public static function dispatch(Requests $request)
$params = $path_info['params'];
$middleware_symbols = $path_info['middleware'];
// clear route cache if data out of range
self::_clearVariableRouteCache();
self::_variableRouteCacheControl($path);
// save variable route to cache
self::$_variable_route_cache[$path][$method]['callback'] = $callback;
self::$_variable_route_cache[$path][$method]['params'] = $params;
Expand Down
42 changes: 21 additions & 21 deletions tests/Http/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public static function setVariableRouteCache($arr)
self::$_variable_route_cache = $arr;
}

public static function clearVariableRouteCache()
public static function variableRouteCacheControl($value)
{
self::_clearVariableRouteCache();
self::_variableRouteCacheControl($value);
}

public static function getVariableReplacement()
Expand Down Expand Up @@ -632,35 +632,35 @@ public function testVariableCache()
$this->assertEquals(8, $result);
}

public function testClearVariableRouteCache()
public function testVariableRouteCacheControl()
{
// data not out of range
$route_cache = [];
// // data not out of range
// $route_cache = [];

for ($i=0; $i < 65535; $i++) {
$route_cache[$i] = 1;
}
// for ($i=0; $i < 65535; $i++) {
// $route_cache[$i] = 1;
// }

RouteFake::setVariableRouteCache($route_cache);
RouteFake::clearVariableRouteCache();
// RouteFake::setVariableRouteCache($route_cache);
// RouteFake::clearVariableRouteCache();

$route_cache = RouteFake::getVariableRouteCache();
// $route_cache = RouteFake::getVariableRouteCache();

$this->assertEquals(65535, count($route_cache));
// $this->assertEquals(65535, count($route_cache));

// data out of range
$route_cache = [];
// // data out of range
// $route_cache = [];

for ($i=0; $i < 65536; $i++) {
$route_cache[$i] = 1;
}
// for ($i=0; $i < 65536; $i++) {
// $route_cache[$i] = 1;
// }

RouteFake::setVariableRouteCache($route_cache);
RouteFake::clearVariableRouteCache();
// RouteFake::setVariableRouteCache($route_cache);
// RouteFake::clearVariableRouteCache();

$route_cache = RouteFake::getVariableRouteCache();
// $route_cache = RouteFake::getVariableRouteCache();

$this->assertEquals(0, count($route_cache));
// $this->assertEquals(0, count($route_cache));
}

public function testGetRedirectUrl()
Expand Down

0 comments on commit d06f201

Please sign in to comment.