github.com/thanos-io/thanos@v0.32.5/internal/cortex/chunk/cache/mock.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cache
     5  
     6  import (
     7  	"context"
     8  	"sync"
     9  )
    10  
    11  type mockCache struct {
    12  	sync.Mutex
    13  	cache map[string][]byte
    14  }
    15  
    16  func (m *mockCache) Store(_ context.Context, keys []string, bufs [][]byte) {
    17  	m.Lock()
    18  	defer m.Unlock()
    19  	for i := range keys {
    20  		m.cache[keys[i]] = bufs[i]
    21  	}
    22  }
    23  
    24  func (m *mockCache) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string) {
    25  	m.Lock()
    26  	defer m.Unlock()
    27  	for _, key := range keys {
    28  		buf, ok := m.cache[key]
    29  		if ok {
    30  			found = append(found, key)
    31  			bufs = append(bufs, buf)
    32  		} else {
    33  			missing = append(missing, key)
    34  		}
    35  	}
    36  	return
    37  }
    38  
    39  func (m *mockCache) Stop() {
    40  }
    41  
    42  // NewMockCache makes a new MockCache.
    43  func NewMockCache() Cache {
    44  	return &mockCache{
    45  		cache: map[string][]byte{},
    46  	}
    47  }
    48  
    49  // NewNoopCache returns a no-op cache.
    50  func NewNoopCache() Cache {
    51  	return NewTiered(nil)
    52  }