Documentation
¶
Index ¶
- func BuildExtendedQueryCacheKey(tableName string, filters []common.FilterOption, sort []common.SortOption, ...) string
- func BuildQueryCacheKey(tableName string, filters []common.FilterOption, sort []common.SortOption, ...) string
- func Close() error
- func ExampleCustomProvider()
- func ExampleDeleteByPattern()
- func ExampleGetOrSet()
- func ExampleInMemoryCache()
- func ExampleMemcacheCache()
- func ExampleRedisCache()
- func GetQueryTotalCacheKey(hash string) string
- func Initialize(provider Provider)
- func InvalidateCacheForTable(ctx context.Context, tableName string) error
- 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) 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) Stats(ctx context.Context) (*CacheStats, error)
- type CacheStats
- type CachedTotal
- type ExpandOptionKey
- 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) 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) 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) 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) Stats(ctx context.Context) (*CacheStats, error)
- type Options
- type Provider
- type QueryCacheKey
- 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) 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) Stats(ctx context.Context) (*CacheStats, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildExtendedQueryCacheKey ¶
func BuildExtendedQueryCacheKey(tableName string, filters []common.FilterOption, sort []common.SortOption, customWhere, customOr string, expandOpts []interface{}, distinct bool, cursorFwd, cursorBwd string) string
BuildExtendedQueryCacheKey builds a cache key for extended query options (restheadspec) Includes expand, distinct, and cursor pagination options
func BuildQueryCacheKey ¶
func BuildQueryCacheKey(tableName string, filters []common.FilterOption, sort []common.SortOption, customWhere, customOr string) string
BuildQueryCacheKey builds a cache key from query parameters for total count caching This is used to cache the total count of records matching a query
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 GetQueryTotalCacheKey ¶
GetQueryTotalCacheKey returns a formatted cache key for storing/retrieving total count
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 InvalidateCacheForTable ¶
InvalidateCacheForTable removes all cached totals for a specific table This should be called when data in the table changes (insert/update/delete)
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) 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.
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 CachedTotal ¶
type CachedTotal struct {
Total int `json:"total"`
}
CachedTotal represents a cached total count
type ExpandOptionKey ¶
type ExpandOptionKey struct {
Relation string `json:"relation"`
Where string `json:"where,omitempty"`
}
ExpandOptionKey represents expand options for cache key
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) 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) 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) 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) 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
// Delete removes a key from the cache.
Delete(ctx context.Context, key 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 QueryCacheKey ¶
type QueryCacheKey struct {
TableName string `json:"table_name"`
Filters []common.FilterOption `json:"filters"`
Sort []common.SortOption `json:"sort"`
CustomSQLWhere string `json:"custom_sql_where,omitempty"`
CustomSQLOr string `json:"custom_sql_or,omitempty"`
Expand []ExpandOptionKey `json:"expand,omitempty"`
Distinct bool `json:"distinct,omitempty"`
CursorForward string `json:"cursor_forward,omitempty"`
CursorBackward string `json:"cursor_backward,omitempty"`
}
QueryCacheKey represents the components used to build a cache key for query total count
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) Exists ¶
func (r *RedisProvider) Exists(ctx context.Context, key string) bool
Exists checks if a key exists in the cache.
func (*RedisProvider) Stats ¶
func (r *RedisProvider) Stats(ctx context.Context) (*CacheStats, error)
Stats returns statistics about the cache provider.