Actually make fucking interfaces

This commit is contained in:
2025-10-10 22:31:01 +02:00
parent da5133eef8
commit a09a01e7bc
8 changed files with 44 additions and 19 deletions

View File

@@ -8,30 +8,26 @@ import (
"fmt"
"time"
"go-eve-pi/types"
logger "git.site.quack-lab.dev/dave/cylogger"
)
// CacheEntry represents a cached API response
type CacheEntry struct {
ID uint `gorm:"primaryKey"`
URLHash string `gorm:"uniqueIndex"`
Response string `gorm:"type:text"`
CachedAt time.Time `gorm:"index"`
}
// CacheEntry is now defined in the types package
// CachedESI implements ESIInterface with caching
type CachedESI struct {
direct ESIInterface
db interface {
GetCacheEntry(urlHash string) (*CacheEntry, error)
SaveCacheEntry(entry *CacheEntry) error
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
SaveCacheEntry(entry *types.CacheEntry) error
}
}
// NewCachedESI creates a new CachedESI instance
func NewCachedESI(direct ESIInterface, db interface {
GetCacheEntry(urlHash string) (*CacheEntry, error)
SaveCacheEntry(entry *CacheEntry) error
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
SaveCacheEntry(entry *types.CacheEntry) error
}) *CachedESI {
return &CachedESI{
direct: direct,
@@ -113,13 +109,13 @@ func (c *CachedESI) getCachedResponse(url string, fetchFunc func() (interface{},
return result, nil
}
cacheEntry = CacheEntry{
cacheEntry = &types.CacheEntry{
URLHash: urlHash,
Response: string(responseBytes),
CachedAt: time.Now(),
}
if err := c.db.SaveCacheEntry(&cacheEntry); err != nil {
if err := c.db.SaveCacheEntry(cacheEntry); err != nil {
logger.Warning("Failed to cache response: %v", err)
}