Compare commits
1 Commits
main
...
background
Author | SHA1 | Date | |
---|---|---|---|
|
cc103043b4 |
@ -48,6 +48,10 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize trending system to start background calculations
|
||||||
|
fmt.Println("Initializing trending system...")
|
||||||
|
trending.Initialize(db.DB.DB)
|
||||||
|
|
||||||
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
|
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
|
||||||
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
|
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
|
||||||
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
||||||
|
@ -3,6 +3,7 @@ package trending
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.highperfocused.tech/highperfocused/lumina-relay/relay/cache"
|
"git.highperfocused.tech/highperfocused/lumina-relay/relay/cache"
|
||||||
@ -19,16 +20,48 @@ type Post struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
trendingCache = cache.New()
|
trendingCache = cache.New()
|
||||||
cacheDuration = 5 * time.Minute
|
cacheDuration = 35 * time.Minute // Slightly longer than update interval
|
||||||
|
updateInterval = 30 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Initialize sets up the background updating of trending calculations
|
||||||
|
// This should be called once at application startup
|
||||||
|
func Initialize(db *sql.DB) {
|
||||||
|
// Perform initial calculation
|
||||||
|
if _, err := calculateTrendingKind20(db); err != nil {
|
||||||
|
fmt.Printf("Error in initial trending calculation: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up periodic updates
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(updateInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
if _, err := calculateTrendingKind20(db); err != nil {
|
||||||
|
fmt.Printf("Error updating trending data: %v\n", err)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Successfully updated trending data at %s\n", time.Now().Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// GetTrendingKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
|
// GetTrendingKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
|
||||||
|
// It returns cached results that are updated periodically in the background
|
||||||
func GetTrendingKind20(db *sql.DB) ([]Post, error) {
|
func GetTrendingKind20(db *sql.DB) ([]Post, error) {
|
||||||
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
||||||
return cached.([]Post), nil
|
return cached.([]Post), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If cache is empty (which shouldn't happen with background updates),
|
||||||
|
// calculate on demand
|
||||||
|
return calculateTrendingKind20(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculateTrendingKind20 performs the actual calculation and updates the cache
|
||||||
|
func calculateTrendingKind20(db *sql.DB) ([]Post, error) {
|
||||||
query := `
|
query := `
|
||||||
WITH reactions AS (
|
WITH reactions AS (
|
||||||
SELECT
|
SELECT
|
||||||
|
Loading…
x
Reference in New Issue
Block a user