go.temporal.io/server@v1.23.0/common/cache/cache.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package cache
    26  
    27  import (
    28  	"time"
    29  
    30  	"go.temporal.io/server/common/clock"
    31  )
    32  
    33  // A Cache is a generalized interface to a cache.  See cache.LRU for a specific
    34  // implementation (bounded cache with LRU eviction)
    35  type Cache interface {
    36  	// Get retrieves an element based on a key, returning nil if the element
    37  	// does not exist
    38  	Get(key interface{}) interface{}
    39  
    40  	// Put adds an element to the cache, returning the previous element
    41  	Put(key interface{}, value interface{}) interface{}
    42  
    43  	// PutIfNotExist puts a value associated with a given key if it does not exist
    44  	PutIfNotExist(key interface{}, value interface{}) (interface{}, error)
    45  
    46  	// Delete deletes an element in the cache
    47  	Delete(key interface{})
    48  
    49  	// Release decrements the ref count of a pinned element. If the ref count
    50  	// drops to 0, the element can be evicted from the cache.
    51  	Release(key interface{})
    52  
    53  	// Iterator returns the iterator of the cache
    54  	Iterator() Iterator
    55  
    56  	// Size returns current size of the Cache, the size definition is implementation of SizeGetter interface
    57  	// for the entry size, if the entry does not implement SizeGetter interface, the size is 1
    58  	Size() int
    59  }
    60  
    61  // Options control the behavior of the cache
    62  type Options struct {
    63  	// TTL controls the time-to-live for a given cache entry.  Cache entries that
    64  	// are older than the TTL will not be returned.
    65  	TTL time.Duration
    66  
    67  	// Pin prevents in-use objects from getting evicted.
    68  	Pin bool
    69  
    70  	// TimeSource is an optional clock to use for time-skipping and testing. If this is nil, a real clock will be used.
    71  	TimeSource clock.TimeSource
    72  }
    73  
    74  // SimpleOptions provides options that can be used to configure SimpleCache
    75  type SimpleOptions struct {
    76  	// RemovedFunc is an optional function called when an element
    77  	// is scheduled for deletion
    78  	RemovedFunc RemovedFunc
    79  }
    80  
    81  // RemovedFunc is a type for notifying applications when an item is
    82  // scheduled for removal from the Cache. If f is a function with the
    83  // appropriate signature and i is the interface{} scheduled for
    84  // deletion, Cache calls go f(i)
    85  type RemovedFunc func(interface{})
    86  
    87  // Iterator represents the interface for cache iterators
    88  type Iterator interface {
    89  	// Close closes the iterator
    90  	// and releases any allocated resources
    91  	Close()
    92  	// HasNext return true if there is more items to be returned
    93  	HasNext() bool
    94  	// Next return the next item
    95  	Next() Entry
    96  }
    97  
    98  // Entry represents a key-value entry within the map
    99  type Entry interface {
   100  	// Key represents the key
   101  	Key() interface{}
   102  	// Value represents the value
   103  	Value() interface{}
   104  	// CreateTime represents the time when the entry is created
   105  	CreateTime() time.Time
   106  }