feat(lookup): a cache TTL of 0 switches the cache off
Wanted for the case where the answers are moving: an operator correcting their
own QRZ record, or chasing a DXpedition whose page changes during the operation,
otherwise waits out thirty days before OpsLog will ask again. Clearing the cache
by hand works once; this is the setting for a whole session.
Nothing is read from it and nothing is written to it — rows stored while it is
off would only sit there going stale, waiting for the day it comes back on.
Switching off is NOT clearing: what it already holds stays, and the Clear cache
button remains the way to throw that away.
Two distinctions the code now has to keep, both load-bearing:
An EXPLICIT stored zero is off; an ABSENT key is the thirty-day default. Every
operator who has never opened this setting has nothing stored, and reading that
blank as a zero would silently switch the cache off for all of them.
The CONSTRUCTOR's zero is still the default, not off. At startup the settings
have not been read yet, and beginning with no cache would hammer the provider
for the first seconds of every launch. Only SetTTL, called once the operator's
settings are known, can switch it off.
A negative lifetime is meaningless and is ignored rather than rounded into
either meaning.
The input had to change too: it derived its value from the stored number on
every keystroke, so the box could not be emptied — and 0 was unreachable
outright, since parseInt('0') || 30 is 30.
This commit is contained in:
@@ -2670,9 +2670,15 @@ func (a *App) reloadLookupProviders() {
|
||||
fmt.Println("OpsLog: settings load error:", err)
|
||||
return
|
||||
}
|
||||
if days, _ := strconv.Atoi(m[keyCacheTTL]); days > 0 {
|
||||
// An EXPLICIT zero switches the cache off; an ABSENT key leaves the thirty-day
|
||||
// default the cache was built with. The difference matters: every operator who
|
||||
// has never opened this setting has no value stored, and reading that blank as
|
||||
// a zero would silently turn the cache off for all of them.
|
||||
if raw := strings.TrimSpace(m[keyCacheTTL]); raw != "" {
|
||||
if days, err := strconv.Atoi(raw); err == nil && days >= 0 {
|
||||
a.cache.SetTTL(time.Duration(days) * 24 * time.Hour)
|
||||
}
|
||||
}
|
||||
|
||||
build := func(name string) lookup.Provider {
|
||||
switch name {
|
||||
@@ -7367,9 +7373,13 @@ func (a *App) GetLookupSettings() (LookupSettings, error) {
|
||||
if err != nil {
|
||||
return LookupSettings{}, err
|
||||
}
|
||||
ttl, _ := strconv.Atoi(m[keyCacheTTL])
|
||||
if ttl <= 0 {
|
||||
ttl = 30
|
||||
// Same rule as reloadLookupProviders: blank means "never set" and gets the
|
||||
// default, while a stored zero is the operator asking for no cache at all.
|
||||
ttl := 30
|
||||
if raw := strings.TrimSpace(m[keyCacheTTL]); raw != "" {
|
||||
if n, err := strconv.Atoi(raw); err == nil && n >= 0 {
|
||||
ttl = n
|
||||
}
|
||||
}
|
||||
return LookupSettings{
|
||||
QRZUser: m[keyQRZUser],
|
||||
@@ -7389,8 +7399,9 @@ func (a *App) SaveLookupSettings(s LookupSettings) error {
|
||||
if a.settings == nil {
|
||||
return fmt.Errorf("db not initialized")
|
||||
}
|
||||
if s.CacheTTLDays <= 0 {
|
||||
s.CacheTTLDays = 30
|
||||
// Zero is kept — it means no cache. Only a negative number is nonsense.
|
||||
if s.CacheTTLDays < 0 {
|
||||
s.CacheTTLDays = 0
|
||||
}
|
||||
// Reject a primary == failsafe routing combo — would just hit the same
|
||||
// provider twice. Frontend should prevent this but defend in depth.
|
||||
|
||||
+4
-2
@@ -4,11 +4,13 @@
|
||||
"date": "",
|
||||
"en": [
|
||||
"An entity that is a single island group now fills the IOTA reference on its own — no callbook subscription needed.",
|
||||
"CAT sharing can now speak TCI instead of Hamlib, so a TCI-only program reaches whatever radio OpsLog is on."
|
||||
"CAT sharing can now speak TCI instead of Hamlib, so a TCI-only program reaches whatever radio OpsLog is on.",
|
||||
"Lookup cache: a TTL of 0 switches it off, so a callbook record you are correcting is re-read every time."
|
||||
],
|
||||
"fr": [
|
||||
"Une entité qui est un seul groupe d’îles remplit désormais la référence IOTA toute seule, sans abonnement callbook.",
|
||||
"Le partage CAT peut désormais parler TCI au lieu de Hamlib : un logiciel TCI atteint la radio, quelle qu’elle soit."
|
||||
"Le partage CAT peut désormais parler TCI au lieu de Hamlib : un logiciel TCI atteint la radio, quelle qu’elle soit.",
|
||||
"Cache des recherches : un TTL à 0 le désactive, pour relire à chaque fois une fiche callbook en cours de correction."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -2368,6 +2368,11 @@ export function SettingsModal({ onClose, onSaved, initialSection, onMainPaneChan
|
||||
}
|
||||
|
||||
function LookupPanel() {
|
||||
// The cache lifetime as TYPED, so the box can be emptied and re-filled.
|
||||
// Re-seeded when the settings arrive from the backend — which is after the
|
||||
// first render, so it cannot simply be the initial value.
|
||||
const [ttlText, setTtlText] = useState(String(lookup.cache_ttl_days));
|
||||
useEffect(() => { setTtlText(String(lookup.cache_ttl_days)); }, [lookup.cache_ttl_days]);
|
||||
// Per-row provider editor — kept inline because it's only used twice
|
||||
// and needs closure access to the parent state.
|
||||
const row = (
|
||||
@@ -2504,16 +2509,31 @@ export function SettingsModal({ onClose, onSaved, initialSection, onMainPaneChan
|
||||
<div className="flex gap-3 items-end">
|
||||
<div className="space-y-1 w-40">
|
||||
<Label>{t('lk.ttl')}</Label>
|
||||
{/* Raw text, not the stored number. Deriving the value from the
|
||||
number on every keystroke made the box impossible to empty —
|
||||
and "0" itself unreachable, since parseInt('0') || 30 is 30.
|
||||
Zero is now a real setting, so it has to be typeable. */}
|
||||
<Input
|
||||
type="number" min={1} max={3650}
|
||||
value={lookup.cache_ttl_days}
|
||||
onChange={(e) => setLookup((s) => ({ ...s, cache_ttl_days: parseInt(e.target.value) || 30 }))}
|
||||
type="number" min={0} max={3650}
|
||||
value={ttlText}
|
||||
onChange={(e) => {
|
||||
const raw = e.target.value;
|
||||
setTtlText(raw);
|
||||
const n = parseInt(raw, 10);
|
||||
if (Number.isFinite(n) && n >= 0) {
|
||||
setLookup((s) => ({ ...s, cache_ttl_days: Math.min(n, 3650) }));
|
||||
}
|
||||
}}
|
||||
onBlur={() => setTtlText(String(lookup.cache_ttl_days))}
|
||||
/>
|
||||
</div>
|
||||
<Button variant="outline" onClick={clearCache} disabled={clearing}>
|
||||
{clearing ? t('lk.clearing') : t('lk.clearCache')}
|
||||
</Button>
|
||||
</div>
|
||||
{lookup.cache_ttl_days === 0 && (
|
||||
<p className="text-[11px] text-warning mt-2">{t('lk.cacheOff')}</p>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -252,7 +252,7 @@ const en: Dict = {
|
||||
'lk.display': 'Display', 'lk.qrzNickname': 'QRZ.com: use the nickname as the name', 'lk.qrzNicknameHint': 'Logs the name the operator goes by on the air rather than their registered first and last name. Falls back to the full name when no nickname is published. QRZ.com only — HamQTH already does this.', 'lk.showPics': 'Show QRZ profile pictures',
|
||||
'lk.showPicsHint': 'Display the photo from QRZ.com next to the worked-before matrix. May noticeably slow lookups during busy contest days; turn off if you operate fast.',
|
||||
'lk.cache': 'Cache', 'lk.cacheHint': "Successful lookups are cached locally so the same callsign isn't fetched twice. TTL controls how long before a fresh query is made.",
|
||||
'lk.ttl': 'TTL (days)', 'lk.clearing': 'Clearing…', 'lk.clearCache': 'Clear cache now',
|
||||
'lk.ttl': 'TTL (days, 0 = no cache)', 'lk.cacheOff': 'Cache off: every callsign is fetched from the provider each time it is typed. Slower, and it counts against a QRZ.com daily quota.', 'lk.clearing': 'Clearing…', 'lk.clearCache': 'Clear cache now',
|
||||
// Bands panel
|
||||
'bnd.hint': "Pick the bands you actually use. The entry strip, the band-slot grid and the band-map switcher only show what's on the right. Order on the right = display order.",
|
||||
'bnd.available': 'Available', 'bnd.allSelected': 'All catalog bands selected.', 'bnd.customPh': 'Custom band (e.g. 4m)',
|
||||
@@ -687,7 +687,7 @@ const fr: Dict = {
|
||||
'lk.display': 'Affichage', 'lk.qrzNickname': 'QRZ.com : utiliser le surnom comme nom', 'lk.qrzNicknameHint': "Enregistre le nom sous lequel l'opérateur se présente à l'air plutôt que ses prénom et nom déclarés. Retombe sur le nom complet si aucun surnom n'est publié. QRZ.com uniquement — HamQTH le fait déjà.", 'lk.showPics': 'Afficher les photos de profil QRZ',
|
||||
'lk.showPicsHint': 'Affiche la photo de QRZ.com à côté de la matrice « déjà contacté ». Peut ralentir les recherches en contest ; désactive si tu opères vite.',
|
||||
'lk.cache': 'Cache', 'lk.cacheHint': "Les recherches réussies sont mises en cache localement pour ne pas re-interroger le même indicatif. Le TTL contrôle la durée avant une nouvelle requête.",
|
||||
'lk.ttl': 'TTL (jours)', 'lk.clearing': 'Effacement…', 'lk.clearCache': 'Vider le cache',
|
||||
'lk.ttl': 'TTL (jours, 0 = pas de cache)', 'lk.cacheOff': 'Cache désactivé : chaque indicatif est demandé au fournisseur à chaque saisie. Plus lent, et cela compte dans le quota quotidien QRZ.com.', 'lk.clearing': 'Effacement…', 'lk.clearCache': 'Vider le cache',
|
||||
'bnd.hint': "Choisis les bandes que tu utilises vraiment. Le bandeau de saisie, la grille de bandes et le sélecteur de carte n'affichent que celles de droite. L'ordre à droite = ordre d'affichage.",
|
||||
'bnd.available': 'Disponibles', 'bnd.allSelected': 'Toutes les bandes du catalogue sont sélectionnées.', 'bnd.customPh': 'Bande perso (ex. 4m)',
|
||||
'bnd.selected': 'Sélectionnés ({n})', 'bnd.none': 'Aucune bande — choisis à gauche.',
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package lookup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A TTL of zero means no cache: nothing is read from it, and nothing is written
|
||||
// to it either.
|
||||
//
|
||||
// It is a real thing to want. An operator correcting their own QRZ record — or
|
||||
// chasing a DXpedition whose page changes during the operation — otherwise
|
||||
// waits out thirty days before OpsLog will ask again. Clearing the cache by
|
||||
// hand works once; switching it off is the setting for a session where the
|
||||
// answers are moving.
|
||||
func TestATTLOfZeroSwitchesTheCacheOff(t *testing.T) {
|
||||
c := testCache(t)
|
||||
ctx := context.Background()
|
||||
|
||||
if err := c.Put(ctx, Result{Callsign: "M0ABC", Name: "Ann", Source: "qrz"}); err != nil {
|
||||
t.Fatalf("put: %v", err)
|
||||
}
|
||||
if _, ok := c.Get(ctx, "M0ABC"); !ok {
|
||||
t.Fatal("the cache did not hold a fresh entry while switched on")
|
||||
}
|
||||
|
||||
c.SetTTL(0)
|
||||
if c.Enabled() {
|
||||
t.Error("Enabled() is true with a zero TTL")
|
||||
}
|
||||
if _, ok := c.Get(ctx, "M0ABC"); ok {
|
||||
t.Error("a cached entry was still returned with the cache off — the provider would never be asked again")
|
||||
}
|
||||
// And nothing new is stored: those rows would only sit there going stale,
|
||||
// waiting for the day the cache is switched back on.
|
||||
if err := c.Put(ctx, Result{Callsign: "M0XYZ", Name: "Bob", Source: "qrz"}); err != nil {
|
||||
t.Fatalf("put with the cache off: %v", err)
|
||||
}
|
||||
c.SetTTL(30 * 24 * time.Hour)
|
||||
if _, ok := c.Get(ctx, "M0XYZ"); ok {
|
||||
t.Error("a lookup made while the cache was off was written to it anyway")
|
||||
}
|
||||
// The entry from before it was switched off is still there — switching off
|
||||
// is not the same as clearing, and the Clear cache button remains the way to
|
||||
// throw the contents away.
|
||||
if _, ok := c.Get(ctx, "M0ABC"); !ok {
|
||||
t.Error("switching the cache off discarded what it already held")
|
||||
}
|
||||
}
|
||||
|
||||
// A negative lifetime is meaningless, and rounding it into either "off" or a
|
||||
// default would be a guess. It is ignored instead.
|
||||
func TestANegativeTTLIsIgnored(t *testing.T) {
|
||||
c := testCache(t)
|
||||
c.SetTTL(7 * 24 * time.Hour)
|
||||
c.SetTTL(-1)
|
||||
if !c.Enabled() {
|
||||
t.Fatal("a negative TTL switched the cache off")
|
||||
}
|
||||
if c.ttl != 7*24*time.Hour {
|
||||
t.Errorf("ttl = %v after a negative value, want the 7 days it already had", c.ttl)
|
||||
}
|
||||
}
|
||||
|
||||
// The constructor's zero is the DEFAULT, not "off": at startup the settings
|
||||
// have not been read, and beginning with no cache would hammer the provider for
|
||||
// the first seconds of every launch.
|
||||
func TestNewCacheWithZeroStillCaches(t *testing.T) {
|
||||
c := testCache(t) // built with NewCache(conn, 0)
|
||||
if !c.Enabled() {
|
||||
t.Error("a cache built with a zero TTL started switched off")
|
||||
}
|
||||
}
|
||||
@@ -432,11 +432,21 @@ func fillFromDXCC(r *Result, dxcc DXCCResolver) bool {
|
||||
// ----- Cache -----
|
||||
|
||||
// Cache is a SQLite-backed cache of lookup results with a TTL.
|
||||
//
|
||||
// A ttl of zero means NO CACHE: every lookup goes to the provider. That is a
|
||||
// real thing to want — an operator correcting their own QRZ record, or chasing
|
||||
// a DXpedition whose page changes during the operation, otherwise waits out the
|
||||
// cache before OpsLog will look again.
|
||||
type Cache struct {
|
||||
db *sql.DB
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
// NewCache builds the cache. A ttl of zero here is the CONSTRUCTOR default
|
||||
// (thirty days), not "off": at startup the settings have not been read yet, and
|
||||
// starting with no cache would hammer the provider for the first seconds of
|
||||
// every launch. Switching it off is a decision the operator makes, through
|
||||
// SetTTL, once their settings are known.
|
||||
func NewCache(db *sql.DB, ttl time.Duration) *Cache {
|
||||
if ttl <= 0 {
|
||||
ttl = 30 * 24 * time.Hour
|
||||
@@ -444,15 +454,25 @@ func NewCache(db *sql.DB, ttl time.Duration) *Cache {
|
||||
return &Cache{db: db, ttl: ttl}
|
||||
}
|
||||
|
||||
// SetTTL updates the cache TTL (e.g. when user changes settings).
|
||||
// SetTTL updates the cache lifetime.
|
||||
//
|
||||
// ZERO switches the cache OFF — nothing is read from it and nothing is written
|
||||
// to it. A NEGATIVE value is meaningless and is ignored, rather than being
|
||||
// rounded into one of the two meanings above.
|
||||
func (c *Cache) SetTTL(ttl time.Duration) {
|
||||
if ttl > 0 {
|
||||
if ttl >= 0 {
|
||||
c.ttl = ttl
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled reports whether anything is being cached at all.
|
||||
func (c *Cache) Enabled() bool { return c != nil && c.ttl > 0 }
|
||||
|
||||
// Get returns the cached result if present and not expired.
|
||||
func (c *Cache) Get(ctx context.Context, callsign string) (Result, bool) {
|
||||
if !c.Enabled() {
|
||||
return Result{}, false
|
||||
}
|
||||
row := c.db.QueryRowContext(ctx, `
|
||||
SELECT callsign, name, qth, address, state, cnty, country, grid,
|
||||
lat, lon, dxcc, cqz, ituz, cont, email, qsl_via, image_url,
|
||||
@@ -519,6 +539,11 @@ func (c *Cache) Get(ctx context.Context, callsign string) (Result, bool) {
|
||||
// Put upserts a lookup result. fetched_at is generated in Go (NowISO) so the
|
||||
// INSERT is backend-agnostic; the conflict tail is dialect-specific.
|
||||
func (c *Cache) Put(ctx context.Context, r Result) error {
|
||||
if !c.Enabled() {
|
||||
// Nothing reads it, so writing would only grow the table — and leave
|
||||
// stale rows waiting for the day the cache is switched back on.
|
||||
return nil
|
||||
}
|
||||
updateCols := []string{
|
||||
"name", "qth", "address", "state", "cnty",
|
||||
"country", "grid", "lat", "lon",
|
||||
|
||||
Reference in New Issue
Block a user