Documentation
¶
Overview ¶
Package selectcache provides transport-layer caching middleware for Go's net.Listener that intercepts and caches network responses with configurable TTL management.
License: MIT
Package selectcache provides HTTP middleware for selective response caching using go-cache, with content-type based filtering to exclude HTML responses.
License: MIT (matches go-cache dependency)
Index ¶
- func GenerateCacheKey(method, path, query string, headers map[string]string) string
- type CacheConfig
- type CacheEntry
- type CacheMetrics
- func (m *CacheMetrics) GetStats() CacheStats
- func (m *CacheMetrics) IsEnabled() bool
- func (m *CacheMetrics) RecordDeletion()
- func (m *CacheMetrics) RecordError(errorType string)
- func (m *CacheMetrics) RecordEviction()
- func (m *CacheMetrics) RecordHit()
- func (m *CacheMetrics) RecordLookupTime(duration time.Duration)
- func (m *CacheMetrics) RecordMiss()
- func (m *CacheMetrics) RecordStore()
- func (m *CacheMetrics) RecordStoreTime(duration time.Duration)
- func (m *CacheMetrics) Reset()
- func (m *CacheMetrics) UpdateMemoryUsage(bytes uint64, entryCount int)
- type CacheStats
- type CachedResponse
- type CachingConnection
- func (c *CachingConnection) Close() error
- func (c *CachingConnection) GetStats() ConnectionStats
- func (c *CachingConnection) ID() string
- func (c *CachingConnection) LocalAddr() net.Addr
- func (c *CachingConnection) Read(b []byte) (int, error)
- func (c *CachingConnection) RemoteAddr() net.Addr
- func (c *CachingConnection) SetCloseCallback(callback func())
- func (c *CachingConnection) SetDeadline(t time.Time) error
- func (c *CachingConnection) SetReadDeadline(t time.Time) error
- func (c *CachingConnection) SetWriteDeadline(t time.Time) error
- func (c *CachingConnection) Write(b []byte) (int, error)
- type CachingListener
- func (cl *CachingListener) Accept() (net.Conn, error)
- func (cl *CachingListener) Addr() net.Addr
- func (cl *CachingListener) ClearCache()
- func (cl *CachingListener) Close() error
- func (cl *CachingListener) GetCache() *TTLCache
- func (cl *CachingListener) GetConfig() *CacheConfig
- func (cl *CachingListener) GetMetrics() *CacheMetrics
- func (cl *CachingListener) GetStats() ListenerStats
- func (cl *CachingListener) UpdateConfig(newConfig *CacheConfig) error
- type Config
- type ConnectionStats
- type ContentDetector
- func (d *ContentDetector) AnalyzeResponse(response []byte, headers http.Header, statusCode int) *ResponseAnalysis
- func (d *ContentDetector) DetectContentTypeFromBytes(data []byte) string
- func (d *ContentDetector) GetContentType(headers http.Header) string
- func (d *ContentDetector) IsHTMLContent(response []byte, headers http.Header) bool
- func (d *ContentDetector) ShouldCache(response []byte, headers http.Header, statusCode int) bool
- type ListenerStats
- type Middleware
- func (m *Middleware) Clear()
- func (m *Middleware) Delete(url string)
- func (m *Middleware) GetCacheForTesting() *cache.Cache
- func (m *Middleware) Handler(next http.Handler) http.Handler
- func (m *Middleware) HandlerFunc(next http.HandlerFunc) http.Handler
- func (m *Middleware) Stats() (itemCount int, hitCount, missCount uint64)
- type ResponseAnalysis
- type ResponseRecorder
- func (r *ResponseRecorder) Body() []byte
- func (r *ResponseRecorder) Header() http.Header
- func (r *ResponseRecorder) Headers() http.Header
- func (r *ResponseRecorder) Size() int
- func (r *ResponseRecorder) StatusCode() int
- func (r *ResponseRecorder) Write(data []byte) (int, error)
- func (r *ResponseRecorder) WriteHeader(code int)
- type TTLCache
- func (c *TTLCache) Clear()
- func (c *TTLCache) Close()
- func (c *TTLCache) Delete(key string) bool
- func (c *TTLCache) Get(key string) (*CacheEntry, bool)
- func (c *TTLCache) MemoryUsage() uint64
- func (c *TTLCache) Set(key string, data []byte, headers http.Header, ttl time.Duration) error
- func (c *TTLCache) Size() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CacheConfig ¶
type CacheConfig struct {
// DefaultTTL is the default time-to-live for cached responses
DefaultTTL time.Duration `json:"default_ttl"`
// ContentTypeTTLs provides per-content-type TTL overrides
ContentTypeTTLs map[string]time.Duration `json:"content_type_ttls"`
// MaxMemoryMB is the maximum memory in megabytes for cache storage
MaxMemoryMB int64 `json:"max_memory_mb"`
// MaxEntries is the maximum number of cache entries
MaxEntries int `json:"max_entries"`
// ExcludedTypes are content types that should never be cached
ExcludedTypes []string `json:"excluded_types"`
// EnableMetrics determines if performance metrics are collected
EnableMetrics bool `json:"enable_metrics"`
// CleanupInterval is how often expired entries are removed
CleanupInterval time.Duration `json:"cleanup_interval"`
// BufferSize is the size of the read buffer for connection analysis
BufferSize int `json:"buffer_size"`
// ConnectionTimeout is the maximum time to wait for connection analysis
ConnectionTimeout time.Duration `json:"connection_timeout"`
}
CacheConfig holds configuration for the transport-layer caching middleware
func DefaultCacheConfig ¶
func DefaultCacheConfig() *CacheConfig
DefaultCacheConfig returns sensible defaults for the caching middleware
func (*CacheConfig) GetTTLForContentType ¶
func (c *CacheConfig) GetTTLForContentType(contentType string) time.Duration
GetTTLForContentType returns the TTL for a specific content type, falling back to DefaultTTL if no specific TTL is configured
func (*CacheConfig) IsContentTypeExcluded ¶
func (c *CacheConfig) IsContentTypeExcluded(contentType string) bool
IsContentTypeExcluded checks if a content type should be excluded from caching
func (*CacheConfig) LoadFromJSON ¶
func (c *CacheConfig) LoadFromJSON(data []byte) error
LoadFromJSON loads configuration from JSON bytes
func (*CacheConfig) ToJSON ¶
func (c *CacheConfig) ToJSON() ([]byte, error)
ToJSON serializes the configuration to JSON
func (*CacheConfig) Validate ¶
func (c *CacheConfig) Validate() error
Validate checks the configuration for invalid values
type CacheEntry ¶
type CacheEntry struct {
// Response data
Data []byte `json:"data"`
Headers http.Header `json:"headers"`
// Timing information
ExpiresAt time.Time `json:"expires_at"`
AccessTime time.Time `json:"access_time"`
StoreTime time.Time `json:"store_time"`
// Metadata
ContentType string `json:"content_type"`
Size int `json:"size"`
}
CacheEntry represents a single cached response with metadata
func (*CacheEntry) IsExpired ¶
func (e *CacheEntry) IsExpired() bool
IsExpired checks if the cache entry has expired
func (*CacheEntry) UpdateAccessTime ¶
func (e *CacheEntry) UpdateAccessTime()
UpdateAccessTime updates the last access time for LRU tracking
type CacheMetrics ¶
type CacheMetrics struct {
// contains filtered or unexported fields
}
CacheMetrics collects performance metrics for the caching system
func NewCacheMetrics ¶
func NewCacheMetrics(enabled bool) *CacheMetrics
NewCacheMetrics creates a new metrics collector
func (*CacheMetrics) GetStats ¶
func (m *CacheMetrics) GetStats() CacheStats
GetStats returns a snapshot of current metrics
func (*CacheMetrics) IsEnabled ¶
func (m *CacheMetrics) IsEnabled() bool
IsEnabled returns whether metrics collection is enabled
func (*CacheMetrics) RecordDeletion ¶
func (m *CacheMetrics) RecordDeletion()
RecordDeletion increments the deletion counter
func (*CacheMetrics) RecordError ¶
func (m *CacheMetrics) RecordError(errorType string)
RecordError increments the error counter for a specific error type
func (*CacheMetrics) RecordEviction ¶
func (m *CacheMetrics) RecordEviction()
RecordEviction increments the eviction counter
func (*CacheMetrics) RecordHit ¶
func (m *CacheMetrics) RecordHit()
RecordHit increments the cache hit counter
func (*CacheMetrics) RecordLookupTime ¶
func (m *CacheMetrics) RecordLookupTime(duration time.Duration)
RecordLookupTime adds to the total lookup time for average calculation
func (*CacheMetrics) RecordMiss ¶
func (m *CacheMetrics) RecordMiss()
RecordMiss increments the cache miss counter
func (*CacheMetrics) RecordStore ¶
func (m *CacheMetrics) RecordStore()
RecordStore increments the cache store counter
func (*CacheMetrics) RecordStoreTime ¶
func (m *CacheMetrics) RecordStoreTime(duration time.Duration)
RecordStoreTime adds to the total store time for average calculation
func (*CacheMetrics) UpdateMemoryUsage ¶
func (m *CacheMetrics) UpdateMemoryUsage(bytes uint64, entryCount int)
UpdateMemoryUsage sets the current memory usage
type CacheStats ¶
type CacheStats struct {
// Operation counts
Hits uint64 `json:"hits"`
Misses uint64 `json:"misses"`
Stores uint64 `json:"stores"`
Evictions uint64 `json:"evictions"`
Deletions uint64 `json:"deletions"`
// Calculated metrics
HitRatio float64 `json:"hit_ratio"`
AvgLookupTimeMs float64 `json:"avg_lookup_time_ms"`
AvgStoreTimeMs float64 `json:"avg_store_time_ms"`
// Memory usage
TotalMemoryBytes uint64 `json:"total_memory_bytes"`
EntryCount int `json:"entry_count"`
AvgEntrySize uint64 `json:"avg_entry_size"`
// Error counts
Errors map[string]uint64 `json:"errors"`
}
CacheStats represents a snapshot of cache metrics
type CachedResponse ¶
CachedResponse represents a cached HTTP response
type CachingConnection ¶
CachingConnection wraps a net.Conn to provide transparent response caching
func NewCachingConnection ¶
func NewCachingConnection(conn net.Conn, cache *TTLCache, config *CacheConfig, metrics *CacheMetrics, detector *ContentDetector) *CachingConnection
NewCachingConnection creates a new caching connection wrapper
func (*CachingConnection) Close ¶
func (c *CachingConnection) Close() error
Close closes the connection and performs cleanup
func (*CachingConnection) GetStats ¶
func (c *CachingConnection) GetStats() ConnectionStats
GetStats returns statistics for this connection
func (*CachingConnection) ID ¶
func (c *CachingConnection) ID() string
ID returns the unique identifier for this connection
func (*CachingConnection) LocalAddr ¶
func (c *CachingConnection) LocalAddr() net.Addr
LocalAddr returns the local network address
func (*CachingConnection) Read ¶
func (c *CachingConnection) Read(b []byte) (int, error)
Read intercepts read operations to analyze requests
func (*CachingConnection) RemoteAddr ¶
func (c *CachingConnection) RemoteAddr() net.Addr
RemoteAddr returns the remote network address
func (*CachingConnection) SetCloseCallback ¶
func (c *CachingConnection) SetCloseCallback(callback func())
SetCloseCallback sets a callback function to be called when the connection closes
func (*CachingConnection) SetDeadline ¶
func (c *CachingConnection) SetDeadline(t time.Time) error
SetDeadline sets both read and write deadlines
func (*CachingConnection) SetReadDeadline ¶
func (c *CachingConnection) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline
func (*CachingConnection) SetWriteDeadline ¶
func (c *CachingConnection) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline
type CachingListener ¶
type CachingListener struct {
// contains filtered or unexported fields
}
CachingListener wraps a net.Listener to provide transparent caching of responses
func NewCachingListener ¶
func NewCachingListener(listener net.Listener, config *CacheConfig) *CachingListener
NewCachingListener creates a new caching listener that wraps the provided listener
func (*CachingListener) Accept ¶
func (cl *CachingListener) Accept() (net.Conn, error)
Accept waits for and returns the next connection to the listener
func (*CachingListener) Addr ¶
func (cl *CachingListener) Addr() net.Addr
Addr returns the listener's network address
func (*CachingListener) ClearCache ¶
func (cl *CachingListener) ClearCache()
ClearCache removes all cached entries
func (*CachingListener) Close ¶
func (cl *CachingListener) Close() error
Close closes the listener and all active connections
func (*CachingListener) GetCache ¶
func (cl *CachingListener) GetCache() *TTLCache
GetCache returns the underlying cache for management operations
func (*CachingListener) GetConfig ¶
func (cl *CachingListener) GetConfig() *CacheConfig
GetConfig returns the cache configuration
func (*CachingListener) GetMetrics ¶
func (cl *CachingListener) GetMetrics() *CacheMetrics
GetMetrics returns the current cache metrics
func (*CachingListener) GetStats ¶
func (cl *CachingListener) GetStats() ListenerStats
GetStats returns comprehensive statistics about the caching listener
func (*CachingListener) UpdateConfig ¶
func (cl *CachingListener) UpdateConfig(newConfig *CacheConfig) error
UpdateConfig updates the cache configuration (note: some changes require restart)
type Config ¶
type Config struct {
// DefaultTTL is the default time-to-live for cached responses
DefaultTTL time.Duration
// CleanupInterval is how often expired items are removed
CleanupInterval time.Duration
// ExcludeContentTypes are MIME types that should not be cached
// Default: ["text/html", "application/xhtml+xml"]
ExcludeContentTypes []string
// IncludeStatusCodes are HTTP status codes that should be cached
// Default: [200]
IncludeStatusCodes []int
}
Config holds configuration for the caching middleware
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns sensible defaults for the middleware
type ConnectionStats ¶
type ConnectionStats struct {
ID string `json:"id"`
IsHTTPRequest bool `json:"is_http_request"`
HasCacheKey bool `json:"has_cache_key"`
RequestSize int `json:"request_size"`
ResponseSize int `json:"response_size"`
LocalAddr string `json:"local_addr"`
RemoteAddr string `json:"remote_addr"`
Closed bool `json:"closed"`
}
ConnectionStats contains statistics for a caching connection
type ContentDetector ¶
type ContentDetector struct {
// contains filtered or unexported fields
}
ContentDetector analyzes response data to determine content type and cacheability
func NewContentDetector ¶
func NewContentDetector(config *CacheConfig) *ContentDetector
NewContentDetector creates a new content detector with the given configuration
func (*ContentDetector) AnalyzeResponse ¶
func (d *ContentDetector) AnalyzeResponse(response []byte, headers http.Header, statusCode int) *ResponseAnalysis
AnalyzeResponse performs comprehensive analysis of a response for caching decisions
func (*ContentDetector) DetectContentTypeFromBytes ¶
func (d *ContentDetector) DetectContentTypeFromBytes(data []byte) string
DetectContentTypeFromBytes attempts to detect content type from response bytes This is a fallback when Content-Type header is missing or unreliable
func (*ContentDetector) GetContentType ¶
func (d *ContentDetector) GetContentType(headers http.Header) string
GetContentType extracts and normalizes the content type from headers
func (*ContentDetector) IsHTMLContent ¶
func (d *ContentDetector) IsHTMLContent(response []byte, headers http.Header) bool
IsHTMLContent determines if the response contains HTML content based on Content-Type header
func (*ContentDetector) ShouldCache ¶
ShouldCache determines if a response should be cached based on content analysis
type ListenerStats ¶
type ListenerStats struct {
CacheStats CacheStats `json:"cache_stats"`
ActiveConnections int `json:"active_connections"`
CacheSize int `json:"cache_size"`
CacheMemoryUsage uint64 `json:"cache_memory_usage"`
ListenerAddress string `json:"listener_address"`
}
ListenerStats contains comprehensive statistics about the caching listener
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware provides selective HTTP response caching
func New ¶
func New(config Config) *Middleware
New creates a new selective cache middleware with the given configuration
func NewDefault ¶
func NewDefault() *Middleware
NewDefault creates a middleware with default settings: - 15 minute TTL - 5 minute cleanup interval - Excludes HTML content types - Only caches 200 status responses
func (*Middleware) Delete ¶
func (m *Middleware) Delete(url string)
Delete removes a specific cached response by URL It reconstructs the cache key using the same logic as requests
func (*Middleware) GetCacheForTesting ¶
func (m *Middleware) GetCacheForTesting() *cache.Cache
GetCacheForTesting returns the underlying cache for testing purposes This method should only be used in tests
func (*Middleware) Handler ¶
func (m *Middleware) Handler(next http.Handler) http.Handler
Handler wraps an http.Handler with selective caching
func (*Middleware) HandlerFunc ¶
func (m *Middleware) HandlerFunc(next http.HandlerFunc) http.Handler
HandlerFunc is a convenience method that wraps an http.HandlerFunc
func (*Middleware) Stats ¶
func (m *Middleware) Stats() (itemCount int, hitCount, missCount uint64)
Stats returns cache statistics
type ResponseAnalysis ¶
type ResponseAnalysis struct {
StatusCode int `json:"status_code"`
ContentType string `json:"content_type"`
Size int `json:"size"`
IsHTML bool `json:"is_html"`
IsCacheable bool `json:"is_cacheable"`
RecommendedTTL time.Duration `json:"recommended_ttl"`
}
ResponseAnalysis contains the results of response content analysis
type ResponseRecorder ¶
type ResponseRecorder struct {
http.ResponseWriter
// contains filtered or unexported fields
}
ResponseRecorder captures HTTP responses for caching
func NewResponseRecorder ¶
func NewResponseRecorder(w http.ResponseWriter, requestMethod string) *ResponseRecorder
NewResponseRecorder creates a new response recorder
func (*ResponseRecorder) Body ¶
func (r *ResponseRecorder) Body() []byte
Body returns a copy of the recorded response body
func (*ResponseRecorder) Header ¶
func (r *ResponseRecorder) Header() http.Header
Header returns the header map that will be sent by WriteHeader
func (*ResponseRecorder) Headers ¶
func (r *ResponseRecorder) Headers() http.Header
Headers returns a copy of the recorded headers
func (*ResponseRecorder) Size ¶
func (r *ResponseRecorder) Size() int
Size returns the size of the recorded response body
func (*ResponseRecorder) StatusCode ¶
func (r *ResponseRecorder) StatusCode() int
StatusCode returns the recorded status code
func (*ResponseRecorder) Write ¶
func (r *ResponseRecorder) Write(data []byte) (int, error)
Write captures the response body and writes to underlying ResponseWriter
func (*ResponseRecorder) WriteHeader ¶
func (r *ResponseRecorder) WriteHeader(code int)
WriteHeader captures the status code and headers
type TTLCache ¶
type TTLCache struct {
// contains filtered or unexported fields
}
TTLCache provides thread-safe cache storage with TTL and LRU eviction
func NewTTLCache ¶
func NewTTLCache(config *CacheConfig, metrics *CacheMetrics) *TTLCache
NewTTLCache creates a new TTL cache with the given configuration
func (*TTLCache) Close ¶
func (c *TTLCache) Close()
Close stops the cleanup routine and releases resources
func (*TTLCache) Get ¶
func (c *TTLCache) Get(key string) (*CacheEntry, bool)
Get retrieves a cached entry by key
func (*TTLCache) MemoryUsage ¶
MemoryUsage returns the current memory usage in bytes