cache

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCacheKey

func GetCacheKey(prefix string, version string, additional ...string) string

GetCacheKey generates a cache key for GitHub Actions cache action. The key should be unique per cache entry and include version information.

func GetCacheKeyForBuild

func GetCacheKeyForBuild(goVersion string, buildTags string) string

GetCacheKeyForBuild generates a cache key for build artifacts.

func GetCacheKeyForDockerLayers

func GetCacheKeyForDockerLayers(imageName string, dockerfilePath string) string

GetCacheKeyForDockerLayers generates a cache key for Docker image layers.

func GetCacheKeyForModules

func GetCacheKeyForModules(goVersion string) string

GetCacheKeyForModules generates a cache key for Go modules.

func IsGitHubActions

func IsGitHubActions() bool

IsGitHubActions checks if we're running in GitHub Actions environment.

func MountCacheVolume

func MountCacheVolume(container *dagger.Container, cacheKey string, mountPath string, manager *DaggerCacheManager) *dagger.Container

MountCacheVolume mounts a cache volume in a container at the specified path.

Parameters:

  • container: The Dagger container.
  • cacheKey: The cache key.
  • mountPath: The path where to mount the cache.

Returns:

  • The container with cache volume mounted.

Types

type DaggerCacheManager

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

DaggerCacheManager manages cache for Dagger container layers. It uses Dagger's built-in cache volumes for efficient layer caching.

func NewDaggerCacheManager

func NewDaggerCacheManager(client *dagger.Client) *DaggerCacheManager

NewDaggerCacheManager creates a new DaggerCacheManager.

Parameters:

  • client: The Dagger client for cache operations.

Returns:

  • A new DaggerCacheManager instance.

func (*DaggerCacheManager) Get

func (d *DaggerCacheManager) Get(_ context.Context, _ string) ([]byte, error)

Get is not directly applicable to Dagger cache volumes. Dagger handles cache automatically through mounted volumes.

func (*DaggerCacheManager) GetCacheVolume

func (d *DaggerCacheManager) GetCacheVolume(key string) *dagger.CacheVolume

GetCacheVolume returns or creates a Dagger cache volume for the given key.

Parameters:

  • key: The cache key identifier.

Returns:

  • A Dagger CacheVolume for the key.

func (*DaggerCacheManager) GetWithFallback

func (d *DaggerCacheManager) GetWithFallback(_ context.Context, _ string, _ time.Duration, fallback func() ([]byte, error)) ([]byte, error)

GetWithFallback is not directly applicable to Dagger cache.

func (*DaggerCacheManager) Invalidate

func (d *DaggerCacheManager) Invalidate(_ context.Context, key string) error

Invalidate removes a cache volume (clears the cache).

func (*DaggerCacheManager) Set

Set is not directly applicable to Dagger cache volumes. Dagger handles cache automatically through mounted volumes.

func (*DaggerCacheManager) WarmUp

func (d *DaggerCacheManager) WarmUp(_ context.Context, keys []string, _ func(string) ([]byte, error)) error

WarmUp pre-warms Dagger cache by creating volumes.

type GitHubActionsCache

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

GitHubActionsCache implements Strategy using GitHub Actions cache. This cache strategy integrates with GitHub Actions cache action. Note: Actual cache operations are handled by GitHub Actions cache action, this implementation provides the interface and helper methods.

func NewGitHubActionsCache

func NewGitHubActionsCache(cacheDir string) *GitHubActionsCache

NewGitHubActionsCache creates a new GitHubActionsCache.

Parameters:

  • cacheDir: Directory where cache files are stored (defaults to .cache if empty).

Returns:

  • A new GitHubActionsCache instance.

func (*GitHubActionsCache) Get

func (c *GitHubActionsCache) Get(_ context.Context, key string) ([]byte, error)

Get retrieves a value from cache by key. In GitHub Actions, cache is restored by the cache action before this runs. This method reads from the local cache directory.

func (*GitHubActionsCache) GetWithFallback

func (c *GitHubActionsCache) GetWithFallback(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)

GetWithFallback retrieves a value from cache, or executes fallback function if not found.

func (*GitHubActionsCache) Invalidate

func (c *GitHubActionsCache) Invalidate(_ context.Context, key string) error

Invalidate removes a value from cache by key.

func (*GitHubActionsCache) Set

func (c *GitHubActionsCache) Set(_ context.Context, key string, data []byte, _ time.Duration) error

Set stores a value in cache with the specified key and TTL. In GitHub Actions, files are saved to cacheDir and the cache action saves them.

func (*GitHubActionsCache) WarmUp

func (c *GitHubActionsCache) WarmUp(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error

WarmUp pre-warms the cache with the specified keys.

type IncrementalCache

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

IncrementalCache implements Strategy for file-based incremental caching. It caches results based on file content hashes, invalidating only when files change.

func NewIncrementalCache

func NewIncrementalCache(baseDir string) *IncrementalCache

NewIncrementalCache creates a new IncrementalCache.

Parameters:

  • baseDir: Base directory for cache files (defaults to .cache/incremental if empty).

Returns:

  • A new IncrementalCache instance.

func (*IncrementalCache) Get

func (c *IncrementalCache) Get(_ context.Context, key string) ([]byte, error)

Get retrieves a value from cache by key.

func (*IncrementalCache) GetCacheKeyForFiles

func (c *IncrementalCache) GetCacheKeyForFiles(files []string) (string, error)

GetCacheKeyForFiles generates a cache key based on file paths and their content hashes.

Parameters:

  • files: List of file paths to include in the cache key.

Returns:

  • A cache key string based on file hashes.

func (*IncrementalCache) GetWithFallback

func (c *IncrementalCache) GetWithFallback(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)

GetWithFallback retrieves a value from cache, or executes fallback function if not found.

func (*IncrementalCache) Invalidate

func (c *IncrementalCache) Invalidate(_ context.Context, key string) error

Invalidate removes a value from cache by key.

func (*IncrementalCache) IsCacheValid

func (c *IncrementalCache) IsCacheValid(ctx context.Context, cacheKey string, files []string) (bool, error)

IsCacheValid checks if cache is still valid for the given files.

Parameters:

  • cacheKey: The cache key to check.
  • files: List of file paths to validate against.

Returns:

  • True if cache is valid, false otherwise.

func (*IncrementalCache) Set

func (c *IncrementalCache) Set(_ context.Context, key string, data []byte, _ time.Duration) error

Set stores a value in cache with the specified key and TTL.

func (*IncrementalCache) WarmUp

func (c *IncrementalCache) WarmUp(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error

WarmUp pre-warms the cache with the specified keys.

type LocalFileCache

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

LocalFileCache implements Strategy using local file system. Suitable for development and local CI/CD runs.

func NewLocalFileCache

func NewLocalFileCache(baseDir string) *LocalFileCache

NewLocalFileCache creates a new LocalFileCache.

Parameters:

  • baseDir: Base directory for cache files (defaults to .cache if empty).

Returns:

  • A new LocalFileCache instance.

func (*LocalFileCache) Get

func (c *LocalFileCache) Get(_ context.Context, key string) ([]byte, error)

Get retrieves a value from cache by key.

func (*LocalFileCache) GetWithFallback

func (c *LocalFileCache) GetWithFallback(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)

GetWithFallback retrieves a value from cache, or executes fallback function if not found.

func (*LocalFileCache) Invalidate

func (c *LocalFileCache) Invalidate(_ context.Context, key string) error

Invalidate removes a value from cache by key.

func (*LocalFileCache) Set

func (c *LocalFileCache) Set(_ context.Context, key string, data []byte, ttl time.Duration) error

Set stores a value in cache with the specified key and TTL.

func (*LocalFileCache) WarmUp

func (c *LocalFileCache) WarmUp(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error

WarmUp pre-warms the cache with the specified keys.

type Metrics

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

Metrics tracks cache performance metrics.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics creates a new Metrics instance.

func (*Metrics) GetAverageLatency

func (m *Metrics) GetAverageLatency() time.Duration

GetAverageLatency returns the average latency for cache operations.

func (*Metrics) GetErrors

func (m *Metrics) GetErrors() int64

GetErrors returns the number of cache errors.

func (*Metrics) GetHitRate

func (m *Metrics) GetHitRate() float64

GetHitRate returns the cache hit rate as a percentage.

func (*Metrics) GetHits

func (m *Metrics) GetHits() int64

GetHits returns the number of cache hits.

func (*Metrics) GetMisses

func (m *Metrics) GetMisses() int64

GetMisses returns the number of cache misses.

func (*Metrics) GetTotalRequests

func (m *Metrics) GetTotalRequests() int64

GetTotalRequests returns the total number of cache requests.

func (*Metrics) RecordError

func (m *Metrics) RecordError()

RecordError records a cache error.

func (*Metrics) RecordHit

func (m *Metrics) RecordHit(latency time.Duration)

RecordHit records a cache hit.

func (*Metrics) RecordMiss

func (m *Metrics) RecordMiss(latency time.Duration)

RecordMiss records a cache miss.

func (*Metrics) Reset

func (m *Metrics) Reset()

Reset resets all metrics to zero.

type MetricsWrapper

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

MetricsWrapper wraps a Strategy with metrics collection.

func NewMetricsWrapper

func NewMetricsWrapper(strategy Strategy) *MetricsWrapper

NewMetricsWrapper creates a new MetricsWrapper.

func (*MetricsWrapper) Get

func (w *MetricsWrapper) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from cache by key with metrics tracking.

func (*MetricsWrapper) GetMetrics

func (w *MetricsWrapper) GetMetrics() *Metrics

GetMetrics returns the metrics instance.

func (*MetricsWrapper) GetWithFallback

func (w *MetricsWrapper) GetWithFallback(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)

GetWithFallback retrieves a value with fallback and metrics tracking.

func (*MetricsWrapper) Invalidate

func (w *MetricsWrapper) Invalidate(ctx context.Context, key string) error

Invalidate removes a value from cache.

func (*MetricsWrapper) Set

func (w *MetricsWrapper) Set(ctx context.Context, key string, data []byte, ttl time.Duration) error

Set stores a value in cache with metrics tracking.

func (*MetricsWrapper) WarmUp

func (w *MetricsWrapper) WarmUp(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error

WarmUp pre-warms the cache.

type MockStrategy

type MockStrategy struct {
	GetFunc             func(ctx context.Context, key string) ([]byte, error)
	SetFunc             func(ctx context.Context, key string, data []byte, ttl time.Duration) error
	InvalidateFunc      func(ctx context.Context, key string) error
	GetWithFallbackFunc func(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)
	WarmUpFunc          func(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error
}

MockStrategy implements Strategy interface for testing.

func NewMockStrategy

func NewMockStrategy() *MockStrategy

NewMockStrategy creates a new MockStrategy with default behavior.

func (*MockStrategy) Get

func (m *MockStrategy) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from cache by key.

func (*MockStrategy) GetWithFallback

func (m *MockStrategy) GetWithFallback(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)

GetWithFallback retrieves a value from cache, or executes fallback function if not found.

func (*MockStrategy) Invalidate

func (m *MockStrategy) Invalidate(ctx context.Context, key string) error

Invalidate removes a value from cache by key.

func (*MockStrategy) Set

func (m *MockStrategy) Set(ctx context.Context, key string, data []byte, ttl time.Duration) error

Set stores a value in cache with the specified key and TTL.

func (*MockStrategy) WarmUp

func (m *MockStrategy) WarmUp(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error

WarmUp pre-warms the cache with the specified keys.

type Strategy

type Strategy interface {
	// Get retrieves a value from cache by key.
	Get(ctx context.Context, key string) ([]byte, error)

	// Set stores a value in cache with the specified key and TTL.
	Set(ctx context.Context, key string, data []byte, ttl time.Duration) error

	// Invalidate removes a value from cache by key.
	Invalidate(ctx context.Context, key string) error

	// GetWithFallback retrieves a value from cache, or executes fallback function if not found.
	GetWithFallback(ctx context.Context, key string, ttl time.Duration, fallback func() ([]byte, error)) ([]byte, error)

	// WarmUp pre-warms the cache with the specified keys.
	WarmUp(ctx context.Context, keys []string, generator func(string) ([]byte, error)) error
}

Strategy defines the interface for cache strategies. Different strategies can use local files, distributed caches, CI/CD caches, etc.

Jump to

Keyboard shortcuts

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