Files
owenrees 8b894465cc Implement tablebase parsing fixes and LFU cache
#5: normalise the full lichess tablebase category vocabulary (cursed-win
and blessed-loss classify as draws), return {:ok, tablebase} on a cache
hit, and unwrap the tablebase in game_from_fen.

#6: replace the LRU cache with an LFU cache storing
{key, value, frequency, last_access}, evicting the least-frequently-used
entry (LRU tie-break) and halving every frequency on a weekly sweep.
Persistence moves to a versioned v2 dets file, dropping the legacy
backup; the cache restores synchronously at startup.

Also updates stale tags enum and Play LiveView tests, and drops the
tracked legacy cache backup.
2026-11-01 21:24:51 +01:00

26 lines
615 B
Elixir

defmodule Chesstrainer.Cache.Sweep do
@moduledoc """
Frequency decay for the LFU cache.
Halving every frequency each week lets entries that were used heavily once but
have not been used since age out of the cache.
"""
@doc """
Halves the frequency of every entry in `table` with integer division, so an
entry used once decays from `1` to `0`.
"""
def halve_frequencies(table) do
:ets.foldl(
fn {key, value, frequency, last_access}, :ok ->
:ets.insert(table, {key, value, div(frequency, 2), last_access})
:ok
end,
:ok,
table
)
:ok
end
end