go.uber.org/cadence@v1.2.9/internal/common/cache/cache.go (about)

     1  // Copyright (c) 2017 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  	"time"
    25  )
    26  
    27  // A Cache is a generalized interface to a cache.  See cache.LRU for a specific
    28  // implementation (bounded cache with LRU eviction)
    29  type Cache interface {
    30  	// Exist checks if a given key exists in the cache
    31  	Exist(key string) bool
    32  
    33  	// Get retrieves an element based on a key, returning nil if the element
    34  	// does not exist
    35  	Get(key string) interface{}
    36  
    37  	// Put adds an element to the cache, returning the previous element
    38  	Put(key string, value interface{}) interface{}
    39  
    40  	// PutIfNotExist puts a value associated with a given key if it does not exist
    41  	PutIfNotExist(key string, value interface{}) (interface{}, error)
    42  
    43  	// Delete deletes an element in the cache
    44  	Delete(key string)
    45  
    46  	// Release decrements the ref count of a pinned element. If the ref count
    47  	// drops to 0, the element can be evicted from the cache.
    48  	Release(key string)
    49  
    50  	// Size returns the number of entries currently stored in the Cache
    51  	Size() int
    52  }
    53  
    54  // Options control the behavior of the cache
    55  type Options struct {
    56  	// TTL controls the time-to-live for a given cache entry.  Cache entries that
    57  	// are older than the TTL will not be returned
    58  	TTL time.Duration
    59  
    60  	// InitialCapacity controls the initial capacity of the cache
    61  	InitialCapacity int
    62  
    63  	// Pin prevents in-use objects from getting evicted
    64  	Pin bool
    65  
    66  	// RemovedFunc is an optional function called when an element
    67  	// is scheduled for deletion
    68  	RemovedFunc RemovedFunc
    69  }
    70  
    71  // RemovedFunc is a type for notifying applications when an item is
    72  // scheduled for removal from the Cache. If f is a function with the
    73  // appropriate signature and i is the interface{} scheduled for
    74  // deletion, Cache calls go f(i)
    75  type RemovedFunc func(interface{})