Documentation
¶
Index ¶
- Variables
- func Close() error
- func Initialize(provider Provider)
- func SetDefaultCache(cache *Cache)
- func UseMemcache(config *MemcacheConfig) error
- func UseMemory(opts *Options) error
- func UseRedis(config *RedisConfig) error
- type Cache
- func (c *Cache) Clear(ctx context.Context) error
- func (c *Cache) Close() error
- func (c *Cache) Delete(ctx context.Context, key string) error
- func (c *Cache) DeleteByPattern(ctx context.Context, pattern string) error
- func (c *Cache) DeleteByTag(ctx context.Context, tag string) error
- func (c *Cache) Exists(ctx context.Context, key string) bool
- func (c *Cache) Get(ctx context.Context, key string, dest interface{}) error
- func (c *Cache) GetBytes(ctx context.Context, key string) ([]byte, error)
- func (c *Cache) GetOrSet(ctx context.Context, key string, dest interface{}, ttl time.Duration, ...) error
- func (c *Cache) Remember(ctx context.Context, key string, ttl time.Duration, ...) (interface{}, error)
- func (c *Cache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (c *Cache) SetBytes(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *Cache) SetBytesWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, ...) error
- func (c *Cache) SetWithTags(ctx context.Context, key string, value interface{}, ttl time.Duration, ...) error
- func (c *Cache) Stats(ctx context.Context) (*CacheStats, error)
- type CacheStats
- type MemcacheConfig
- type MemcacheProvider
- func (m *MemcacheProvider) Clear(ctx context.Context) error
- func (m *MemcacheProvider) Close() error
- func (m *MemcacheProvider) Delete(ctx context.Context, key string) error
- func (m *MemcacheProvider) DeleteByPattern(ctx context.Context, pattern string) error
- func (m *MemcacheProvider) DeleteByTag(ctx context.Context, tag string) error
- func (m *MemcacheProvider) Exists(ctx context.Context, key string) bool
- func (m *MemcacheProvider) Get(ctx context.Context, key string) ([]byte, bool)
- func (m *MemcacheProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (m *MemcacheProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, ...) error
- func (m *MemcacheProvider) Stats(ctx context.Context) (*CacheStats, error)
- type MemoryProvider
- func (m *MemoryProvider) CleanExpired(ctx context.Context) int
- func (m *MemoryProvider) Clear(ctx context.Context) error
- func (m *MemoryProvider) Close() error
- func (m *MemoryProvider) Delete(ctx context.Context, key string) error
- func (m *MemoryProvider) DeleteByPattern(ctx context.Context, pattern string) error
- func (m *MemoryProvider) DeleteByTag(ctx context.Context, tag string) error
- func (m *MemoryProvider) Exists(ctx context.Context, key string) bool
- func (m *MemoryProvider) Get(ctx context.Context, key string) ([]byte, bool)
- func (m *MemoryProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (m *MemoryProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, ...) error
- func (m *MemoryProvider) Stats(ctx context.Context) (*CacheStats, error)
- type Options
- type Provider
- type RedisConfig
- type RedisProvider
- func (r *RedisProvider) Clear(ctx context.Context) error
- func (r *RedisProvider) Close() error
- func (r *RedisProvider) Delete(ctx context.Context, key string) error
- func (r *RedisProvider) DeleteByPattern(ctx context.Context, pattern string) error
- func (r *RedisProvider) DeleteByTag(ctx context.Context, tag string) error
- func (r *RedisProvider) Exists(ctx context.Context, key string) bool
- func (r *RedisProvider) Get(ctx context.Context, key string) ([]byte, bool)
- func (r *RedisProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (r *RedisProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, ...) error
- func (r *RedisProvider) Stats(ctx context.Context) (*CacheStats, error)
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("cache: provider closed")
ErrClosed is returned by a provider that has been closed.
var ErrFlushNotAllowed = errors.New("cache: Clear flushes the entire server; set AllowFlush in the provider config to permit it")
ErrFlushNotAllowed is returned by Clear on shared-server providers (Redis, Memcache) unless AllowFlush is set in their config, because Clear flushes the whole server/DB.
var ErrNotFound = errors.New("cache: key not found")
ErrNotFound is returned when a key is not in the cache. The key is deliberately not included in the error: keys may embed credentials (e.g. session tokens).
Functions ¶
func Initialize ¶
func Initialize(provider Provider)
Initialize initializes the cache with a provider. If not called, the package will use an in-memory provider by default.
func SetDefaultCache ¶
func SetDefaultCache(cache *Cache)
SetDefaultCache sets a custom cache instance as the default cache. This is useful for testing or when you want to use a pre-configured cache instance. The caller keeps ownership of both the new and the displaced cache; neither is closed.
func UseMemcache ¶
func UseMemcache(config *MemcacheConfig) error
UseMemcache configures the cache to use Memcache storage.
func UseRedis ¶
func UseRedis(config *RedisConfig) error
UseRedis configures the cache to use Redis storage.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is the main cache manager that wraps a Provider.
func GetDefaultCache ¶
func GetDefaultCache() *Cache
GetDefaultCache returns the default cache instance. Initializes with in-memory provider if not already initialized. Safe for concurrent use.
func (*Cache) DeleteByPattern ¶
DeleteByPattern removes all keys matching the pattern.
func (*Cache) DeleteByTag ¶ added in v0.0.119
DeleteByTag removes all keys associated with the given tag.
func (*Cache) GetOrSet ¶
func (c *Cache) GetOrSet(ctx context.Context, key string, dest interface{}, ttl time.Duration, loader func() (interface{}, error)) error
GetOrSet retrieves a value from cache, or sets it if it doesn't exist. The loader function is called only if the key is not found in cache.
func (*Cache) Remember ¶
func (c *Cache) Remember(ctx context.Context, key string, ttl time.Duration, loader func() (interface{}, error)) (interface{}, error)
Remember is a convenience function that caches the result of a function call. It's similar to GetOrSet but returns the value directly.
WARNING: the returned type differs between a hit and a miss. On a hit the value is generic decoded JSON (map[string]interface{}, []interface{}, float64, string, ...); on a miss it is exactly what loader returned. Do not type-assert the result to a concrete type; prefer GetOrSet, which decodes into a typed destination.
func (*Cache) SetBytesWithTags ¶ added in v0.0.119
func (c *Cache) SetBytesWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error
SetBytesWithTags stores raw bytes in the cache with the specified TTL and tags.
type CacheStats ¶
type CacheStats struct {
Hits int64 `json:"hits"`
Misses int64 `json:"misses"`
Keys int64 `json:"keys"`
ProviderType string `json:"provider_type"`
ProviderStats map[string]any `json:"provider_stats,omitempty"`
}
CacheStats contains cache statistics.
type MemcacheConfig ¶
type MemcacheConfig struct {
// Servers is a list of memcache server addresses (e.g., "localhost:11211")
Servers []string
// MaxIdleConns is the maximum number of idle connections (default: 2)
MaxIdleConns int
// Timeout for connection operations (default: 1 second)
Timeout time.Duration
// Options contains general cache options
Options *Options
// AllowFlush permits Clear() to run flush_all, which wipes every key on every
// configured server (including data not owned by this cache). Off by default.
AllowFlush bool
}
MemcacheConfig contains Memcache-specific configuration.
type MemcacheProvider ¶
type MemcacheProvider struct {
// contains filtered or unexported fields
}
MemcacheProvider is a Memcache implementation of the Provider interface.
func NewMemcacheProvider ¶
func NewMemcacheProvider(config *MemcacheConfig) (*MemcacheProvider, error)
NewMemcacheProvider creates a new Memcache cache provider.
func (*MemcacheProvider) Clear ¶
func (m *MemcacheProvider) Clear(ctx context.Context) error
Clear removes all items from the cache. It runs flush_all on every configured server and therefore requires MemcacheConfig.AllowFlush.
func (*MemcacheProvider) Close ¶
func (m *MemcacheProvider) Close() error
Close closes the provider and releases idle connections.
func (*MemcacheProvider) Delete ¶
func (m *MemcacheProvider) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
func (*MemcacheProvider) DeleteByPattern ¶
func (m *MemcacheProvider) DeleteByPattern(ctx context.Context, pattern string) error
DeleteByPattern is not supported by Memcache; it always returns an error. Use tags (SetWithTags / DeleteByTag) for group invalidation instead.
func (*MemcacheProvider) DeleteByTag ¶ added in v0.0.119
func (m *MemcacheProvider) DeleteByTag(ctx context.Context, tag string) error
DeleteByTag removes all keys associated with the given tag.
func (*MemcacheProvider) Exists ¶
func (m *MemcacheProvider) Exists(ctx context.Context, key string) bool
Exists checks if a key exists in the cache.
func (*MemcacheProvider) Set ¶
func (m *MemcacheProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Set stores a value in the cache with the specified TTL.
func (*MemcacheProvider) SetWithTags ¶ added in v0.0.119
func (m *MemcacheProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error
SetWithTags stores a value in the cache with the specified TTL and tags. Note: Tag support in Memcache is limited and less efficient than Redis. The tag index is updated with compare-and-swap; if it cannot be updated the value is removed again and an error is returned, so an untracked entry is never left behind.
func (*MemcacheProvider) Stats ¶
func (m *MemcacheProvider) Stats(ctx context.Context) (*CacheStats, error)
Stats returns statistics about the cache provider. Note: Memcache provider returns limited statistics.
type MemoryProvider ¶
type MemoryProvider struct {
// contains filtered or unexported fields
}
MemoryProvider is an in-memory implementation of the Provider interface.
func NewMemoryProvider ¶
func NewMemoryProvider(opts *Options) *MemoryProvider
NewMemoryProvider creates a new in-memory cache provider. A MaxSize <= 0 selects the default (10000); use MaxSize -1 for an unbounded cache. A background goroutine removes expired items until Close is called.
func (*MemoryProvider) CleanExpired ¶
func (m *MemoryProvider) CleanExpired(ctx context.Context) int
CleanExpired removes all expired items from the cache.
func (*MemoryProvider) Clear ¶
func (m *MemoryProvider) Clear(ctx context.Context) error
Clear removes all items from the cache.
func (*MemoryProvider) Close ¶
func (m *MemoryProvider) Close() error
Close closes the provider, stops the janitor and releases stored items. Later writes return ErrClosed and reads report a miss.
func (*MemoryProvider) Delete ¶
func (m *MemoryProvider) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
func (*MemoryProvider) DeleteByPattern ¶
func (m *MemoryProvider) DeleteByPattern(ctx context.Context, pattern string) error
DeleteByPattern removes all keys matching the pattern. The pattern is a Go regular expression (unanchored); it is compiled before the lock is taken.
func (*MemoryProvider) DeleteByTag ¶ added in v0.0.119
func (m *MemoryProvider) DeleteByTag(ctx context.Context, tag string) error
DeleteByTag removes all keys associated with the given tag.
func (*MemoryProvider) Exists ¶
func (m *MemoryProvider) Exists(ctx context.Context, key string) bool
Exists checks if a key exists in the cache.
func (*MemoryProvider) Set ¶
func (m *MemoryProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Set stores a value in the cache with the specified TTL.
func (*MemoryProvider) SetWithTags ¶ added in v0.0.119
func (m *MemoryProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error
SetWithTags stores a value in the cache with the specified TTL and tags.
func (*MemoryProvider) Stats ¶
func (m *MemoryProvider) Stats(ctx context.Context) (*CacheStats, error)
Stats returns statistics about the cache provider.
type Options ¶
type Options struct {
// DefaultTTL is the default time-to-live for cache items.
DefaultTTL time.Duration
// MaxSize is the maximum number of items (for in-memory provider).
// 0 selects the default (10000); a negative value means unbounded.
MaxSize int
// CleanupInterval is how often the in-memory provider removes expired items
// (default: 1 minute).
CleanupInterval time.Duration
// EvictionPolicy determines how items are evicted (LRU, LFU, etc).
EvictionPolicy string
}
Options contains configuration options for cache providers.
type Provider ¶
type Provider interface {
// Get retrieves a value from the cache by key.
// Returns nil, false if key doesn't exist or is expired.
Get(ctx context.Context, key string) ([]byte, bool)
// Set stores a value in the cache with the specified TTL.
// If ttl is 0, the item never expires.
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// SetWithTags stores a value in the cache with the specified TTL and tags.
// Tags can be used to invalidate groups of related keys.
// If ttl is 0, the item never expires.
SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error
// Delete removes a key from the cache.
Delete(ctx context.Context, key string) error
// DeleteByTag removes all keys associated with the given tag.
DeleteByTag(ctx context.Context, tag string) error
// DeleteByPattern removes all keys matching the pattern.
// Pattern syntax depends on the provider implementation.
DeleteByPattern(ctx context.Context, pattern string) error
// Clear removes all items from the cache.
Clear(ctx context.Context) error
// Exists checks if a key exists in the cache.
Exists(ctx context.Context, key string) bool
// Close closes the provider and releases any resources.
Close() error
// Stats returns statistics about the cache provider.
Stats(ctx context.Context) (*CacheStats, error)
}
Provider defines the interface that all cache providers must implement.
type RedisConfig ¶
type RedisConfig struct {
// Host is the Redis server host (default: localhost)
Host string
// Port is the Redis server port (default: 6379)
Port int
// Password for Redis authentication (optional)
Password string
// DB is the Redis database number (default: 0)
DB int
// PoolSize is the maximum number of connections (default: 10)
PoolSize int
// Options contains general cache options
Options *Options
// AllowFlush permits Clear() to run FLUSHDB, which wipes the entire logical Redis DB
// (including data that is not owned by this cache). Off by default.
AllowFlush bool
}
RedisConfig contains Redis-specific configuration.
type RedisProvider ¶
type RedisProvider struct {
// contains filtered or unexported fields
}
RedisProvider is a Redis implementation of the Provider interface.
func NewRedisProvider ¶
func NewRedisProvider(config *RedisConfig) (*RedisProvider, error)
NewRedisProvider creates a new Redis cache provider.
func (*RedisProvider) Clear ¶
func (r *RedisProvider) Clear(ctx context.Context) error
Clear removes all items from the cache. It runs FLUSHDB and therefore requires RedisConfig.AllowFlush.
func (*RedisProvider) Close ¶
func (r *RedisProvider) Close() error
Close closes the provider and releases any resources.
func (*RedisProvider) Delete ¶
func (r *RedisProvider) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
func (*RedisProvider) DeleteByPattern ¶
func (r *RedisProvider) DeleteByPattern(ctx context.Context, pattern string) error
DeleteByPattern removes all keys matching the pattern.
func (*RedisProvider) DeleteByTag ¶ added in v0.0.119
func (r *RedisProvider) DeleteByTag(ctx context.Context, tag string) error
DeleteByTag removes all keys associated with the given tag.
func (*RedisProvider) Exists ¶
func (r *RedisProvider) Exists(ctx context.Context, key string) bool
Exists checks if a key exists in the cache.
func (*RedisProvider) SetWithTags ¶ added in v0.0.119
func (r *RedisProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error
SetWithTags stores a value in the cache with the specified TTL and tags.
func (*RedisProvider) Stats ¶
func (r *RedisProvider) Stats(ctx context.Context) (*CacheStats, error)
Stats returns statistics about the cache provider. Only an allowlist of numeric counters from INFO is exposed, not the raw output.