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

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"time"
     7  )
     8  
     9  // Cache byte arrays by key.
    10  //
    11  // NB we intentionally do not return errors in this interface - caching is best
    12  // effort by definition.  We found that when these methods did return errors,
    13  // the caller would just log them - so its easier for implementation to do that.
    14  // Whatsmore, we found partially successful Fetchs were often treated as failed
    15  // when they returned an error.
    16  type Cache interface {
    17  	Store(ctx context.Context, key []string, buf [][]byte)
    18  	Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string)
    19  	Stop() error
    20  }
    21  
    22  // Config for building Caches.
    23  type Config struct {
    24  	EnableFifoCache bool `yaml:"enable_fifocache,omitempty"`
    25  
    26  	DefaultValidity time.Duration `yaml:"defaul_validity,omitempty"`
    27  
    28  	Background     BackgroundConfig      `yaml:"background,omitempty"`
    29  	Memcache       MemcachedConfig       `yaml:"memcached,omitempty"`
    30  	MemcacheClient MemcachedClientConfig `yaml:"memcached_client,omitempty"`
    31  	Fifocache      FifoCacheConfig       `yaml:"fifocache,omitempty"`
    32  
    33  	// This is to name the cache metrics properly.
    34  	Prefix string `yaml:"prefix,omitempty"`
    35  
    36  	// For tests to inject specific implementations.
    37  	Cache Cache
    38  }
    39  
    40  // RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
    41  func (cfg *Config) RegisterFlagsWithPrefix(prefix string, description string, f *flag.FlagSet) {
    42  	cfg.Background.RegisterFlagsWithPrefix(prefix, description, f)
    43  	cfg.Memcache.RegisterFlagsWithPrefix(prefix, description, f)
    44  	cfg.MemcacheClient.RegisterFlagsWithPrefix(prefix, description, f)
    45  	cfg.Fifocache.RegisterFlagsWithPrefix(prefix, description, f)
    46  
    47  	f.BoolVar(&cfg.EnableFifoCache, prefix+"cache.enable-fifocache", false, description+"Enable in-memory cache.")
    48  	f.DurationVar(&cfg.DefaultValidity, prefix+"default-validity", 0, description+"The default validity of entries for caches unless overridden.")
    49  
    50  	cfg.Prefix = prefix
    51  }
    52  
    53  // New creates a new Cache using Config.
    54  func New(cfg Config) (Cache, error) {
    55  	if cfg.Cache != nil {
    56  		return cfg.Cache, nil
    57  	}
    58  
    59  	caches := []Cache{}
    60  
    61  	if cfg.EnableFifoCache {
    62  		if cfg.Fifocache.Validity == 0 && cfg.DefaultValidity != 0 {
    63  			cfg.Fifocache.Validity = cfg.DefaultValidity
    64  		}
    65  
    66  		cache := NewFifoCache(cfg.Prefix+"fifocache", cfg.Fifocache)
    67  		caches = append(caches, Instrument(cfg.Prefix+"fifocache", cache))
    68  	}
    69  
    70  	if cfg.MemcacheClient.Host != "" {
    71  		if cfg.Memcache.Expiration == 0 && cfg.DefaultValidity != 0 {
    72  			cfg.Memcache.Expiration = cfg.DefaultValidity
    73  		}
    74  
    75  		client := NewMemcachedClient(cfg.MemcacheClient)
    76  		cache := NewMemcached(cfg.Memcache, client, cfg.Prefix)
    77  
    78  		cacheName := cfg.Prefix + "memcache"
    79  		caches = append(caches, NewBackground(cacheName, cfg.Background, Instrument(cacheName, cache)))
    80  	}
    81  
    82  	cache := NewTiered(caches)
    83  	if len(caches) > 1 {
    84  		cache = Instrument(cfg.Prefix+"tiered", cache)
    85  	}
    86  	return cache, nil
    87  }