Documentation
¶
Index ¶
- Constants
- func Clear(cacheDir string) error
- func GenerateCacheKey(req *http.Request, host string, cacheAuth bool) (string, error)
- func GenerateFilePath(urlPath string) string
- func GetCacheSize(cacheDir string) (int64, error)
- func GetSizeForHost(cacheDir string, host string) (int64, error)
- func HasFileExtension(path string, extensions []string) bool
- func IsExcludedExtension(path string, excludeExtensions []string) bool
- func IsExcludedPath(path string, excludedPaths []string) bool
- func ResponseToHTTP(cached *CachedResponse, w http.ResponseWriter)
- func SanitizePath(path string) string
- func ShortKey(key string) string
- func ShouldCacheRequest(req *http.Request, cacheConfig config.CacheConfig) bool
- func ShouldCacheResponse(resp *http.Response, cacheConfig config.CacheConfig) bool
- func StreamResponse(resp *http.Response, w http.ResponseWriter, hash string, host string, ...) error
- type Cache
- func (c *Cache) CacheConfig() config.CacheConfig
- func (c *Cache) CheckAndEvict() error
- func (c *Cache) Close() error
- func (c *Cache) Delete(key string) error
- func (c *Cache) Directory() string
- func (c *Cache) EvictLRU(requiredSpace int64) error
- func (c *Cache) Exists(key string) bool
- func (c *Cache) Get(key string) (*CachedResponse, error)
- func (c *Cache) GetSize() (int64, error)
- func (c *Cache) GetSizeForHost(host string) (int64, error)
- func (c *Cache) GetStats() *CacheStats
- func (c *Cache) Put(hash string, host string, urlPath string, response *CachedResponse) error
- func (c *Cache) PutFromDisk(hash string, host string, urlPath string, statusCode int, ...) error
- func (c *Cache) RebuildIndex() error
- type CacheIndex
- func (idx *CacheIndex) Add(hash string, host string, urlPath string, filePath string, statusCode int, ...) error
- func (idx *CacheIndex) Cleanup(invalidHashes []string) error
- func (idx *CacheIndex) Close() error
- func (idx *CacheIndex) Count() int
- func (idx *CacheIndex) Delete(hash string) error
- func (idx *CacheIndex) Get(hash string) (*IndexEntry, bool)
- func (idx *CacheIndex) GetAll() map[string]*IndexEntry
- func (idx *CacheIndex) GetLRUEntry() (*IndexEntry, bool)
- func (idx *CacheIndex) Load() error
- func (idx *CacheIndex) Save() error
- func (idx *CacheIndex) TotalSize() int64
- func (idx *CacheIndex) TotalSizeForHost(host string) int64
- func (idx *CacheIndex) Validate(cacheDir string) ([]string, error)
- type CacheKey
- type CacheKeyInfo
- type CacheStats
- type CachedResponse
- type ChunkMetadata
- type ChunkedCache
- func (cc *ChunkedCache) Cleanup() error
- func (cc *ChunkedCache) Delete(hash string) error
- func (cc *ChunkedCache) GetChunk(hash string, chunkIndex int) ([]byte, error)
- func (cc *ChunkedCache) GetMetadata(hash string) (*ChunkMetadata, bool)
- func (cc *ChunkedCache) PutChunk(hash string, chunkIndex int, data []byte) (string, error)
- func (cc *ChunkedCache) SetMetadata(hash string, meta *ChunkMetadata) error
- func (cc *ChunkedCache) Validate(hash string) error
- type IndexEntry
- type ResponseMetadata
- type Storage
- func (s *Storage) Close() error
- func (s *Storage) Delete(hash string) error
- func (s *Storage) Get(hash string) (*CachedResponse, error)
- func (s *Storage) GetCacheSize() int64
- func (s *Storage) GetIndex() map[string]*IndexEntry
- func (s *Storage) GetSizeForHost(host string) int64
- func (s *Storage) GetStats() *CacheStats
- func (s *Storage) Put(hash string, host string, urlPath string, response *CachedResponse) error
- func (s *Storage) PutFromDisk(hash string, host string, urlPath string, statusCode int, ...) error
- func (s *Storage) RebuildIndex() error
Constants ¶
const ( // ChunkSize is the size of each cache chunk (1MB) ChunkSize = 1024 * 1024 // MetaFileName is the name of the chunk metadata file MetaFileName = "_chunks.json" )
Variables ¶
This section is empty.
Functions ¶
func GenerateCacheKey ¶
GenerateCacheKey generates a SHA256 cache key from an HTTP request. The key is based on the method, path, query, and authorization header (if enabled). Host is used for cache isolation but is NOT included in the hash generation. It returns a SHA256 hash that will be used as the cache key.
func GenerateFilePath ¶
GenerateFilePath generates a safe file path from URL path for storage.
func GetCacheSize ¶
GetCacheSize returns the total size of the cache in bytes.
func GetSizeForHost ¶
GetSizeForHost returns the total cache size for a specific host in bytes.
func HasFileExtension ¶
HasFileExtension checks if the path ends with any of the specified file extensions. It supports both simple extensions (e.g., ".zip") and compound extensions (e.g., ".tar.gz"). Compound extensions should be listed first to ensure correct matching. The comparison is case-insensitive.
func IsExcludedExtension ¶
IsExcludedExtension checks if a file extension is in the exclusion list.
func IsExcludedPath ¶
IsExcludedPath checks if a URL path is excluded. Patterns ending with "/" use prefix matching (exclude a directory). Exact paths without trailing "/" use exact matching (exclude a single file).
func ResponseToHTTP ¶
func ResponseToHTTP(cached *CachedResponse, w http.ResponseWriter)
ResponseToHTTP converts a CachedResponse to an HTTP response.
func SanitizePath ¶
SanitizePath makes a string safe for use as a file path (exported version).
func ShouldCacheRequest ¶
func ShouldCacheRequest(req *http.Request, cacheConfig config.CacheConfig) bool
ShouldCacheRequest determines if a request should be cached.
func ShouldCacheResponse ¶
func ShouldCacheResponse(resp *http.Response, cacheConfig config.CacheConfig) bool
ShouldCacheResponse determines if a response should be cached.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache represents an HTTP cache.
func (*Cache) CacheConfig ¶
func (c *Cache) CacheConfig() config.CacheConfig
CacheConfig returns the cache configuration.
func (*Cache) CheckAndEvict ¶
CheckAndEvict checks if the cache exceeds the maximum size and evicts items if necessary.
func (*Cache) Get ¶
func (c *Cache) Get(key string) (*CachedResponse, error)
Get retrieves a cached response.
func (*Cache) GetSizeForHost ¶
GetSizeForHost returns the cache size for a specific host in bytes.
func (*Cache) PutFromDisk ¶
func (c *Cache) PutFromDisk(hash string, host string, urlPath string, statusCode int, headers map[string][]string, filePath string, size int64) error
PutFromDisk stores a reference to a file that's already on disk. Used for streaming cache writes where the file is written directly.
func (*Cache) RebuildIndex ¶
RebuildIndex rebuilds the cache index by validating all entries.
type CacheIndex ¶
type CacheIndex struct {
// contains filtered or unexported fields
}
CacheIndex maintains the mapping between cache keys and cached files.
func NewCacheIndex ¶
func NewCacheIndex(cacheDir string) (*CacheIndex, error)
NewCacheIndex creates a new cache index backed by bbolt.
func (*CacheIndex) Add ¶
func (idx *CacheIndex) Add(hash string, host string, urlPath string, filePath string, statusCode int, headers map[string]string, size int64, crc32 uint32) error
Add adds a new entry to the index.
func (*CacheIndex) Cleanup ¶
func (idx *CacheIndex) Cleanup(invalidHashes []string) error
Cleanup removes invalid entries from the index.
func (*CacheIndex) Close ¶
func (idx *CacheIndex) Close() error
Close closes the underlying bbolt database.
func (*CacheIndex) Count ¶
func (idx *CacheIndex) Count() int
Count returns the number of entries in the index.
func (*CacheIndex) Delete ¶
func (idx *CacheIndex) Delete(hash string) error
Delete removes an entry from the index.
func (*CacheIndex) Get ¶
func (idx *CacheIndex) Get(hash string) (*IndexEntry, bool)
Get retrieves an entry from the index and updates its access time.
func (*CacheIndex) GetAll ¶
func (idx *CacheIndex) GetAll() map[string]*IndexEntry
GetAll returns all entries in the index.
func (*CacheIndex) GetLRUEntry ¶
func (idx *CacheIndex) GetLRUEntry() (*IndexEntry, bool)
GetLRUEntry returns the least recently used entry.
func (*CacheIndex) Load ¶
func (idx *CacheIndex) Load() error
Load is a no-op for bbolt (data is loaded on demand).
func (*CacheIndex) Save ¶
func (idx *CacheIndex) Save() error
Save is a no-op for bbolt (data is persisted on each write).
func (*CacheIndex) TotalSize ¶
func (idx *CacheIndex) TotalSize() int64
TotalSize returns the total size of all cached files.
func (*CacheIndex) TotalSizeForHost ¶
func (idx *CacheIndex) TotalSizeForHost(host string) int64
TotalSizeForHost returns the total size of cached files for a specific host.
type CacheKey ¶
type CacheKey struct {
Method string // HTTP method
Host string // Request host (for cache isolation, not used in hash)
Path string // Request path (without domain)
Query string // Query string
Authorization string // Authorization header (if caching auth requests)
}
CacheKey represents a cache key.
type CacheKeyInfo ¶
CacheKeyInfo represents parsed cache key information.
func ParseCacheKey ¶
func ParseCacheKey(hash string) (*CacheKeyInfo, error)
ParseCacheKey parses a cache key back into its components. This is useful for debugging and displaying cache information.
func (*CacheKeyInfo) URL ¶
func (cki *CacheKeyInfo) URL() string
URL returns the URL information (limited for hash-based keys).
type CacheStats ¶
type CacheStats struct {
TotalFiles int64 // Total number of cached files
TotalSizeMB float64 // Total cache size in MB
Hits int64 // Number of cache hits
Misses int64 // Number of cache misses
}
CacheStats represents cache statistics.
func GetStats ¶
func GetStats(cacheDir string) (*CacheStats, error)
GetStats returns cache statistics.
type CachedResponse ¶
type CachedResponse struct {
StatusCode int // HTTP status code
Headers map[string][]string // HTTP headers
Body []byte // Response body (for small files in memory)
FilePath string // File path (for large files on disk)
}
CachedResponse represents a cached HTTP response.
func WriteResponse ¶
func WriteResponse(resp *http.Response) (*CachedResponse, error)
WriteResponse writes an HTTP response to a CachedResponse structure.
type ChunkMetadata ¶
type ChunkMetadata struct {
TotalSize int64 `json:"totalSize"` // Total file size
ChunkCount int `json:"chunkCount"` // Number of chunks
ETag string `json:"etag"` // ETag header
LastModified string `json:"lastModified"` // Last-Modified header
ContentType string `json:"contentType"` // Content-Type header
ChunkHashes []string `json:"chunkHashes"` // SHA256 hash of each chunk
Created int64 `json:"created"` // Creation timestamp
Accessed int64 `json:"accessed"` // Last access timestamp
}
ChunkMetadata stores metadata for chunked files.
type ChunkedCache ¶
type ChunkedCache struct {
// contains filtered or unexported fields
}
ChunkedCache handles chunked file caching.
func NewChunkedCache ¶
func NewChunkedCache(cacheDir string) (*ChunkedCache, error)
NewChunkedCache creates a new chunked cache.
func (*ChunkedCache) Cleanup ¶
func (cc *ChunkedCache) Cleanup() error
Cleanup removes invalid chunks and metadata.
func (*ChunkedCache) Delete ¶
func (cc *ChunkedCache) Delete(hash string) error
Delete removes all chunks for a file.
func (*ChunkedCache) GetChunk ¶
func (cc *ChunkedCache) GetChunk(hash string, chunkIndex int) ([]byte, error)
GetChunk returns a specific chunk of a file.
func (*ChunkedCache) GetMetadata ¶
func (cc *ChunkedCache) GetMetadata(hash string) (*ChunkMetadata, bool)
GetMetadata retrieves metadata for a chunked file.
func (*ChunkedCache) SetMetadata ¶
func (cc *ChunkedCache) SetMetadata(hash string, meta *ChunkMetadata) error
SetMetadata sets the metadata for a chunked file.
func (*ChunkedCache) Validate ¶
func (cc *ChunkedCache) Validate(hash string) error
Validate validates all chunks for a file.
type IndexEntry ¶
type IndexEntry struct {
Hash string `json:"hash"`
Host string `json:"host"`
URLPath string `json:"urlPath"`
FilePath string `json:"filePath"`
Created int64 `json:"created"`
Accessed int64 `json:"accessed"`
Size int64 `json:"size"`
CRC32 uint32 `json:"crc32"`
Expires int64 `json:"expires"`
Metadata ResponseMetadata `json:"metadata"`
}
IndexEntry represents a single cache entry in the index.
type ResponseMetadata ¶
type ResponseMetadata struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
}
ResponseMetadata contains HTTP response metadata.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage manages cache storage on the file system. Uses SHA256 hash as cache key, stores files using URL path, and maintains a single index file.
func NewStorage ¶
NewStorage creates a new cache storage and loads the cache index.
func (*Storage) Get ¶
func (s *Storage) Get(hash string) (*CachedResponse, error)
Get reads cached data from storage using hash as key.
func (*Storage) GetCacheSize ¶
GetCacheSize returns the total size of the cache in bytes using the existing index.
func (*Storage) GetIndex ¶
func (s *Storage) GetIndex() map[string]*IndexEntry
GetIndex returns a copy of the cache index.
func (*Storage) GetSizeForHost ¶
GetSizeForHost returns the total cache size for a specific host in bytes using the existing index.
func (*Storage) GetStats ¶
func (s *Storage) GetStats() *CacheStats
GetStats returns cache statistics using the existing index.
func (*Storage) Put ¶
Put writes cached data to storage using hash as key and URL path as file location.
func (*Storage) PutFromDisk ¶
func (s *Storage) PutFromDisk(hash string, host string, urlPath string, statusCode int, headers map[string][]string, filePath string, size int64) error
PutFromDisk adds a cache entry for a file that's already on disk. This is used for streaming cache writes where the file is written directly.
func (*Storage) RebuildIndex ¶
RebuildIndex rebuilds the cache index by validating all entries.