github.com/celestiaorg/celestia-node@v0.15.0-beta.1/share/eds/cache/doublecache.go (about)

     1  package cache
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/filecoin-project/dagstore/shard"
     7  )
     8  
     9  // DoubleCache represents a Cache that looks into multiple caches one by one.
    10  type DoubleCache struct {
    11  	first, second Cache
    12  }
    13  
    14  // NewDoubleCache creates a new DoubleCache with the provided caches.
    15  func NewDoubleCache(first, second Cache) *DoubleCache {
    16  	return &DoubleCache{
    17  		first:  first,
    18  		second: second,
    19  	}
    20  }
    21  
    22  // Get looks for an item in all the caches one by one and returns the Cache found item.
    23  func (mc *DoubleCache) Get(key shard.Key) (Accessor, error) {
    24  	ac, err := mc.first.Get(key)
    25  	if err == nil {
    26  		return ac, nil
    27  	}
    28  	return mc.second.Get(key)
    29  }
    30  
    31  // Remove removes an item from all underlying caches
    32  func (mc *DoubleCache) Remove(key shard.Key) error {
    33  	err1 := mc.first.Remove(key)
    34  	err2 := mc.second.Remove(key)
    35  	return errors.Join(err1, err2)
    36  }
    37  
    38  func (mc *DoubleCache) First() Cache {
    39  	return mc.first
    40  }
    41  
    42  func (mc *DoubleCache) Second() Cache {
    43  	return mc.second
    44  }
    45  
    46  func (mc *DoubleCache) EnableMetrics() error {
    47  	if err := mc.first.EnableMetrics(); err != nil {
    48  		return err
    49  	}
    50  	return mc.second.EnableMetrics()
    51  }