cache

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: GPL-2.0, GPL-2.0-only Imports: 17 Imported by: 0

Documentation

Overview

Package cache manages the local cache of downloaded rulesets.

Example (CacheWorkflow)
// Setup
apiClient := newMockAPIClient(func(w http.ResponseWriter, _ *http.Request) {
	var buf bytes.Buffer
	gzWriter := gzip.NewWriter(&buf)
	if err := writeRulesetTarball(gzWriter); err != nil {
		panic(err)
	}
	_ = gzWriter.Close()
	tarballData := buf.Bytes()

	checksum := CalculateSHA256(tarballData)
	w.Header().Set("scanoss-ruleset-name", "dca")
	w.Header().Set("scanoss-ruleset-version", "latest")
	w.Header().Set("x-checksum-sha256", checksum)
	w.Header().Set("scanoss-ruleset-created-at", time.Now().Format(time.RFC3339))
	w.WriteHeader(http.StatusOK)
	_, _ = w.Write(tarballData)
})

// Create client and manager
tempDir, err := os.MkdirTemp("", "crypto-finder-cache-example")
if err != nil {
	panic(err)
}
defer os.RemoveAll(tempDir)

manager := &Manager{
	apiClient: apiClient,
	cacheDir:  tempDir,
	noCache:   false,
}

// First call - downloads and caches
ctx := context.Background()
path1, _ := manager.GetRulesetPath(ctx, "dca", "latest")
fmt.Printf("First call downloaded to: %s\n", filepath.Base(filepath.Dir(path1)))

// Second call - uses cache
path2, _ := manager.GetRulesetPath(ctx, "dca", "latest")
fmt.Printf("Second call reused cache: %v\n", path1 == path2)

// With no-cache - forces re-download
manager.SetNoCache(true)
path3, _ := manager.GetRulesetPath(ctx, "dca", "latest")
fmt.Printf("No-cache call forced download: %s\n", filepath.Base(filepath.Dir(path3)))
Output:
First call downloaded to: dca
Second call reused cache: true
No-cache call forced download: dca

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateSHA256

func CalculateSHA256(data []byte) string

CalculateSHA256 calculates the SHA256 checksum of the given data.

func VerifyChecksum

func VerifyChecksum(data []byte, expectedChecksum string) error

VerifyChecksum verifies that the calculated checksum matches the expected checksum.

Types

type Manager

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

Manager manages the local cache of downloaded rulesets.

func NewManager

func NewManager(apiClient *api.Client) (*Manager, error)

NewManager creates a new cache manager.

func (*Manager) GetRulesetPath

func (m *Manager) GetRulesetPath(ctx context.Context, name, version string) (string, error)

GetRulesetPath returns the path to a cached ruleset If the ruleset is not cached or expired, it downloads it first. If noCache is enabled, it always downloads a fresh copy and updates the cache.

func (*Manager) SetMaxStaleCacheAge

func (m *Manager) SetMaxStaleCacheAge(maxAge time.Duration)

SetMaxStaleCacheAge sets the maximum age for stale cache fallback. If the cached rules are older than this duration, they will not be used as fallback.

func (*Manager) SetNoCache

func (m *Manager) SetNoCache(enabled bool)

SetNoCache enables or disables cache bypass mode. When enabled, the manager will always download fresh rulesets and update the cache, ignoring any existing cached rulesets.

func (*Manager) SetStrictMode

func (m *Manager) SetStrictMode(enabled bool)

SetStrictMode enables or disables strict mode. When enabled, the manager will fail if cache is expired and API is unreachable, instead of falling back to stale cache.

type Metadata

type Metadata struct {
	RulesetName    string    `json:"ruleset_name"`
	Version        string    `json:"version"`
	DownloadedAt   time.Time `json:"downloaded_at"`
	LastAccessed   time.Time `json:"last_accessed"`
	ChecksumSHA256 string    `json:"checksum_sha256"`
	TTLSeconds     int64     `json:"ttl_seconds"`
}

Metadata represents the .cache-meta.json file stored with each cached ruleset.

func LoadMetadata

func LoadMetadata(path string) (*Metadata, error)

LoadMetadata loads metadata from a .cache-meta.json file.

func NewMetadata

func NewMetadata(rulesetName, version, checksum string, ttlSeconds int64) *Metadata

NewMetadata creates a new metadata instance.

func (*Metadata) Age

func (m *Metadata) Age() time.Duration

Age returns the age of the cache (time since download).

func (*Metadata) IsExpired

func (m *Metadata) IsExpired() bool

IsExpired checks if the cache has expired based on TTL.

func (*Metadata) IsTooStale

func (m *Metadata) IsTooStale(maxAge time.Duration) bool

IsTooStale checks if the cache is older than the specified maximum age.

func (*Metadata) Save

func (m *Metadata) Save(path string) error

Save saves the metadata to a .cache-meta.json file.

func (*Metadata) UpdateLastAccessed

func (m *Metadata) UpdateLastAccessed()

UpdateLastAccessed updates the last accessed timestamp.

Jump to

Keyboard shortcuts

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