diff --git a/api.go b/api.go index b127335..88bf355 100644 --- a/api.go +++ b/api.go @@ -152,10 +152,10 @@ func handleStatistics(w http.ResponseWriter, r *http.Request) { flog.Trace("Database connection obtained") cacheKeyBytes, _ := json.Marshal(req) - cacheKey := string(cacheKeyBytes) + cacheKey := "stats:" + string(cacheKeyBytes) flog.Debug("Checking cache for key: %s", cacheKey) - cachedData, found := db.GetCachedStatistics(cacheKey) + cachedData, found := db.GetCacheEntry(cacheKey, 3*24*time.Hour) if found { flog.Info("Serving from cache") w.Header().Set("Content-Type", "application/json") @@ -186,7 +186,7 @@ func handleStatistics(w http.ResponseWriter, r *http.Request) { } flog.Debug("Storing in cache") - if err := db.CacheStatistics(cacheKey, responseData); err != nil { + if err := db.CacheEntry(cacheKey, responseData); err != nil { flog.Error("Failed to cache statistics: %v", err) } diff --git a/db.go b/db.go index 96fdc4f..5c9bab2 100644 --- a/db.go +++ b/db.go @@ -39,7 +39,6 @@ func (CacheEntry) TableName() string { return "cache_entries" } - type FitStatistics struct { TotalKillmails int64 ShipBreakdown map[int64]SystemStats // shipTypeID -> {Count, Percentage} @@ -65,8 +64,6 @@ type DB interface { SaveKillmails(killmails []Killmail) error InitTables() error QueryFits(params QueryParams) (*FitStatistics, error) - GetCachedStatistics(cacheKey string) ([]byte, bool) - CacheStatistics(cacheKey string, data []byte) error GetCacheEntry(cacheKey string, maxAge time.Duration) ([]byte, bool) CacheEntry(cacheKey string, data []byte) error SearchShips(query string, limit int) ([]models.InvType, error) @@ -206,11 +203,8 @@ func (db *DBWrapper) InitTables() error { return fmt.Errorf("failed to create fitted_modules table: %w", err) } - if err := db.gormDB.AutoMigrate(&StatisticsCache{}); err != nil { - return fmt.Errorf("failed to migrate cache table: %w", err) - } - - err := db.gormDB.Exec(`DELETE FROM statistics_cache WHERE created_at < datetime('now', '-3 days')`).Error + // Clean up old cache entries (older than 3 days) + err := db.gormDB.Exec(`DELETE FROM cache_entries WHERE created_at < datetime('now', '-3 days')`).Error if err != nil { return fmt.Errorf("failed to clean old cache: %w", err) } @@ -218,14 +212,6 @@ func (db *DBWrapper) InitTables() error { return nil } -func (db *DBWrapper) Raw(sql string, args ...any) *gorm.DB { - return db.gormDB.Raw(sql, args...) -} - -func (db *DBWrapper) DB() *gorm.DB { - return db.gormDB -} - func (db *DBWrapper) SaveKillmails(killmails []Killmail) error { ctx := context.Background() @@ -579,16 +565,6 @@ func (db *DBWrapper) QueryFits(params QueryParams) (*FitStatistics, error) { return stats, nil } -func (db *DBWrapper) GetCachedStatistics(cacheKey string) ([]byte, bool) { - // Use unified cache with "stats:" prefix - return db.GetCacheEntry("stats:"+cacheKey, 3*24*time.Hour) -} - -func (db *DBWrapper) CacheStatistics(cacheKey string, data []byte) error { - // Store in unified cache with "stats:" prefix - return db.CacheEntry("stats:"+cacheKey, data) -} - func (db *DBWrapper) GetCacheEntry(cacheKey string, maxAge time.Duration) ([]byte, bool) { var cached CacheEntry err := db.gormDB.