Cache Interface Abstraction for Things-Kit
This module defines the cache abstraction for Things-Kit applications. It contains only interfaces, no implementation.
go get github.com/things-kit/things-kit-cacheThe things-kit-cache package defines the contract that all cache implementations must follow. This allows applications to program against a stable interface while being free to choose any cache backend (Redis, Valkey, Memcached, in-memory, etc.).
The Cache interface defines operations for a distributed key-value cache:
type Cache interface {
// Basic operations
Get(ctx context.Context, key string) (string, error)
Set(ctx context.Context, key string, value string, expiration time.Duration) error
Delete(ctx context.Context, key string) error
Exists(ctx context.Context, key string) (bool, error)
// Binary data operations
GetBytes(ctx context.Context, key string) ([]byte, error)
SetBytes(ctx context.Context, key string, value []byte, expiration time.Duration) error
// Expiration management
Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
TTL(ctx context.Context, key string) (time.Duration, error)
// Connection management
Ping(ctx context.Context) error
Close() error
}The BatchCache interface extends Cache with batch operations for improved performance:
type BatchCache interface {
Cache
MGet(ctx context.Context, keys ...string) (map[string]string, error)
MSet(ctx context.Context, pairs map[string]string, expiration time.Duration) error
MDelete(ctx context.Context, keys ...string) error
}The things-kit-redis module provides a Redis-based implementation.
import (
"github.com/things-kit/things-kit/app"
"github.com/things-kit/things-kit-cache"
"github.com/things-kit/things-kit-redis"
)
func main() {
app.New(
viperconfig.Module,
logging.Module,
redis.Module, // Provides cache.Cache
fx.Provide(NewMyService),
).Run()
}MIT License - see LICENSE file for details