MCPcopy
hub / github.com/thanos-io/thanos / NewInMemoryCacheWithConfig

Function NewInMemoryCacheWithConfig

pkg/cache/inmemory.go:97–193  ·  view source on GitHub ↗

NewInMemoryCacheWithConfig creates a new thread-safe LRU cache and ensures the total cache size approximately does not exceed maxBytes.

(name string, logger log.Logger, reg prometheus.Registerer, config InMemoryCacheConfig)

Source from the content-addressed store, hash-verified

95// NewInMemoryCacheWithConfig creates a new thread-safe LRU cache and ensures the total cache
96// size approximately does not exceed maxBytes.
97func NewInMemoryCacheWithConfig(name string, logger log.Logger, reg prometheus.Registerer, config InMemoryCacheConfig) (*InMemoryCache, error) {
98 if config.MaxItemSize > config.MaxSize {
99 return nil, errors.Errorf("max item size (%v) cannot be bigger than overall cache size (%v)", config.MaxItemSize, config.MaxSize)
100 }
101
102 c := &InMemoryCache{
103 logger: logger,
104 maxSizeBytes: uint64(config.MaxSize),
105 maxItemSizeBytes: uint64(config.MaxItemSize),
106 name: name,
107 }
108
109 c.evicted = promauto.With(reg).NewCounter(prometheus.CounterOpts{
110 Name: "thanos_cache_inmemory_items_evicted_total",
111 Help: "Total number of items that were evicted from the inmemory cache.",
112 ConstLabels: prometheus.Labels{"name": name},
113 })
114
115 c.added = promauto.With(reg).NewCounter(prometheus.CounterOpts{
116 Name: "thanos_cache_inmemory_items_added_total",
117 Help: "Total number of items that were added to the inmemory cache.",
118 ConstLabels: prometheus.Labels{"name": name},
119 })
120
121 c.requests = promauto.With(reg).NewCounter(prometheus.CounterOpts{
122 Name: "thanos_cache_inmemory_requests_total",
123 Help: "Total number of requests to the inmemory cache.",
124 ConstLabels: prometheus.Labels{"name": name},
125 })
126
127 c.hitsExpired = promauto.With(reg).NewCounter(prometheus.CounterOpts{
128 Name: "thanos_cache_inmemory_hits_on_expired_data_total",
129 Help: "Total number of requests to the inmemory cache that were a hit but needed to be evicted due to TTL.",
130 ConstLabels: prometheus.Labels{"name": name},
131 })
132
133 c.overflow = promauto.With(reg).NewCounter(prometheus.CounterOpts{
134 Name: "thanos_cache_inmemory_items_overflowed_total",
135 Help: "Total number of items that could not be added to the inmemory cache due to being too big.",
136 ConstLabels: prometheus.Labels{"name": name},
137 })
138
139 c.hits = promauto.With(reg).NewCounter(prometheus.CounterOpts{
140 Name: "thanos_cache_inmemory_hits_total",
141 Help: "Total number of requests to the inmemory cache that were a hit.",
142 ConstLabels: prometheus.Labels{"name": name},
143 })
144
145 c.current = promauto.With(reg).NewGauge(prometheus.GaugeOpts{
146 Name: "thanos_cache_inmemory_items",
147 Help: "Current number of items in the inmemory cache.",
148 ConstLabels: prometheus.Labels{"name": name},
149 })
150
151 c.currentSize = promauto.With(reg).NewGauge(prometheus.GaugeOpts{
152 Name: "thanos_cache_inmemory_items_size_bytes",
153 Help: "Current byte size of items in the inmemory cache.",
154 ConstLabels: prometheus.Labels{"name": name},

Callers 1

NewInMemoryCacheFunction · 0.85

Calls 4

ErrorfMethod · 0.80
WithMethod · 0.80
InfoMethod · 0.65
LogMethod · 0.45

Tested by

no test coverage detected