package store import ( "context" "database/sql" "errors" "fmt" "time" ) // AnswerCache is how a project reuses answers: for how long one is served // again to the same request, and whether that is any request's answer or // only one that would come out the same every time. Zero TTL reuses none. // // Only the setting lives in the store. The answers are held in each // gateway's memory, so turning this on writes no text anywhere. type AnswerCache struct { TTL time.Duration // Any reuses answers to requests sampled at a temperature above 0 // with no seed, which would otherwise each be asked again. Any bool } // SetAnswerCache turns reusing answers on for a project, or off with ttl 0. // The TTL is kept in whole seconds, at least one. func (s *DB) SetAnswerCache(ctx context.Context, projectID string, ttl time.Duration, anyAnswer bool) error { var secs any a := 0 if ttl > 0 { secs = max(int64(ttl/time.Second), 1) if anyAnswer { a = 1 } } res, err := s.db.ExecContext(ctx, `UPDATE projects SET cache_ttl_seconds = ?, cache_any = ? WHERE id = ?`, secs, a, projectID) if err != nil { return err } if n, _ := res.RowsAffected(); n == 0 { return fmt.Errorf("store: no project %q", projectID) } return nil } // AnswerCacheOf is a project's setting; zero when it reuses none. func (s *DB) AnswerCacheOf(ctx context.Context, projectID string) (AnswerCache, error) { var secs sql.NullInt64 var a int err := s.db.QueryRowContext(ctx, `SELECT cache_ttl_seconds, cache_any FROM projects WHERE id = ?`, projectID).Scan(&secs, &a) if errors.Is(err, sql.ErrNoRows) || err == nil && (!secs.Valid || secs.Int64 <= 0) { return AnswerCache{}, nil } if err != nil { return AnswerCache{}, err } return AnswerCache{TTL: time.Duration(secs.Int64) * time.Second, Any: a == 1}, nil }