...

Source file src/edge-infra.dev/pkg/f8n/warehouse/oci/cache/providers/memory/memory.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/oci/cache/providers/memory

     1  // Memory Provider for the oci cache
     2  // this provider is based on an arc cache
     3  // ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC)
     4  // ARC is an enhancement over the standard LRU cache in that tracks both frequency and recency of use
     5  
     6  package memory
     7  
     8  import (
     9  	v1 "github.com/google/go-containerregistry/pkg/v1"
    10  	lru "github.com/hashicorp/golang-lru"
    11  )
    12  
    13  // ArcCache encapsulates an arc cache to store oci artifacts.
    14  type ArcCache struct {
    15  	store *lru.ARCCache
    16  }
    17  
    18  // New returns a new instance of the memory provider.
    19  func New(limit int) (*ArcCache, error) {
    20  	cache, err := lru.NewARC(limit)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	return &ArcCache{cache}, nil
    25  }
    26  
    27  // Get fetches an object from the arc cache if it exists
    28  func (m *ArcCache) Get(h v1.Hash) (any, bool) {
    29  	return m.store.Get(h)
    30  }
    31  
    32  // Keys returns the set of cache keys in the backing store
    33  func (m *ArcCache) Keys() []any {
    34  	return m.store.Keys()
    35  }
    36  
    37  // Exists check if the artifact with the specified hash exists in the arc cache
    38  func (m *ArcCache) Exists(h v1.Hash) bool {
    39  	return m.store.Contains(h)
    40  }
    41  
    42  // Add inserts an artifact to the arc cache
    43  func (m *ArcCache) Add(h v1.Hash, obj any) {
    44  	m.store.Add(h, obj)
    45  }
    46  
    47  // Len returns number of entries in cache.
    48  func (m *ArcCache) Len() int {
    49  	return m.store.Len()
    50  }
    51  
    52  func (m *ArcCache) Purge() {
    53  	m.store.Purge()
    54  }
    55  

View as plain text