Bound native fragment caching and collapse concurrent page renders

This commit is contained in:
GPT on behalf of DHH
2026-10-07 21:38:36 +02:00
parent b220486c16
commit 8d02540e31
7 changed files with 254 additions and 30 deletions
+10 -1
View File
@@ -7,13 +7,14 @@ class ResponseCache
MAX_KEY_BYTES = 2.kilobytes
def self.instance
@instance ||= new
INSTANCE
end
def initialize
@mutex = Mutex.new
@entries = {}
@bytes = 0
@render_locks = Array.new(16) { Mutex.new }
end
def budget
@@ -36,6 +37,12 @@ class ResponseCache
nil
end
# Collapse cold renders without retaining a mutex for every viewer or URL.
# Rendering must never hold the observer/entry mutex: controllers read it too.
def synchronize_render(key, version, &block)
@render_locks[[ key, version ].hash % @render_locks.length].synchronize(&block)
end
def write(key, version, entry)
size = key.bytesize + entry[:body].bytesize + entry[:headers].sum { |name, value| name.bytesize + value.bytesize } + 256
return if key.bytesize > MAX_KEY_BYTES || size > [ budget, MAX_ENTRY_BYTES ].min
@@ -86,4 +93,6 @@ class ResponseCache
end
[ @database, @namespace, @version ]
end
INSTANCE = new
end