package cache

import "github.com/google/go-containerregistry/pkg/v1/cache"

Package cache provides methods to cache layers.

Index

Examples

Variables

var ErrNotFound = errors.New("layer was not found")

ErrNotFound is returned by Get when no layer with the given Hash is found.

Functions

func Image

func Image(i v1.Image, c Cache) v1.Image

Image returns a new Image which wraps the given Image, whose layers will be pulled from the Cache if they are found, and written to the Cache as they are read from the underlying Image.

Example

Code:play 

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/google/go-containerregistry/pkg/v1/cache"
	"github.com/google/go-containerregistry/pkg/v1/random"
)

func main() {
	img, err := random.Image(1024*1024, 3)
	if err != nil {
		log.Fatal(err)
	}
	dir, err := os.MkdirTemp("", "")
	if err != nil {
		log.Fatal(err)
	}
	fs := cache.NewFilesystemCache(dir)

	// cached will cache layers from img using the fs cache
	cached := cache.Image(img, fs)

	// Use cached as you would use img.
	digest, err := cached.Digest()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(digest)
}

func ImageIndex

func ImageIndex(ii v1.ImageIndex, c Cache) v1.ImageIndex

ImageIndex returns a new ImageIndex which wraps the given ImageIndex's children with either Image(child, c) or ImageIndex(child, c) depending on type.

Types

type Cache

type Cache interface {
	// Put writes the Layer to the Cache.
	//
	// The returned Layer should be used for future operations, since lazy
	// cachers might only populate the cache when the layer is actually
	// consumed.
	//
	// The returned layer can be consumed, and the cache entry populated,
	// by calling either Compressed or Uncompressed and consuming the
	// returned io.ReadCloser.
	Put(v1.Layer) (v1.Layer, error)

	// Get returns the Layer cached by the given Hash, or ErrNotFound if no
	// such layer was found.
	Get(v1.Hash) (v1.Layer, error)

	// Delete removes the Layer with the given Hash from the Cache.
	Delete(v1.Hash) error
}

Cache encapsulates methods to interact with cached layers.

func NewFilesystemCache

func NewFilesystemCache(path string) Cache

NewFilesystemCache returns a Cache implementation backed by files.

func ReadOnly

func ReadOnly(c Cache) Cache

ReadOnly returns a read-only implementation of the given Cache.

Put and Delete operations are a no-op.

Source Files

cache.go fs.go ro.go

Version
v0.20.3 (latest)
Published
Jan 15, 2025
Platform
linux/amd64
Imports
10 packages
Last checked
16 hours ago

Tools for package owners.