cache

package
v0.0.0-...-215be59 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
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 Clear

func Clear(cacheDir string) error

Clear removes all cached data.

func GenerateCacheKey

func GenerateCacheKey(req *http.Request, host string, cacheAuth bool) (string, error)

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

func GenerateFilePath(urlPath string) string

GenerateFilePath generates a safe file path from URL path for storage.

func GetCacheSize

func GetCacheSize(cacheDir string) (int64, error)

GetCacheSize returns the total size of the cache in bytes.

func GetSizeForHost

func GetSizeForHost(cacheDir string, host string) (int64, error)

GetSizeForHost returns the total cache size for a specific host in bytes.

func HasFileExtension

func HasFileExtension(path string, extensions []string) bool

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

func IsExcludedExtension(path string, excludeExtensions []string) bool

IsExcludedExtension checks if a file extension is in the exclusion list.

func IsExcludedPath

func IsExcludedPath(path string, excludedPaths []string) bool

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

func SanitizePath(path string) string

SanitizePath makes a string safe for use as a file path (exported version).

func ShortKey

func ShortKey(key string) string

ShortKey returns a shortened version of the cache key for logging (first 16 chars).

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.

func StreamResponse

func StreamResponse(resp *http.Response, w http.ResponseWriter, hash string, host string, urlPath string, cache *Cache) error

StreamResponse streams an HTTP response to both the client and cache.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache represents an HTTP cache.

func New

func New(config config.CacheConfig) (*Cache, error)

New creates a new cache instance.

func (*Cache) CacheConfig

func (c *Cache) CacheConfig() config.CacheConfig

CacheConfig returns the cache configuration.

func (*Cache) CheckAndEvict

func (c *Cache) CheckAndEvict() error

CheckAndEvict checks if the cache exceeds the maximum size and evicts items if necessary.

func (*Cache) Close

func (c *Cache) Close() error

Close performs cleanup operations.

func (*Cache) Delete

func (c *Cache) Delete(key string) error

Delete removes a response from the cache.

func (*Cache) Directory

func (c *Cache) Directory() string

Directory returns the cache directory.

func (*Cache) EvictLRU

func (c *Cache) EvictLRU(requiredSpace int64) error

EvictLRU removes least recently used items to free up space.

func (*Cache) Exists

func (c *Cache) Exists(key string) bool

Exists checks if a cache key exists.

func (*Cache) Get

func (c *Cache) Get(key string) (*CachedResponse, error)

Get retrieves a cached response.

func (*Cache) GetSize

func (c *Cache) GetSize() (int64, error)

GetSize returns the total cache size in bytes.

func (*Cache) GetSizeForHost

func (c *Cache) GetSizeForHost(host string) (int64, error)

GetSizeForHost returns the cache size for a specific host in bytes.

func (*Cache) GetStats

func (c *Cache) GetStats() *CacheStats

GetStats returns cache statistics.

func (*Cache) Put

func (c *Cache) Put(hash string, host string, urlPath string, response *CachedResponse) error

Put stores a response in the cache.

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

func (c *Cache) RebuildIndex() error

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.

func (*CacheIndex) Validate

func (idx *CacheIndex) Validate(cacheDir string) ([]string, error)

Validate validates all entries in the index against actual files.

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

type CacheKeyInfo struct {
	Hash   string
	Method string
	Path   string
}

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) PutChunk

func (cc *ChunkedCache) PutChunk(hash string, chunkIndex int, data []byte) (string, error)

PutChunk stores a chunk of a 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

func NewStorage(cacheDir string) (*Storage, error)

NewStorage creates a new cache storage and loads the cache index.

func (*Storage) Close

func (s *Storage) Close() error

Close closes the storage and releases resources.

func (*Storage) Delete

func (s *Storage) Delete(hash string) error

Delete removes cached data from storage.

func (*Storage) Get

func (s *Storage) Get(hash string) (*CachedResponse, error)

Get reads cached data from storage using hash as key.

func (*Storage) GetCacheSize

func (s *Storage) GetCacheSize() int64

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

func (s *Storage) GetSizeForHost(host string) int64

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

func (s *Storage) Put(hash string, host string, urlPath string, response *CachedResponse) error

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

func (s *Storage) RebuildIndex() error

RebuildIndex rebuilds the cache index by validating all entries.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL