mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	
						commit
						cd5f83f013
					
				
					 2 changed files with 20 additions and 4 deletions
				
			
		|  | @ -83,5 +83,6 @@ end | ||||||
| Spec.after_each do | Spec.after_each do | ||||||
|   Kemal.config.clear |   Kemal.config.clear | ||||||
|   Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new |   Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new | ||||||
|  |   Kemal::RouteHandler::INSTANCE.cached_routes = Hash(String, Radix::Result(Route)).new | ||||||
|   Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new |   Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -4,11 +4,13 @@ module Kemal | ||||||
|   class RouteHandler |   class RouteHandler | ||||||
|     include HTTP::Handler |     include HTTP::Handler | ||||||
| 
 | 
 | ||||||
|     INSTANCE = new |     INSTANCE            = new | ||||||
|     property routes |     CACHED_ROUTES_LIMIT = 1024 | ||||||
|  |     property routes, cached_routes | ||||||
| 
 | 
 | ||||||
|     def initialize |     def initialize | ||||||
|       @routes = Radix::Tree(Route).new |       @routes = Radix::Tree(Route).new | ||||||
|  |       @cached_routes = Hash(String, Radix::Result(Route)).new | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def call(context : HTTP::Server::Context) |     def call(context : HTTP::Server::Context) | ||||||
|  | @ -22,9 +24,22 @@ module Kemal | ||||||
|       add_to_radix_tree("HEAD", path, Route.new("HEAD", path) { }) if method == "GET" |       add_to_radix_tree("HEAD", path, Route.new("HEAD", path) { }) if method == "GET" | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     # Check if a route is defined and returns the lookup |     # Looks up the route from the Radix::Tree for the first time and caches to improve performance. | ||||||
|     def lookup_route(verb : String, path : String) |     def lookup_route(verb : String, path : String) | ||||||
|       @routes.find radix_path(verb, path) |       lookup_path = radix_path(verb, path) | ||||||
|  | 
 | ||||||
|  |       if cached_route = @cached_routes[lookup_path]? | ||||||
|  |         return cached_route | ||||||
|  |       end | ||||||
|  | 
 | ||||||
|  |       route = @routes.find(lookup_path) | ||||||
|  | 
 | ||||||
|  |       if route.found? | ||||||
|  |         @cached_routes.clear if @cached_routes.size == CACHED_ROUTES_LIMIT | ||||||
|  |         @cached_routes[lookup_path] = route | ||||||
|  |       end | ||||||
|  | 
 | ||||||
|  |       route | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     # Processes the route if it's a match. Otherwise renders 404. |     # Processes the route if it's a match. Otherwise renders 404. | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue