tercul-backend/internal/platform/cache/cache.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

95 lines
2.7 KiB
Go

package cache
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
)
// Cache defines the interface for caching operations
type Cache interface {
// Get retrieves a value from the cache
Get(ctx context.Context, key string, value interface{}) error
// Set stores a value in the cache with an optional expiration
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
// Delete removes a value from the cache
Delete(ctx context.Context, key string) error
// Clear removes all values from the cache
Clear(ctx context.Context) error
// GetMulti retrieves multiple values from the cache
GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
// SetMulti stores multiple values in the cache with an optional expiration
SetMulti(ctx context.Context, items map[string]interface{}, expiration time.Duration) error
}
// Item represents a cache item with metadata
type Item struct {
Key string
Value interface{}
Expiration time.Duration
}
// MarshalBinary implements the encoding.BinaryMarshaler interface
func (i *Item) MarshalBinary() ([]byte, error) {
return json.Marshal(i.Value)
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface
func (i *Item) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &i.Value)
}
// KeyGenerator generates cache keys for different types of data
type KeyGenerator interface {
// EntityKey generates a key for an entity by ID
EntityKey(entityType string, id uuid.UUID) string
// ListKey generates a key for a list of entities
ListKey(entityType string, page, pageSize int) string
// QueryKey generates a key for a custom query
QueryKey(entityType, queryName string, params ...interface{}) string
}
// DefaultKeyGenerator implements the KeyGenerator interface
type DefaultKeyGenerator struct {
Prefix string
}
// NewDefaultKeyGenerator creates a new DefaultKeyGenerator
func NewDefaultKeyGenerator(prefix string) *DefaultKeyGenerator {
if prefix == "" {
prefix = "tercul:"
}
return &DefaultKeyGenerator{
Prefix: prefix,
}
}
// EntityKey generates a key for an entity by ID
func (g *DefaultKeyGenerator) EntityKey(entityType string, id uuid.UUID) string {
return g.Prefix + entityType + ":id:" + id.String()
}
// ListKey generates a key for a list of entities
func (g *DefaultKeyGenerator) ListKey(entityType string, page, pageSize int) string {
return g.Prefix + entityType + ":list:" + fmt.Sprintf("%d:%d", page, pageSize)
}
// QueryKey generates a key for a custom query
func (g *DefaultKeyGenerator) QueryKey(entityType, queryName string, params ...interface{}) string {
key := g.Prefix + entityType + ":" + queryName
for _, param := range params {
key += ":" + fmt.Sprintf("%v", param)
}
return key
}