mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
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:
@@ -58,6 +58,12 @@ config :tailwind,
|
||||
env: %{"NODE_PATH" => [Path.expand("../deps", __DIR__), Mix.Project.build_path()]}
|
||||
]
|
||||
|
||||
# Configure the tablebase cache: maximum entries and the UTC time of the daily
|
||||
# backup. Both can be overridden with environment variables at runtime.
|
||||
config :chesstrainer, Chesstrainer.Cache,
|
||||
max_size: 100_000,
|
||||
backup_time: {3, 0}
|
||||
|
||||
# Configure Elixir's Logger
|
||||
config :logger, :default_formatter,
|
||||
format: "$time $metadata[$level] $message\n",
|
||||
|
||||
@@ -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"))]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user