Skip to content

Commit 2cf852b

Browse files
committed
Refactor route caching to use configurable max size
1 parent d42f03a commit 2cf852b

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

spec/spec_helper.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ Spec.after_each do
9393
Kemal.config.clear
9494
Kemal::FilterHandler::INSTANCE.tree = Radix::Tree(Array(Kemal::FilterHandler::FilterBlock)).new
9595
Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new
96-
Kemal::RouteHandler::INSTANCE.cached_routes = Kemal::LRUCache(String, Radix::Result(Kemal::Route)).new(1024)
96+
Kemal::RouteHandler::INSTANCE.cached_routes = Kemal::LRUCache(String, Radix::Result(Kemal::Route)).new(Kemal.config.max_route_cache_size)
9797
Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new
9898
end

src/kemal/config.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Kemal
2626
property serve_static : (Bool | Hash(String, Bool))
2727
property static_headers : (HTTP::Server::Context, String, File::Info -> Void)?
2828
property? powered_by_header : Bool = true
29+
property max_route_cache_size : Int32
2930

3031
def initialize
3132
@app_name = "Kemal"
@@ -43,6 +44,7 @@ module Kemal
4344
@running = false
4445
@shutdown_message = true
4546
@handler_position = 0
47+
@max_route_cache_size = 1024
4648
end
4749

4850
@[Deprecated("Use standard library Log")]
@@ -69,6 +71,7 @@ module Kemal
6971
@router_included = false
7072
@handler_position = 0
7173
@default_handlers_setup = false
74+
@max_route_cache_size = 1024
7275
HANDLERS.clear
7376
CUSTOM_HANDLERS.clear
7477
FILTER_HANDLERS.clear

src/kemal/route_handler.cr

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Kemal
5757
private def insert_front(node : Node(K, V))
5858
node.prev = nil
5959
node.next = @head
60-
@head.try { |h| h.prev = node }
60+
@head.try(&.prev=(node))
6161
@head = node
6262
@tail = node if @tail.nil?
6363
end
@@ -68,8 +68,8 @@ module Kemal
6868
# unlink
6969
prev = node.prev
7070
nxt = node.next
71-
prev.try { |p| p.next = nxt }
72-
nxt.try { |n| n.prev = prev }
71+
prev.try(&.next=(nxt))
72+
nxt.try(&.prev=(prev))
7373

7474
# fix tail if needed
7575
if node == @tail
@@ -79,7 +79,7 @@ module Kemal
7979
# insert at head
8080
node.prev = nil
8181
node.next = @head
82-
@head.try { |h| h.prev = node }
82+
@head.try(&.prev=(node))
8383
@head = node
8484
end
8585

@@ -105,13 +105,12 @@ module Kemal
105105
class RouteHandler
106106
include HTTP::Handler
107107

108-
INSTANCE = new
109-
CACHED_ROUTES_LIMIT = 1024
108+
INSTANCE = new
110109
property routes, cached_routes
111110

112111
def initialize
113112
@routes = Radix::Tree(Route).new
114-
@cached_routes = LRUCache(String, Radix::Result(Route)).new(CACHED_ROUTES_LIMIT)
113+
@cached_routes = LRUCache(String, Radix::Result(Route)).new(Kemal.config.max_route_cache_size)
115114
end
116115

117116
def call(context : HTTP::Server::Context)

0 commit comments

Comments
 (0)