Documentation
¶
Index ¶
- Constants
- Variables
- func NewModule() modular.Module
- type CacheConfig
- type CacheEngine
- type CacheModule
- func (m *CacheModule) Constructor() modular.ModuleConstructor
- func (m *CacheModule) Delete(ctx context.Context, key string) error
- func (m *CacheModule) DeleteMulti(ctx context.Context, keys []string) error
- func (m *CacheModule) Dependencies() []string
- func (m *CacheModule) Flush(ctx context.Context) error
- func (m *CacheModule) Get(ctx context.Context, key string) (interface{}, bool)
- func (m *CacheModule) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (m *CacheModule) Init(app modular.Application) error
- func (m *CacheModule) Name() string
- func (m *CacheModule) ProvidesServices() []modular.ServiceProvider
- func (m *CacheModule) RegisterConfig(app modular.Application) error
- func (m *CacheModule) RequiresServices() []modular.ServiceDependency
- func (m *CacheModule) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (m *CacheModule) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- func (m *CacheModule) Start(ctx context.Context) error
- func (m *CacheModule) Stop(ctx context.Context) error
- type MemoryCache
- func (c *MemoryCache) Close(_ context.Context) error
- func (c *MemoryCache) Connect(ctx context.Context) error
- func (c *MemoryCache) Delete(_ context.Context, key string) error
- func (c *MemoryCache) DeleteMulti(ctx context.Context, keys []string) error
- func (c *MemoryCache) Flush(_ context.Context) error
- func (c *MemoryCache) Get(_ context.Context, key string) (interface{}, bool)
- func (c *MemoryCache) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (c *MemoryCache) Set(_ context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *MemoryCache) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- type RedisCache
- func (c *RedisCache) Close(ctx context.Context) error
- func (c *RedisCache) Connect(ctx context.Context) error
- func (c *RedisCache) Delete(ctx context.Context, key string) error
- func (c *RedisCache) DeleteMulti(ctx context.Context, keys []string) error
- func (c *RedisCache) Flush(ctx context.Context) error
- func (c *RedisCache) Get(ctx context.Context, key string) (interface{}, bool)
- func (c *RedisCache) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (c *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *RedisCache) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
Constants ¶
const ModuleName = "cache"
ModuleName is the name of this module
const ServiceName = "cache.provider"
ServiceName is the name of the service provided by this module
Variables ¶
var ( // ErrCacheFull is returned when the memory cache is full and cannot store new items ErrCacheFull = errors.New("cache is full") // ErrInvalidKey is returned when the key is invalid ErrInvalidKey = errors.New("invalid cache key") // ErrInvalidValue is returned when the value cannot be stored in the cache ErrInvalidValue = errors.New("invalid cache value") // ErrNotConnected is returned when an operation is attempted on a cache that is not connected ErrNotConnected = errors.New("cache not connected") )
Error definitions
Functions ¶
Types ¶
type CacheConfig ¶
type CacheConfig struct {
// Engine specifies the cache engine to use ("memory" or "redis")
Engine string `json:"engine" yaml:"engine" env:"ENGINE" validate:"oneof=memory redis"`
// DefaultTTL is the default time-to-live for cache entries in seconds
DefaultTTL int `json:"defaultTTL" yaml:"defaultTTL" env:"DEFAULT_TTL" validate:"min=1"`
// CleanupInterval is how often to clean up expired items (in seconds)
CleanupInterval int `json:"cleanupInterval" yaml:"cleanupInterval" env:"CLEANUP_INTERVAL" validate:"min=1"`
// MaxItems is the maximum number of items to store in memory cache
MaxItems int `json:"maxItems" yaml:"maxItems" env:"MAX_ITEMS" validate:"min=1"`
// Redis-specific configuration
RedisURL string `json:"redisURL" yaml:"redisURL" env:"REDIS_URL"`
RedisPassword string `json:"redisPassword" yaml:"redisPassword" env:"REDIS_PASSWORD"`
RedisDB int `json:"redisDB" yaml:"redisDB" env:"REDIS_DB" validate:"min=0"`
// ConnectionMaxAge is the maximum age of a connection in seconds
ConnectionMaxAge int `json:"connectionMaxAge" yaml:"connectionMaxAge" env:"CONNECTION_MAX_AGE" validate:"min=1"`
}
CacheConfig defines the configuration for the cache module
type CacheEngine ¶
type CacheEngine interface {
// Connect establishes connection to the cache backend
Connect(ctx context.Context) error
// Close closes the connection to the cache backend
Close(ctx context.Context) error
// Get retrieves an item from the cache
Get(ctx context.Context, key string) (interface{}, bool)
// Set stores an item in the cache with a TTL
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
// Delete removes an item from the cache
Delete(ctx context.Context, key string) error
// Flush removes all items from the cache
Flush(ctx context.Context) error
// GetMulti retrieves multiple items from the cache
GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
// SetMulti stores multiple items in the cache with a TTL
SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
// DeleteMulti removes multiple items from the cache
DeleteMulti(ctx context.Context, keys []string) error
}
CacheEngine defines the interface for cache engine implementations
type CacheModule ¶
type CacheModule struct {
// contains filtered or unexported fields
}
CacheModule represents the cache module
func (*CacheModule) Constructor ¶
func (m *CacheModule) Constructor() modular.ModuleConstructor
Constructor provides a dependency injection constructor for the module
func (*CacheModule) Delete ¶
func (m *CacheModule) Delete(ctx context.Context, key string) error
Delete removes an item from the cache
func (*CacheModule) DeleteMulti ¶
func (m *CacheModule) DeleteMulti(ctx context.Context, keys []string) error
DeleteMulti removes multiple items from the cache
func (*CacheModule) Dependencies ¶
func (m *CacheModule) Dependencies() []string
Dependencies returns the names of modules this module depends on
func (*CacheModule) Flush ¶
func (m *CacheModule) Flush(ctx context.Context) error
Flush removes all items from the cache
func (*CacheModule) Get ¶
func (m *CacheModule) Get(ctx context.Context, key string) (interface{}, bool)
Get retrieves a cached item by key
func (*CacheModule) Init ¶
func (m *CacheModule) Init(app modular.Application) error
Init initializes the module
func (*CacheModule) ProvidesServices ¶
func (m *CacheModule) ProvidesServices() []modular.ServiceProvider
ProvidesServices declares services provided by this module
func (*CacheModule) RegisterConfig ¶
func (m *CacheModule) RegisterConfig(app modular.Application) error
RegisterConfig registers the module's configuration structure
func (*CacheModule) RequiresServices ¶
func (m *CacheModule) RequiresServices() []modular.ServiceDependency
RequiresServices declares services required by this module
func (*CacheModule) Set ¶
func (m *CacheModule) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Set stores an item in the cache with an optional TTL
func (*CacheModule) SetMulti ¶
func (m *CacheModule) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
SetMulti stores multiple items in the cache
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache implements CacheEngine using in-memory storage
func NewMemoryCache ¶
func NewMemoryCache(config *CacheConfig) *MemoryCache
NewMemoryCache creates a new memory cache engine
func (*MemoryCache) Close ¶
func (c *MemoryCache) Close(_ context.Context) error
Close stops the memory cache cleanup routine
func (*MemoryCache) Connect ¶
func (c *MemoryCache) Connect(ctx context.Context) error
Connect initializes the memory cache
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(_ context.Context, key string) error
Delete removes an item from the cache
func (*MemoryCache) DeleteMulti ¶
func (c *MemoryCache) DeleteMulti(ctx context.Context, keys []string) error
DeleteMulti removes multiple items from the cache
func (*MemoryCache) Flush ¶
func (c *MemoryCache) Flush(_ context.Context) error
Flush removes all items from the cache
func (*MemoryCache) Get ¶
func (c *MemoryCache) Get(_ context.Context, key string) (interface{}, bool)
Get retrieves an item from the cache
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements CacheEngine using Redis
func NewRedisCache ¶
func NewRedisCache(config *CacheConfig) *RedisCache
NewRedisCache creates a new Redis cache engine
func (*RedisCache) Close ¶
func (c *RedisCache) Close(ctx context.Context) error
Close closes the connection to Redis
func (*RedisCache) Connect ¶
func (c *RedisCache) Connect(ctx context.Context) error
Connect establishes connection to Redis
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(ctx context.Context, key string) error
Delete removes an item from the Redis cache
func (*RedisCache) DeleteMulti ¶
func (c *RedisCache) DeleteMulti(ctx context.Context, keys []string) error
DeleteMulti removes multiple items from the Redis cache
func (*RedisCache) Flush ¶
func (c *RedisCache) Flush(ctx context.Context) error
Flush removes all items from the Redis cache
func (*RedisCache) Get ¶
func (c *RedisCache) Get(ctx context.Context, key string) (interface{}, bool)
Get retrieves an item from the Redis cache