github.com/sequix/cortex@v1.1.6/pkg/chunk/cache/snappy.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/sequix/cortex/pkg/util"
     7  	"github.com/go-kit/kit/log/level"
     8  	"github.com/golang/snappy"
     9  )
    10  
    11  type snappyCache struct {
    12  	next Cache
    13  }
    14  
    15  // NewSnappy makes a new snappy encoding cache wrapper.
    16  func NewSnappy(next Cache) Cache {
    17  	return &snappyCache{
    18  		next: next,
    19  	}
    20  }
    21  
    22  func (s *snappyCache) Store(ctx context.Context, keys []string, bufs [][]byte) {
    23  	cs := make([][]byte, 0, len(bufs))
    24  	for _, buf := range bufs {
    25  		c := snappy.Encode(nil, buf)
    26  		cs = append(cs, c)
    27  	}
    28  	s.next.Store(ctx, keys, cs)
    29  }
    30  
    31  func (s *snappyCache) Fetch(ctx context.Context, keys []string) ([]string, [][]byte, []string) {
    32  	found, bufs, missing := s.next.Fetch(ctx, keys)
    33  	ds := make([][]byte, 0, len(bufs))
    34  	for _, buf := range bufs {
    35  		d, err := snappy.Decode(nil, buf)
    36  		if err != nil {
    37  			level.Error(util.Logger).Log("msg", "failed to decode cache entry", "err", err)
    38  			return nil, nil, keys
    39  		}
    40  		ds = append(ds, d)
    41  	}
    42  	return found, ds, missing
    43  }
    44  
    45  func (s *snappyCache) Stop() error {
    46  	return s.next.Stop()
    47  }