Documentation
¶
Index ¶
- func Close() error
- func ExampleCustomProvider()
- func ExampleDeleteByPattern()
- func ExampleGetOrSet()
- func ExampleInMemoryCache()
- func ExampleMemcacheCache()
- func ExampleRedisCache()
- 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 ¶
This section is empty.
Functions ¶
func ExampleCustomProvider ¶
func ExampleCustomProvider()
ExampleCustomProvider demonstrates using a custom provider.
func ExampleDeleteByPattern ¶
func ExampleDeleteByPattern()
ExampleDeleteByPattern demonstrates pattern-based deletion (Redis only).
func ExampleGetOrSet ¶
func ExampleGetOrSet()
ExampleGetOrSet demonstrates the GetOrSet pattern for lazy loading.
func ExampleInMemoryCache ¶
func ExampleInMemoryCache()
ExampleInMemoryCache demonstrates using the in-memory cache provider.
func ExampleMemcacheCache ¶
func ExampleMemcacheCache()
ExampleMemcacheCache demonstrates using the Memcache cache provider.
func ExampleRedisCache ¶
func ExampleRedisCache()
ExampleRedisCache demonstrates using the Redis cache provider.
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.
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.
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.
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
}
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.
func (*MemcacheProvider) Close ¶
func (m *MemcacheProvider) Close() error
Close closes the provider and releases any resources.
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 removes all keys matching the pattern. Note: Memcache does not support pattern-based deletion natively. This is a no-op for memcache and returns an error.
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.
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.
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 and releases any resources.
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.
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).
MaxSize int
// 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
}
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.
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.