mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +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:
Vendored
+54
-7
@@ -1,25 +1,38 @@
|
||||
defmodule Chesstrainer.Cache.Persist do
|
||||
@moduledoc """
|
||||
Persistence for the cache. Backup and restore functions.
|
||||
|
||||
A backup is written to a temporary DETS file in the same directory and then
|
||||
atomically renamed into place. A crash during the write therefore leaves the
|
||||
previous backup untouched instead of a truncated file that fails to restore.
|
||||
"""
|
||||
|
||||
@backup_path "priv/static/backups"
|
||||
@format_version "v2"
|
||||
|
||||
@doc """
|
||||
Writes the `table` to its backup file. Returns `:ok` or `{:error, reason}`.
|
||||
"""
|
||||
def backup(table) do
|
||||
File.mkdir_p!(@backup_path)
|
||||
name = backup_filename(table) |> String.to_charlist()
|
||||
{:ok, dets_table} = :dets.open_file(name, type: :set)
|
||||
:ets.to_dets(table, dets_table)
|
||||
:dets.sync(dets_table)
|
||||
:dets.close(dets_table)
|
||||
path = backup_path(table)
|
||||
|
||||
:global.trans(
|
||||
{__MODULE__, table},
|
||||
fn -> write(table, path <> ".tmp", path) end,
|
||||
[node()],
|
||||
:infinity
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Restores the backup file for `table`, if a readable one exists.
|
||||
"""
|
||||
def restore(table) do
|
||||
File.mkdir_p!(@backup_path)
|
||||
drop_legacy_backup(table)
|
||||
|
||||
path = backup_filename(table)
|
||||
path = backup_path(table)
|
||||
|
||||
if File.exists?(path) do
|
||||
name = String.to_charlist(path)
|
||||
@@ -49,7 +62,41 @@ defmodule Chesstrainer.Cache.Persist do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp backup_filename(table), do: Path.join(@backup_path, "#{table}.#{@format_version}.det")
|
||||
@doc """
|
||||
The path of the backup file for `table`.
|
||||
"""
|
||||
def backup_path(table), do: Path.join(@backup_path, "#{table}.#{@format_version}.det")
|
||||
|
||||
defp write(table, temp, path) do
|
||||
# Remove a stale temporary file left by an earlier crash; if the path is
|
||||
# unusable the open below fails and the existing backup stays in place.
|
||||
_ = File.rm(temp)
|
||||
|
||||
result =
|
||||
with {:ok, dets_table} <- :dets.open_file(String.to_charlist(temp), type: :set) do
|
||||
try do
|
||||
_ = :ets.to_dets(table, dets_table)
|
||||
:dets.sync(dets_table)
|
||||
after
|
||||
:dets.close(dets_table)
|
||||
end
|
||||
end
|
||||
|
||||
case result do
|
||||
:ok ->
|
||||
case File.rename(temp, path) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
_ = File.rm(temp)
|
||||
{:error, reason}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp drop_legacy_backup(table) do
|
||||
legacy = Path.join(@backup_path, "#{table}.det")
|
||||
|
||||
Reference in New Issue
Block a user