Documentation
¶
Overview ¶
Package cache provides caching utilities including compression support.
Index ¶
- func CompressionRatio(original, compressed []byte) float64
- func ShouldCompress(data []byte) bool
- type CacheAsideStrategy
- func (s *CacheAsideStrategy) Clear(ctx context.Context, pattern string) error
- func (s *CacheAsideStrategy) Delete(ctx context.Context, key string) error
- func (s *CacheAsideStrategy) Get(ctx context.Context, key string) (interface{}, error)
- func (s *CacheAsideStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (s *CacheAsideStrategy) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (s *CacheAsideStrategy) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- type CacheConfig
- type CacheItem
- type CacheMetrics
- type CacheStats
- type CacheStrategy
- type CacheableQuery
- func (cq *CacheableQuery) Execute(ctx context.Context) (interface{}, error)
- func (cq *CacheableQuery) Invalidate(ctx context.Context) error
- func (cq *CacheableQuery) WithExecutor(executor func() (interface{}, error)) *CacheableQuery
- func (cq *CacheableQuery) WithTTL(ttl time.Duration) *CacheableQuery
- type CachedSearchResults
- type CachedSession
- type CachedTicket
- type CachedUser
- type DataLoader
- type LocalCache
- type LocalCacheConfig
- type LocalCacheItem
- type LocalCacheStats
- type Manager
- func (m *Manager) GetMultiTickets(ctx context.Context, ticketIDs []int64) (map[int64]*CachedTicket, error)
- func (m *Manager) GetQueueTickets(ctx context.Context, queueID int, page, limit int) ([]*CachedTicket, error)
- func (m *Manager) GetSearchResults(ctx context.Context, query string, filters map[string]interface{}) (*CachedSearchResults, error)
- func (m *Manager) GetSession(ctx context.Context, sessionID string) (*CachedSession, error)
- func (m *Manager) GetStats() *CacheStats
- func (m *Manager) GetStrategy(name string) CacheStrategy
- func (m *Manager) GetTicket(ctx context.Context, ticketID int64) (*CachedTicket, error)
- func (m *Manager) GetUser(ctx context.Context, userID int) (*CachedUser, error)
- func (m *Manager) InvalidateAll(ctx context.Context) error
- func (m *Manager) InvalidatePattern(ctx context.Context, pattern string) error
- func (m *Manager) InvalidateTicket(ctx context.Context, ticketID int64) error
- func (m *Manager) SetQueueTickets(ctx context.Context, queueID int, page, limit int, tickets []*CachedTicket) error
- func (m *Manager) SetSearchResults(ctx context.Context, query string, filters map[string]interface{}, ...) error
- func (m *Manager) SetSession(ctx context.Context, session *CachedSession) error
- func (m *Manager) SetTicket(ctx context.Context, ticket *CachedTicket) error
- func (m *Manager) SetUser(ctx context.Context, user *CachedUser) error
- func (m *Manager) WarmCache(ctx context.Context) error
- type ManagerConfig
- type QueryCache
- func (qc *QueryCache) Get(ctx context.Context, query string, args ...interface{}) (*QueryResult, error)
- func (qc *QueryCache) GetOrSet(ctx context.Context, query string, args []interface{}, ...) (interface{}, error)
- func (qc *QueryCache) GetStats(ctx context.Context) (map[string]interface{}, error)
- func (qc *QueryCache) InvalidateAll(ctx context.Context) error
- func (qc *QueryCache) InvalidateTable(ctx context.Context, table string) error
- func (qc *QueryCache) NewCacheableQuery(query string, args []interface{}) *CacheableQuery
- func (qc *QueryCache) Set(ctx context.Context, query string, result interface{}, args ...interface{}) error
- func (qc *QueryCache) WarmUp(ctx context.Context, queries []WarmUpQuery) error
- type QueryCacheConfig
- type QueryResult
- type ReadThroughStrategy
- func (s *ReadThroughStrategy) Clear(ctx context.Context, pattern string) error
- func (s *ReadThroughStrategy) Delete(ctx context.Context, key string) error
- func (s *ReadThroughStrategy) Get(ctx context.Context, key string) (interface{}, error)
- func (s *ReadThroughStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (s *ReadThroughStrategy) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (s *ReadThroughStrategy) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- type RedisCache
- func (rc *RedisCache) Clear(ctx context.Context, pattern string) error
- func (rc *RedisCache) Close() error
- func (rc *RedisCache) Delete(ctx context.Context, key string) error
- func (rc *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (rc *RedisCache) Expire(ctx context.Context, key string, ttl time.Duration) error
- func (rc *RedisCache) Get(ctx context.Context, key string) (interface{}, error)
- func (rc *RedisCache) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (rc *RedisCache) GetObject(ctx context.Context, key string, dest interface{}) error
- func (rc *RedisCache) Info(ctx context.Context) (map[string]interface{}, error)
- func (rc *RedisCache) Invalidate(ctx context.Context, pattern string) error
- func (rc *RedisCache) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) error
- func (rc *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (rc *RedisCache) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- func (rc *RedisCache) SetObject(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (rc *RedisCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type WarmUpQuery
- type WarmupFunc
- type WriteBehindStrategy
- func (s *WriteBehindStrategy) Clear(ctx context.Context, pattern string) error
- func (s *WriteBehindStrategy) Delete(ctx context.Context, key string) error
- func (s *WriteBehindStrategy) Get(ctx context.Context, key string) (interface{}, error)
- func (s *WriteBehindStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (s *WriteBehindStrategy) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (s *WriteBehindStrategy) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- func (s *WriteBehindStrategy) Start(ctx context.Context)
- type WriteThroughStrategy
- func (s *WriteThroughStrategy) Clear(ctx context.Context, pattern string) error
- func (s *WriteThroughStrategy) Delete(ctx context.Context, key string) error
- func (s *WriteThroughStrategy) Get(ctx context.Context, key string) (interface{}, error)
- func (s *WriteThroughStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (s *WriteThroughStrategy) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (s *WriteThroughStrategy) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompressionRatio ¶
CompressionRatio calculates the compression ratio.
func ShouldCompress ¶
ShouldCompress determines if data should be compressed based on size and type.
Types ¶
type CacheAsideStrategy ¶
type CacheAsideStrategy struct {
// contains filtered or unexported fields
}
CacheAsideStrategy leaves cache management to the application.
func (*CacheAsideStrategy) Clear ¶
func (s *CacheAsideStrategy) Clear(ctx context.Context, pattern string) error
Clear removes all matching keys.
func (*CacheAsideStrategy) Delete ¶
func (s *CacheAsideStrategy) Delete(ctx context.Context, key string) error
Delete removes from cache.
func (*CacheAsideStrategy) Get ¶
func (s *CacheAsideStrategy) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves from cache only.
func (*CacheAsideStrategy) GetMulti ¶
func (s *CacheAsideStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMulti retrieves multiple keys.
type CacheConfig ¶
type CacheConfig struct {
// Redis connection
RedisAddr []string
RedisPassword string
RedisDB int
// Cluster mode
ClusterMode bool
// Cache settings
DefaultTTL time.Duration
KeyPrefix string
Compression bool
MaxRetries int
// Pool settings
PoolSize int
MinIdleConns int
MaxConnAge time.Duration
// Timeouts
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}
CacheConfig defines cache configuration.
type CacheItem ¶
type CacheItem struct {
Key string
Value interface{}
TTL time.Duration
Tags []string
Version int
CreatedAt time.Time
UpdatedAt time.Time
}
CacheItem represents a cached item with metadata.
type CacheMetrics ¶
type CacheMetrics struct {
// contains filtered or unexported fields
}
CacheMetrics tracks cache performance.
type CacheStats ¶
type CacheStats struct {
LocalHits int64 `json:"local_hits"`
LocalMisses int64 `json:"local_misses"`
LocalSize int64 `json:"local_size"`
RedisHits int64 `json:"redis_hits"`
RedisMisses int64 `json:"redis_misses"`
HitRate float64 `json:"hit_rate"`
Timestamp time.Time `json:"timestamp"`
}
CacheStats represents cache statistics.
type CacheStrategy ¶
type CacheStrategy interface {
Get(ctx context.Context, key string) (interface{}, error)
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Clear(ctx context.Context, pattern string) error
GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
}
CacheStrategy defines how data should be cached.
type CacheableQuery ¶
type CacheableQuery struct {
// contains filtered or unexported fields
}
CacheableQuery wraps a database query for automatic caching.
func (*CacheableQuery) Execute ¶
func (cq *CacheableQuery) Execute(ctx context.Context) (interface{}, error)
Execute runs the query with caching.
func (*CacheableQuery) Invalidate ¶
func (cq *CacheableQuery) Invalidate(ctx context.Context) error
Invalidate removes this specific query from cache.
func (*CacheableQuery) WithExecutor ¶
func (cq *CacheableQuery) WithExecutor(executor func() (interface{}, error)) *CacheableQuery
WithExecutor sets the query executor.
func (*CacheableQuery) WithTTL ¶
func (cq *CacheableQuery) WithTTL(ttl time.Duration) *CacheableQuery
WithTTL sets a custom TTL for this query.
type CachedSearchResults ¶
type CachedSearchResults struct {
Query string `json:"query"`
TotalCount int `json:"total_count"`
Results []interface{} `json:"results"`
CachedAt time.Time `json:"cached_at"`
}
CachedSearchResults represents cached search results.
type CachedSession ¶
type CachedSession struct {
ID string `json:"id"`
UserID int `json:"user_id"`
Data map[string]interface{} `json:"data"`
ExpiresAt time.Time `json:"expires_at"`
CachedAt time.Time `json:"cached_at"`
}
CachedSession represents a cached session.
type CachedTicket ¶
type CachedTicket struct {
ID int64 `json:"id"`
TicketNumber string `json:"ticket_number"`
Title string `json:"title"`
QueueID int `json:"queue_id"`
StateID int `json:"state_id"`
PriorityID int `json:"priority_id"`
CustomerID string `json:"customer_id"`
OwnerID int `json:"owner_id"`
CachedAt time.Time `json:"cached_at"`
}
CachedTicket represents a cached ticket.
type CachedUser ¶
type CachedUser struct {
ID int `json:"id"`
Login string `json:"login"`
Email string `json:"email"`
Name string `json:"name"`
RoleIDs []int `json:"role_ids"`
GroupIDs []int `json:"group_ids"`
CachedAt time.Time `json:"cached_at"`
}
CachedUser represents a cached user.
type DataLoader ¶
type DataLoader interface {
Load(ctx context.Context, key string) (interface{}, error)
LoadMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
}
DataLoader fetches data from the source.
type LocalCache ¶
type LocalCache struct {
// contains filtered or unexported fields
}
LocalCache provides an in-memory cache with TTL support.
func NewLocalCache ¶
func NewLocalCache(config *LocalCacheConfig) *LocalCache
NewLocalCache creates a new local cache.
func (*LocalCache) Delete ¶
func (lc *LocalCache) Delete(key string)
Delete removes an item from local cache.
func (*LocalCache) Get ¶
func (lc *LocalCache) Get(key string) (interface{}, bool)
Get retrieves an item from local cache.
func (*LocalCache) GetStats ¶
func (lc *LocalCache) GetStats() *LocalCacheStats
GetStats returns cache statistics.
type LocalCacheConfig ¶
LocalCacheConfig defines local cache settings.
type LocalCacheItem ¶
type LocalCacheItem struct {
Value interface{}
ExpiresAt time.Time
AccessedAt time.Time
CreatedAt time.Time
Size int64
}
LocalCacheItem represents a cached item.
type LocalCacheStats ¶
type LocalCacheStats struct {
Hits int64
Misses int64
Sets int64
Deletes int64
Evictions int64
Size int64
}
LocalCacheStats tracks local cache statistics.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager provides a high-level caching interface with multiple strategies.
func NewManager ¶
func NewManager(config *ManagerConfig) (*Manager, error)
NewManager creates a new cache manager.
func (*Manager) GetMultiTickets ¶
func (m *Manager) GetMultiTickets(ctx context.Context, ticketIDs []int64) (map[int64]*CachedTicket, error)
GetMultiTickets retrieves multiple tickets from cache.
func (*Manager) GetQueueTickets ¶
func (m *Manager) GetQueueTickets(ctx context.Context, queueID int, page, limit int) ([]*CachedTicket, error)
GetQueueTickets retrieves tickets for a queue from cache.
func (*Manager) GetSearchResults ¶
func (m *Manager) GetSearchResults(ctx context.Context, query string, filters map[string]interface{}) (*CachedSearchResults, error)
GetSearchResults retrieves cached search results.
func (*Manager) GetSession ¶
GetSession retrieves a session from cache.
func (*Manager) GetStats ¶
func (m *Manager) GetStats() *CacheStats
GetStats returns cache statistics.
func (*Manager) GetStrategy ¶
func (m *Manager) GetStrategy(name string) CacheStrategy
GetStrategy returns a caching strategy by name.
func (*Manager) InvalidateAll ¶
InvalidateAll clears all caches.
func (*Manager) InvalidatePattern ¶
InvalidatePattern invalidates all keys matching a pattern.
func (*Manager) InvalidateTicket ¶
InvalidateTicket removes a ticket from cache.
func (*Manager) SetQueueTickets ¶
func (m *Manager) SetQueueTickets(ctx context.Context, queueID int, page, limit int, tickets []*CachedTicket) error
SetQueueTickets caches tickets for a queue.
func (*Manager) SetSearchResults ¶
func (m *Manager) SetSearchResults(ctx context.Context, query string, filters map[string]interface{}, results *CachedSearchResults) error
SetSearchResults caches search results.
func (*Manager) SetSession ¶
func (m *Manager) SetSession(ctx context.Context, session *CachedSession) error
SetSession caches a session.
func (*Manager) SetTicket ¶
func (m *Manager) SetTicket(ctx context.Context, ticket *CachedTicket) error
SetTicket caches a ticket.
type ManagerConfig ¶
type ManagerConfig struct {
RedisConfig *CacheConfig
LocalConfig *LocalCacheConfig
// Strategy settings
DefaultStrategy string
EnableLocal bool
EnableRedis bool
// Cache warming
WarmOnStartup bool
WarmupFunctions map[string]WarmupFunc
}
ManagerConfig defines cache manager configuration.
type QueryCache ¶
type QueryCache struct {
// contains filtered or unexported fields
}
QueryCache implements caching for database query results.
func NewQueryCache ¶
func NewQueryCache(config *QueryCacheConfig) *QueryCache
NewQueryCache creates a new query cache instance.
func (*QueryCache) Get ¶
func (qc *QueryCache) Get(ctx context.Context, query string, args ...interface{}) (*QueryResult, error)
Get retrieves a cached query result.
func (*QueryCache) GetOrSet ¶
func (qc *QueryCache) GetOrSet(ctx context.Context, query string, args []interface{}, executor func() (interface{}, error)) (interface{}, error)
GetOrSet attempts to get from cache, otherwise executes and caches.
func (*QueryCache) GetStats ¶
func (qc *QueryCache) GetStats(ctx context.Context) (map[string]interface{}, error)
GetStats returns cache statistics.
func (*QueryCache) InvalidateAll ¶
func (qc *QueryCache) InvalidateAll(ctx context.Context) error
InvalidateAll clears all query cache.
func (*QueryCache) InvalidateTable ¶
func (qc *QueryCache) InvalidateTable(ctx context.Context, table string) error
InvalidateTable invalidates all queries related to a table.
func (*QueryCache) NewCacheableQuery ¶
func (qc *QueryCache) NewCacheableQuery(query string, args []interface{}) *CacheableQuery
NewCacheableQuery creates a new cacheable query.
func (*QueryCache) Set ¶
func (qc *QueryCache) Set(ctx context.Context, query string, result interface{}, args ...interface{}) error
Set stores a query result in cache.
func (*QueryCache) WarmUp ¶
func (qc *QueryCache) WarmUp(ctx context.Context, queries []WarmUpQuery) error
WarmUp pre-loads commonly used queries.
type QueryCacheConfig ¶
type QueryCacheConfig struct {
RedisCache *RedisCache
DefaultTTL time.Duration
}
QueryCacheConfig defines query cache configuration.
type QueryResult ¶
type QueryResult struct {
Query string `json:"query"`
Args []interface{} `json:"args"`
Result interface{} `json:"result"`
Count int `json:"count"`
CachedAt time.Time `json:"cached_at"`
ExpiresAt time.Time `json:"expires_at"`
}
QueryResult represents a cached query result.
type ReadThroughStrategy ¶
type ReadThroughStrategy struct {
// contains filtered or unexported fields
}
ReadThroughStrategy reads from cache, fetches from source on miss.
func (*ReadThroughStrategy) Clear ¶
func (s *ReadThroughStrategy) Clear(ctx context.Context, pattern string) error
Clear removes all matching keys.
func (*ReadThroughStrategy) Delete ¶
func (s *ReadThroughStrategy) Delete(ctx context.Context, key string) error
Delete removes from cache.
func (*ReadThroughStrategy) Get ¶
func (s *ReadThroughStrategy) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves from cache or loads from source.
func (*ReadThroughStrategy) GetMulti ¶
func (s *ReadThroughStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMulti retrieves multiple keys.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements a distributed caching layer using Redis.
func NewRedisCache ¶
func NewRedisCache(config *CacheConfig) (*RedisCache, error)
NewRedisCache creates a new Redis cache instance.
func (*RedisCache) Clear ¶
func (rc *RedisCache) Clear(ctx context.Context, pattern string) error
Clear removes all keys matching a pattern.
func (*RedisCache) Delete ¶
func (rc *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a value from Redis.
func (*RedisCache) Get ¶
func (rc *RedisCache) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves a value from Redis.
func (*RedisCache) GetObject ¶
func (rc *RedisCache) GetObject(ctx context.Context, key string, dest interface{}) error
GetObject retrieves and unmarshals a value from cache.
func (*RedisCache) Info ¶
func (rc *RedisCache) Info(ctx context.Context) (map[string]interface{}, error)
Info returns cache information.
func (*RedisCache) Invalidate ¶
func (rc *RedisCache) Invalidate(ctx context.Context, pattern string) error
Invalidate removes cache entries matching pattern.
func (*RedisCache) Set ¶
func (rc *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Set stores a value in Redis.
func (*RedisCache) SetMulti ¶
func (rc *RedisCache) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
SetMulti stores multiple values in Redis.
type WarmUpQuery ¶
WarmUpQuery defines a query to warm up.
type WarmupFunc ¶
WarmupFunc is a function that pre-populates cache.
type WriteBehindStrategy ¶
type WriteBehindStrategy struct {
// contains filtered or unexported fields
}
WriteBehindStrategy writes to local immediately, Redis asynchronously.
func (*WriteBehindStrategy) Clear ¶
func (s *WriteBehindStrategy) Clear(ctx context.Context, pattern string) error
Clear removes all matching keys.
func (*WriteBehindStrategy) Delete ¶
func (s *WriteBehindStrategy) Delete(ctx context.Context, key string) error
Delete removes from local immediately, queues for Redis.
func (*WriteBehindStrategy) Get ¶
func (s *WriteBehindStrategy) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves from local first, then Redis.
func (*WriteBehindStrategy) GetMulti ¶
func (s *WriteBehindStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMulti retrieves multiple keys.
func (*WriteBehindStrategy) Set ¶
func (s *WriteBehindStrategy) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Set writes to local immediately, queues for Redis.
func (*WriteBehindStrategy) SetMulti ¶
func (s *WriteBehindStrategy) SetMulti(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
SetMulti sets multiple keys.
func (*WriteBehindStrategy) Start ¶
func (s *WriteBehindStrategy) Start(ctx context.Context)
Start starts the background writer.
type WriteThroughStrategy ¶
type WriteThroughStrategy struct {
// contains filtered or unexported fields
}
WriteThroughStrategy writes to both local and Redis synchronously.
func (*WriteThroughStrategy) Clear ¶
func (s *WriteThroughStrategy) Clear(ctx context.Context, pattern string) error
Clear removes all matching keys.
func (*WriteThroughStrategy) Delete ¶
func (s *WriteThroughStrategy) Delete(ctx context.Context, key string) error
Delete removes from both caches.
func (*WriteThroughStrategy) Get ¶
func (s *WriteThroughStrategy) Get(ctx context.Context, key string) (interface{}, error)
Get retrieves from local first, then Redis.
func (*WriteThroughStrategy) GetMulti ¶
func (s *WriteThroughStrategy) GetMulti(ctx context.Context, keys []string) (map[string]interface{}, error)
GetMulti retrieves multiple keys.