github.com/uber-go/tally/v4@v4.1.17/internal/cache/tag_cache.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package cache
    22  
    23  import (
    24  	"sync"
    25  
    26  	"github.com/uber-go/tally/v4/internal/identity"
    27  	m3thrift "github.com/uber-go/tally/v4/m3/thrift/v2"
    28  )
    29  
    30  // TagCache is an identity.Accumulator-based tag cache.
    31  type TagCache struct {
    32  	entries map[uint64][]m3thrift.MetricTag
    33  	mtx     sync.RWMutex
    34  }
    35  
    36  // NewTagCache creates a new TagCache.
    37  func NewTagCache() *TagCache {
    38  	return &TagCache{
    39  		entries: make(map[uint64][]m3thrift.MetricTag),
    40  	}
    41  }
    42  
    43  // Get returns the cached value for key.
    44  func (c *TagCache) Get(key uint64) ([]m3thrift.MetricTag, bool) {
    45  	c.mtx.RLock()
    46  	defer c.mtx.RUnlock()
    47  
    48  	entry, ok := c.entries[key]
    49  	return entry, ok
    50  }
    51  
    52  // Set attempts to set the value of key as tslice, returning either tslice or
    53  // the pre-existing value if found.
    54  func (c *TagCache) Set(key uint64, tslice []m3thrift.MetricTag) []m3thrift.MetricTag {
    55  	c.mtx.RLock()
    56  	existing, ok := c.entries[key]
    57  	c.mtx.RUnlock()
    58  
    59  	if ok {
    60  		return existing
    61  	}
    62  
    63  	c.mtx.Lock()
    64  	defer c.mtx.Unlock()
    65  
    66  	c.entries[key] = tslice
    67  	return tslice
    68  }
    69  
    70  // Len returns the size of the cache,
    71  func (c *TagCache) Len() int {
    72  	c.mtx.RLock()
    73  	defer c.mtx.RUnlock()
    74  	return len(c.entries)
    75  }
    76  
    77  // TagMapKey generates a new key based on tags.
    78  func TagMapKey(tags map[string]string) uint64 {
    79  	return identity.StringStringMap(tags)
    80  }