Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move cache to external library, add API-level configuration #3248

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions cmd/syft/internal/options/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"github.com/mitchellh/go-homedir"

"github.com/anchore/clio"
"github.com/anchore/syft/internal/cache"
"github.com/anchore/go-cache"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft"
)

// Cache provides configuration for the Syft caching behavior
Expand All @@ -36,25 +37,25 @@ func (c *Cache) PostLoad() error {
}
// if TTL is <= 0, disable caching entirely
if ttl <= 0 {
cache.SetManager(nil)
syft.SetCacheManager(nil)
return nil
}
// if dir == "" but we have a TTL, use an in-memory cache
if c.Dir == "" {
cache.SetManager(cache.NewInMemory(ttl))
syft.SetCacheManager(cache.NewInMemory(ttl))
return nil
}
dir, err := homedir.Expand(c.Dir)
if err != nil {
log.Warnf("unable to expand cache directory %s: %v", c.Dir, err)
cache.SetManager(cache.NewInMemory(ttl))
syft.SetCacheManager(cache.NewInMemory(ttl))
} else {
m, err := cache.NewFromDir(dir, ttl)
m, err := cache.NewFromDir(log.Get(), dir, ttl)
if err != nil {
log.Warnf("unable to get filesystem cache at %s: %v", c.Dir, err)
cache.SetManager(cache.NewInMemory(ttl))
syft.SetCacheManager(cache.NewInMemory(ttl))
} else {
cache.SetManager(m)
syft.SetCacheManager(m)
}
}
return nil
Expand Down
16 changes: 8 additions & 8 deletions cmd/syft/internal/options/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/cache"
"github.com/anchore/syft/internal/cachemanager"
)

func Test_defaultDir(t *testing.T) {
Expand Down Expand Up @@ -109,7 +109,7 @@ func Test_cacheOptions(t *testing.T) {
TTL: "0",
},
test: func(t *testing.T) {
c := cache.GetManager().GetCache("test-disable-1", "v-disable-1")
c := cachemanager.Get().GetCache("test-disable-1", "v-disable-1")
err := c.Write("key-disable-1", strings.NewReader("some-value-disable-1"))
require.NoError(t, err)
rdr, err := c.Read("key-disable-1")
Expand All @@ -124,7 +124,7 @@ func Test_cacheOptions(t *testing.T) {
TTL: "0s",
},
test: func(t *testing.T) {
c := cache.GetManager().GetCache("test-disable-2", "v-disable-2")
c := cachemanager.Get().GetCache("test-disable-2", "v-disable-2")
err := c.Write("key-disable-2", strings.NewReader("some-value-disable-2"))
require.NoError(t, err)
rdr, err := c.Read("key-disable-2")
Expand All @@ -140,7 +140,7 @@ func Test_cacheOptions(t *testing.T) {
TTL: "0d",
},
test: func(t *testing.T) {
c := cache.GetManager().GetCache("test-disable-3", "v-disable-3")
c := cachemanager.Get().GetCache("test-disable-3", "v-disable-3")
err := c.Write("key-disable-3", strings.NewReader("some-value-disable-3"))
require.NoError(t, err)
rdr, err := c.Read("key-disable-3")
Expand All @@ -155,7 +155,7 @@ func Test_cacheOptions(t *testing.T) {
TTL: "10m",
},
test: func(t *testing.T) {
c := cache.GetManager().GetCache("test-mem", "v-mem")
c := cachemanager.Get().GetCache("test-mem", "v-mem")
err := c.Write("key-mem", strings.NewReader("some-value-mem"))
require.NoError(t, err)
rdr, err := c.Read("key-mem")
Expand All @@ -175,7 +175,7 @@ func Test_cacheOptions(t *testing.T) {
TTL: "10m",
},
test: func(t *testing.T) {
c := cache.GetManager().GetCache("test-disk", "v-disk")
c := cachemanager.Get().GetCache("test-disk", "v-disk")
err := c.Write("key-disk", strings.NewReader("some-value-disk"))
require.NoError(t, err)
rdr, err := c.Read("key-disk")
Expand All @@ -191,8 +191,8 @@ func Test_cacheOptions(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
original := cache.GetManager()
defer cache.SetManager(original)
original := cachemanager.Get()
defer cachemanager.Set(original)

err := test.opts.PostLoad()
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ require (
github.com/BurntSushi/toml v1.4.0
github.com/OneOfOne/xxhash v1.2.8
github.com/adrg/xdg v0.5.0
github.com/anchore/go-cache v0.0.0-20240918212921-3fb02c7c559f
github.com/magiconair/properties v1.8.7
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/anchore/clio v0.0.0-20240522144804-d81e109008aa h1:pwlAn4O9SBUnlgfa69
github.com/anchore/clio v0.0.0-20240522144804-d81e109008aa/go.mod h1:nD3H5uIvjxlfmakOBgtyFQbk5Zjp3l538kxfpHPslzI=
github.com/anchore/fangs v0.0.0-20240903175602-e716ef12c23d h1:ZD4wdCBgJJzJybjTUIEiiupLF7B9H3WLuBTjspBO2Mc=
github.com/anchore/fangs v0.0.0-20240903175602-e716ef12c23d/go.mod h1:Xh4ObY3fmoMzOEVXwDtS1uK44JC7+nRD0n29/1KYFYg=
github.com/anchore/go-cache v0.0.0-20240918212921-3fb02c7c559f h1:Hgh7nFHNzYcHSt/k93SpisP8ArlaZxGbq4/U7fes+pE=
github.com/anchore/go-cache v0.0.0-20240918212921-3fb02c7c559f/go.mod h1:sX0O2JkumwyIZaAhNVG7RoQXZ1yF+J8sLyPpr8WouXI=
github.com/anchore/go-collections v0.0.0-20240216171411-9321230ce537 h1:GjNGuwK5jWjJMyVppBjYS54eOiiSNv4Ba869k4wh72Q=
github.com/anchore/go-collections v0.0.0-20240216171411-9321230ce537/go.mod h1:1aiktV46ATCkuVg0O573ZrH56BUawTECPETbZyBcqT8=
github.com/anchore/go-logger v0.0.0-20230725134548-c21dafa1ec5a h1:nJ2G8zWKASyVClGVgG7sfM5mwoZlZ2zYpIzN2OhjWkw=
Expand Down
51 changes: 0 additions & 51 deletions internal/cache/README.md

This file was deleted.

24 changes: 0 additions & 24 deletions internal/cache/bypass.go

This file was deleted.

18 changes: 0 additions & 18 deletions internal/cache/bypass_test.go

This file was deleted.

49 changes: 0 additions & 49 deletions internal/cache/cache.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/cache/cache_test.go

This file was deleted.

40 changes: 0 additions & 40 deletions internal/cache/error_resolver.go

This file was deleted.

47 changes: 0 additions & 47 deletions internal/cache/error_resolver_test.go

This file was deleted.

Loading
Loading