syft/internal/cache
anchore-actions-token-generator[bot] 4b7ae0ed3b
chore(deps): update tools to latest versions (#3121)
* chore(deps): update tools to latest versions

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: update code to reflect new linter settings for error messages

Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>

---------

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
Co-authored-by: spiffcs <32073428+spiffcs@users.noreply.github.com>
2024-08-16 17:56:36 +00:00
..
bypass.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
bypass_test.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
cache.go fix: make caching options more explicit (#2966) 2024-06-14 18:45:48 +00:00
cache_test.go fix: make caching options more explicit (#2966) 2024-06-14 18:45:48 +00:00
error_resolver.go chore(deps): update tools to latest versions (#3121) 2024-08-16 17:56:36 +00:00
error_resolver_test.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
filesystem.go fix: handle errors reading go licenses (#2985) 2024-06-24 10:27:03 -04:00
filesystem_test.go fix: handle errors reading go licenses (#2985) 2024-06-24 10:27:03 -04:00
hash_type.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
hash_type_test.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
memory.go fix: make caching options more explicit (#2966) 2024-06-14 18:45:48 +00:00
memory_test.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
README.md chore: fix some comments (#3114) 2024-08-12 12:08:04 -04:00
resolver.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00
resolver_test.go fix: separate golang license caches from mod dir (#2852) 2024-06-12 19:12:35 -04:00

Caching

All caches are created from a global manager. By default this is a bypassedCache, which performs no caching. One benefit of this is that tests don't need to worry about caching causing issues unless they explicitly need to test the cache and can opt-in using the cache.TestCache(t) helper.

Syft sets a filesystemCache when the cache options are loaded.

When using the filesystemCache all items are stored on disk under a root directory, generally in the form of:

<rootDir>/<named-cache>/<data-version>/path/to/data

Using the cache

The easiest and preferred method to use the cache is a cache.Resolver, which automatically creates a <data-version> based on the structure of the provided type. If the structure changes in any way it will end up with a new version key and all will re populate this new key, ignoring cached values from older, different versions. The resolver will store items using the json package to serialize/deserialize values, so to save space it is encouraged to use omitempty. For example:

type myCacheItem struct {
	Name string `json:"name",omitempty`
}

It is possible to use core types such as pkg.Package as long as they support the standard json serialization, but this is discouraged in order to decouple changes to them from affecting the information stored in the cache.

To get a cache for this type:

resolver := cache.GetResolver[myCacheItem]("myCacheName", "v1")

Using the resolver is a single call, which manages checking for items in the cache, expiry times, and if not found invoking the callback to populate the cache and return a value:

data := resolver.Resolve("some/cache/key", func() (myCacheItem, error) {
	// do things to return a myCacheItem or error
})

If it is common that checking for an item will result in errors, and you do not want to re-run the resolve function when errors are encountered, instead of using GetResolver, you can use GetResolverCachingErrors, which is useful for things such as resolving artifacts over a network, where a number of them will not be resolved, and you do not want to continue to have the expense of running the network resolution. This should be used when it is acceptable a network outage and cached errors is an acceptable risk.

An example can be seen in the golang cataloger fetching remote licenses.