Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type OpenTDFServer struct {
HTTPServer *http.Server
ConnectRPCInProcess *inProcessServer
ConnectRPC *ConnectRPC
CacheManager *cache.Manager[any]
CacheManager *cache.Manager

// To Deprecate: Use the TrustKeyIndex and TrustKeyManager instead
CryptoProvider *security.StandardCrypto
Expand All @@ -158,7 +158,7 @@ type inProcessServer struct {
*ConnectRPC
}

func NewOpenTDFServer(config Config, logger *logger.Logger, cacheManager *cache.Manager[any]) (*OpenTDFServer, error) {
func NewOpenTDFServer(config Config, logger *logger.Logger, cacheManager *cache.Manager) (*OpenTDFServer, error) {
var (
authN *auth.Authentication
err error
Expand Down
49 changes: 22 additions & 27 deletions service/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (

var ErrCacheMiss = errors.New("cache miss")

// Manager is a generic cache manager for any value type T.
type Manager[T any] struct {
cache *cache.Cache[interface{}]
// Manager is a cache manager for any value.
type Manager struct {
cache *cache.Cache[any]
}

// Cache is a generic cache implementation using gocache for any value type T.
type Cache[T any] struct {
manager *Manager[T]
// Cache is a cache implementation using gocache for any value type.
type Cache struct {
manager *Manager
serviceName string
cacheOptions Options
logger *logger.Logger
Expand All @@ -33,8 +33,8 @@ type Options struct {
Cost int64
}

// NewCacheManager creates a new generic cache manager using Ristretto as the backend.
func NewCacheManager[T any](maxCost int64) (*Manager[T], error) {
// NewCacheManager creates a new cache manager using Ristretto as the backend.
func NewCacheManager(maxCost int64) (*Manager, error) {
numCounters, bufferItems, err := EstimateRistrettoConfigParams(maxCost)
if err != nil {
return nil, err
Expand All @@ -49,20 +49,20 @@ func NewCacheManager[T any](maxCost int64) (*Manager[T], error) {
return nil, err
}
ristrettoStore := ristretto_store.NewRistretto(store)
return &Manager[T]{
cache: cache.New[interface{}](ristrettoStore),
return &Manager{
cache: cache.New[any](ristrettoStore),
}, nil
}

// NewCache creates a new generic Cache instance with the given service name and options.
// NewCache creates a new Cache client instance with the given service name and options.
// The purpose of this function is to create a new cache for a specific service.
// Because caching can be expensive we want to make sure there are some strict controls with
// how it is used.
func (c *Manager[T]) NewCache(serviceName string, log *logger.Logger, options Options) (*Cache[T], error) {
func (c *Manager) NewCache(serviceName string, log *logger.Logger, options Options) (*Cache, error) {
if log == nil {
return nil, errors.New("logger cannot be nil")
}
cache := &Cache[T]{
cache := &Cache{
manager: c,
serviceName: serviceName,
cacheOptions: options,
Expand All @@ -76,25 +76,20 @@ func (c *Manager[T]) NewCache(serviceName string, log *logger.Logger, options Op
return cache, nil
}

// Get retrieves a value from the cache and type asserts it to T.
func (c *Cache[T]) Get(ctx context.Context, key string) (T, error) {
var zero T
// Get retrieves a value from the cache
func (c *Cache) Get(ctx context.Context, key string) (any, error) {
val, err := c.manager.cache.Get(ctx, c.getKey(key))
if err != nil {
// All errors are a cache miss in the gocache library.
c.logger.Debug("cache miss", "key", key, "error", err)
return zero, ErrCacheMiss
return nil, ErrCacheMiss
}
c.logger.Debug("cache hit", "key", key)
typedVal, ok := val.(T)
if !ok {
return zero, errors.New("cache: type assertion failed")
}
return typedVal, nil
return val, nil
}

// Set stores a value of type T in the cache.
func (c *Cache[T]) Set(ctx context.Context, key string, object T, tags []string) error {
func (c *Cache) Set(ctx context.Context, key string, object any, tags []string) error {
tags = append(tags, c.getServiceTag())
opts := []store.Option{
store.WithTags(tags),
Expand All @@ -111,18 +106,18 @@ func (c *Cache[T]) Set(ctx context.Context, key string, object T, tags []string)
return nil
}

func (c *Cache[T]) Invalidate(ctx context.Context) error {
func (c *Cache) Invalidate(ctx context.Context) error {
return c.manager.cache.Invalidate(ctx, store.WithInvalidateTags([]string{c.getServiceTag()}))
}

func (c *Cache[T]) Delete(ctx context.Context, key string) error {
func (c *Cache) Delete(ctx context.Context, key string) error {
return c.manager.cache.Delete(ctx, c.getKey(key))
}

func (c *Cache[T]) getKey(key string) string {
func (c *Cache) getKey(key string) string {
return c.serviceName + ":" + key
}

func (c *Cache[T]) getServiceTag() string {
func (c *Cache) getServiceTag() string {
return "svc:" + c.serviceName
}
8 changes: 4 additions & 4 deletions service/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (

func TestNewCacheManager_ValidMaxCost(t *testing.T) {
maxCost := int64(1024 * 1024) // 1MB
manager, err := NewCacheManager[any](maxCost)
manager, err := NewCacheManager(maxCost)
require.NoError(t, err)
require.NotNil(t, manager)
require.NotNil(t, manager.cache)
}

func TestNewCacheManager_InvalidMaxCost(t *testing.T) {
// Ristretto requires MaxCost > 0, so use 0 or negative
_, err := NewCacheManager[any](0)
_, err := NewCacheManager(0)
require.Error(t, err)

_, err = NewCacheManager[any](-100)
_, err = NewCacheManager(-100)
require.Error(t, err)
}

func TestNewCacheManager_NewCacheIntegration(t *testing.T) {
maxCost := int64(1024 * 1024)
manager, err := NewCacheManager[any](maxCost)
manager, err := NewCacheManager(maxCost)
require.NoError(t, err)
require.NotNil(t, manager)

Expand Down
4 changes: 2 additions & 2 deletions service/pkg/server/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type startServicesParams struct {
client *sdk.SDK
logger *logging.Logger
reg serviceregistry.Registry
cacheManager *cache.Manager[any]
cacheManager *cache.Manager
keyManagers []trust.KeyManager
}

Expand Down Expand Up @@ -195,7 +195,7 @@ func startServices(ctx context.Context, params startServicesParams) (func(), err
}

// Check if the service supports and needs a cache
var cacheClient *cache.Cache[any]
var cacheClient *cache.Cache
if cacheSvc, ok := svc.(serviceregistry.CacheSupportedService); ok {
cacheClient, err = cacheManager.NewCache(ns, svcLogger, *cacheSvc.CacheOptions())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion service/pkg/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Start(f ...StartOptions) error {
if cfg.Server.Cache.Driver != "ristretto" {
return fmt.Errorf("unsupported cache driver: %s", cfg.Server.Cache.Driver)
}
cacheManager, err := cache.NewCacheManager[any](cfg.Server.Cache.RistrettoCache.MaxCostBytes())
cacheManager, err := cache.NewCacheManager(cfg.Server.Cache.RistrettoCache.MaxCostBytes())
if err != nil {
return fmt.Errorf("could not create cache manager: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions service/pkg/server/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func mockOpenTDFServer() (*server.OpenTDFServer, error) {
&logger.Logger{
Logger: slog.New(slog.Default().Handler()),
},
&cache.Manager[any]{},
&cache.Manager{},
)
}

Expand Down Expand Up @@ -367,7 +367,7 @@ func (s *StartTestSuite) Test_Start_When_Extra_Service_Registered() {
keyManagers: []trust.KeyManager{},
logger: logger,
reg: registry,
cacheManager: &cache.Manager[any]{},
cacheManager: &cache.Manager{},
})
require.NoError(t, err)
defer cleanup()
Expand Down
2 changes: 1 addition & 1 deletion service/pkg/serviceregistry/serviceregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type RegistrationParams struct {
trace.Tracer

// Cache is the cache that can be used to cache data. This cache is scoped to the service
Cache *cache.Cache[any]
Cache *cache.Cache

KeyManagers []trust.KeyManager

Expand Down
Loading