Scale the tablebase cache to ~100k entries (#24)

* Slim the cached tablebase value to the fields the trainer reads (#20)

* Sample for LFU eviction instead of scanning the cache (#21)

* Make cache size configurable and back up daily off-peak (#22)

* Document the cache design in the glossary and an ADR (#23)

* Address cache-scaling code review findings
This commit is contained in:
2026-11-02 23:02:01 +01:00
committed by GitHub
parent fa71783e55
commit a5bb1cb670
16 changed files with 695 additions and 120 deletions
+54
View File
@@ -20,6 +20,60 @@ if System.get_env("PHX_SERVER") do
config :chesstrainer, ChesstrainerWeb.Endpoint, server: true
end
# Tablebase cache: size and the UTC time of the daily backup. Environment
# variables win over the defaults in config/config.exs so deployments can tune
# the cache without a rebuild. Invalid values are reported and ignored rather
# than crashing boot with an opaque parse error.
cache_config = Application.get_env(:chesstrainer, Chesstrainer.Cache, [])
default_max_size = Keyword.fetch!(cache_config, :max_size)
default_backup_time = Keyword.fetch!(cache_config, :backup_time)
cache_max_size =
case System.get_env("TABLEBASE_CACHE_MAX_SIZE") do
nil ->
default_max_size
value ->
case Integer.parse(value) do
{size, ""} when size > 0 ->
size
_ ->
IO.warn(
"ignoring TABLEBASE_CACHE_MAX_SIZE=#{inspect(value)}: " <>
"expected a positive integer, using #{default_max_size}",
[]
)
default_max_size
end
end
cache_backup_time =
case System.get_env("TABLEBASE_BACKUP_TIME") do
nil ->
default_backup_time
value ->
case Chesstrainer.Cache.BackupSchedule.parse_time(value) do
{:ok, time} ->
time
:error ->
IO.warn(
"ignoring TABLEBASE_BACKUP_TIME=#{inspect(value)}: " <>
"expected HH:MM in UTC, using #{inspect(default_backup_time)}",
[]
)
default_backup_time
end
end
config :chesstrainer, Chesstrainer.Cache,
max_size: cache_max_size,
backup_time: cache_backup_time
config :chesstrainer, ChesstrainerWeb.Endpoint,
http: [port: String.to_integer(System.get_env("PORT", "4000"))]