mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
Some checks failed
- 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.
95 lines
2.7 KiB
Go
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
|
|
}
|