vitess.io/vitess@v0.16.2/go/cache/cache.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  // Cache is a generic interface type for a data structure that keeps recently used
    20  // objects in memory and evicts them when it becomes full.
    21  type Cache interface {
    22  	Get(key string) (any, bool)
    23  	Set(key string, val any) bool
    24  	ForEach(callback func(any) bool)
    25  
    26  	Delete(key string)
    27  	Clear()
    28  
    29  	// Wait waits for all pending operations on the cache to settle. Since cache writes
    30  	// are asynchronous, a write may not be immediately accessible unless the user
    31  	// manually calls Wait.
    32  	Wait()
    33  
    34  	Len() int
    35  	Evictions() int64
    36  	Hits() int64
    37  	Misses() int64
    38  	UsedCapacity() int64
    39  	MaxCapacity() int64
    40  	SetCapacity(int64)
    41  }
    42  
    43  type cachedObject interface {
    44  	CachedSize(alloc bool) int64
    45  }
    46  
    47  // NewDefaultCacheImpl returns the default cache implementation for Vitess. The options in the
    48  // Config struct control the memory and entry limits for the cache, and the underlying cache
    49  // implementation.
    50  func NewDefaultCacheImpl(cfg *Config) Cache {
    51  	switch {
    52  	case cfg == nil:
    53  		return &nullCache{}
    54  
    55  	case cfg.LFU:
    56  		if cfg.MaxEntries == 0 || cfg.MaxMemoryUsage == 0 {
    57  			return &nullCache{}
    58  		}
    59  		return NewRistrettoCache(cfg.MaxEntries, cfg.MaxMemoryUsage, func(val any) int64 {
    60  			return val.(cachedObject).CachedSize(true)
    61  		})
    62  
    63  	default:
    64  		if cfg.MaxEntries == 0 {
    65  			return &nullCache{}
    66  		}
    67  		return NewLRUCache(cfg.MaxEntries, func(_ any) int64 {
    68  			return 1
    69  		})
    70  	}
    71  }
    72  
    73  // Config is the configuration options for a cache instance
    74  type Config struct {
    75  	// MaxEntries is the estimated amount of entries that the cache will hold at capacity
    76  	MaxEntries int64
    77  	// MaxMemoryUsage is the maximum amount of memory the cache can handle
    78  	MaxMemoryUsage int64
    79  	// LFU toggles whether to use a new cache implementation with a TinyLFU admission policy
    80  	LFU bool
    81  }
    82  
    83  // DefaultConfig is the default configuration for a cache instance in Vitess
    84  var DefaultConfig = &Config{
    85  	MaxEntries:     5000,
    86  	MaxMemoryUsage: 32 * 1024 * 1024,
    87  	LFU:            true,
    88  }