From b93de95dd24ff2a1d3126e6d7d00cd77530a03bf Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Fri, 24 Jul 2026 09:15:40 +0200 Subject: tradlit: offline caching + read; update pre-caches traditional; --clean prunes it; v0.2.0 --- internal/liturgy/fetch.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'internal/liturgy/fetch.go') diff --git a/internal/liturgy/fetch.go b/internal/liturgy/fetch.go index f4f6992..8c887f4 100644 --- a/internal/liturgy/fetch.go +++ b/internal/liturgy/fetch.go @@ -41,11 +41,13 @@ var publishedRe = regexp.MustCompile(`id="\w*0all"`) // validate its own input first. var dateRe = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`) -// cacheFileRe matches the cache file names Load/Harvest write into -// cacheDir(): ".html" or ".json". CleanCache uses it -// to tell cache entries apart from anything else that might be sitting in -// the directory. -var cacheFileRe = regexp.MustCompile(`^(\d{4}-\d{2}-\d{2})\.(html|json)$`) +// cacheFileRe matches the date-prefixed cache file names Load/Harvest/tradlit +// write into CacheDir(): ".html", ".json", and +// ".trad..json" (the traditional-lectionary cache; see +// internal/tradlit). CleanCache uses it to tell cache entries apart from +// anything else that might be sitting in the directory, and to recover the +// date (group 1) for the age check regardless of which cache file it is. +var cacheFileRe = regexp.MustCompile(`^(\d{4}-\d{2}-\d{2})\.[a-z0-9.]+$`) // Options controls how Load resolves a day's readings. type Options struct { @@ -58,9 +60,11 @@ type Options struct { Offline bool } -// cacheDir is where the HTML/JSON cache layers live: +// CacheDir is where the HTML/JSON cache layers live: // ${XDG_CACHE_HOME:-~/.cache}/lectio/ -func cacheDir() string { +// Exported so internal/tradlit shares the same cache root for the +// traditional lectionary's propers. +func CacheDir() string { base := os.Getenv("XDG_CACHE_HOME") if base == "" { home, err := os.UserHomeDir() @@ -93,7 +97,7 @@ func Load(opts Options) ([]Section, error) { return LoadOffline(opts.Date) } - dir := cacheDir() + dir := CacheDir() jsonPath := filepath.Join(dir, opts.Date+".json") htmlPath := filepath.Join(dir, opts.Date+".html") @@ -142,12 +146,14 @@ func Load(opts Options) ([]Section, error) { } // CleanCache removes cached readings whose date is before `before` from -// cacheDir(). It matches only files named ".html"/".json"; -// anything else in the directory (e.g. a stray notes.txt, or the sigla -// store, which lives elsewhere entirely) is left alone. A missing cache dir -// is not an error -- it just means there is nothing to clean yet. +// CacheDir(). It matches only date-prefixed cache files (see cacheFileRe: +// ".html", ".json", or the traditional lectionary's +// ".trad..json"); anything else in the directory (e.g. a stray +// notes.txt, or the sigla store, which lives elsewhere entirely) is left +// alone. A missing cache dir is not an error -- it just means there is +// nothing to clean yet. func CleanCache(before time.Time) (removed int, freed int64, err error) { - dir := cacheDir() + dir := CacheDir() entries, err := os.ReadDir(dir) if err != nil { if os.IsNotExist(err) { -- cgit v1.3