diff --git a/.gitignore b/.gitignore index 06a75af..86523a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -postgres/ \ No newline at end of file +postgres**/ \ No newline at end of file diff --git a/relay/main.go b/relay/main.go index be77293..3547137 100644 --- a/relay/main.go +++ b/relay/main.go @@ -5,14 +5,22 @@ import ( "fmt" "net/http" "os" + "strconv" "time" + "git.highperfocused.tech/highperfocused/lumina-relay/relay/cache" "git.highperfocused.tech/highperfocused/lumina-relay/relay/trending" "github.com/fiatjaf/eventstore/postgresql" "github.com/fiatjaf/khatru" "github.com/fiatjaf/khatru/policies" ) +// Cache for storing generic data like event counts +var dataCache = cache.New() + +const eventCountCacheKey = "total_event_count" +const eventCountCacheDuration = 1 * time.Minute + func getEnv(key, fallback string) string { if value, ok := os.LookupEnv(key); ok { return value @@ -20,6 +28,43 @@ func getEnv(key, fallback string) string { return fallback } +// Gets total event count, using cache if available +func getTotalEventCount(db *postgresql.PostgresBackend) (int, error) { + // Try getting from cache first + if cachedCount, ok := dataCache.Get(eventCountCacheKey); ok { + return cachedCount.(int), nil + } + + // If not in cache, query the database + count := 0 + row := db.DB.QueryRow("SELECT COUNT(*) FROM event") + if err := row.Scan(&count); err != nil { + return 0, err + } + + // Update the cache + dataCache.Set(eventCountCacheKey, count, eventCountCacheDuration) + return count, nil +} + +// Updates event count in the background periodically +func startEventCountUpdater(db *postgresql.PostgresBackend) { + go func() { + ticker := time.NewTicker(eventCountCacheDuration) + defer ticker.Stop() + for range ticker.C { + count := 0 + row := db.DB.QueryRow("SELECT COUNT(*) FROM event") + if err := row.Scan(&count); err != nil { + fmt.Printf("Error updating event count: %v\n", err) + continue + } + dataCache.Set(eventCountCacheKey, count, eventCountCacheDuration) + fmt.Printf("Updated event count cache: %d events\n", count) + } + }() +} + func main() { fmt.Print(` LUMINA RELAY @@ -48,6 +93,20 @@ func main() { panic(err) } + // Initialize trending system to start background calculations + fmt.Println("Initializing trending system...") + if err := trending.Initialize(db.DB.DB); err != nil { + fmt.Printf("Warning: Error initializing trending system: %v\n", err) + } + + // Initialize event count cache and start periodic updates + fmt.Println("Initializing event count cache...") + _, err := getTotalEventCount(&db) + if err != nil { + fmt.Printf("Warning: Error initializing event count cache: %v\n", err) + } + startEventCountUpdater(&db) + relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent) relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents) relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent) @@ -66,11 +125,11 @@ func main() { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/html") - // Query the total number of events - count := 0 - row := db.DB.QueryRow("SELECT COUNT(*) FROM event") - if err := row.Scan(&count); err != nil { - fmt.Printf("Error counting events: %v\n", err) + // Get event count from cache + count, err := getTotalEventCount(&db) + if err != nil { + fmt.Printf("Error getting event count: %v\n", err) + count = 0 // Fall back to zero if there's an error } // Improved HTML content with link to stats page @@ -120,6 +179,7 @@ func main() {
Number of events stored: %d
+