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 ¶
CalculateSHA256 calculates the SHA256 checksum of the given data.
func VerifyChecksum ¶
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 ¶
NewManager creates a new cache manager.
func (*Manager) GetRulesetPath ¶
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 ¶
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 ¶
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 ¶
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 ¶
LoadMetadata loads metadata from a .cache-meta.json file.
func NewMetadata ¶
NewMetadata creates a new metadata instance.
func (*Metadata) IsTooStale ¶
IsTooStale checks if the cache is older than the specified maximum age.
func (*Metadata) UpdateLastAccessed ¶
func (m *Metadata) UpdateLastAccessed()
UpdateLastAccessed updates the last accessed timestamp.