...

Source file src/github.com/launchdarkly/ccache/configuration.go

Documentation: github.com/launchdarkly/ccache

     1  package ccache
     2  
     3  type Configuration struct {
     4  	maxSize        int64
     5  	buckets        int
     6  	itemsToPrune   int
     7  	deleteBuffer   int
     8  	promoteBuffer  int
     9  	getsPerPromote int32
    10  	tracking       bool
    11  	onDelete       func(item *Item)
    12  }
    13  
    14  // Creates a configuration object with sensible defaults
    15  // Use this as the start of the fluent configuration:
    16  // e.g.: ccache.New(ccache.Configure().MaxSize(10000))
    17  func Configure() *Configuration {
    18  	return &Configuration{
    19  		buckets:        16,
    20  		itemsToPrune:   500,
    21  		deleteBuffer:   1024,
    22  		getsPerPromote: 3,
    23  		promoteBuffer:  1024,
    24  		maxSize:        5000,
    25  		tracking:       false,
    26  	}
    27  }
    28  
    29  // The max size for the cache
    30  // [5000]
    31  func (c *Configuration) MaxSize(max int64) *Configuration {
    32  	c.maxSize = max
    33  	return c
    34  }
    35  
    36  // Keys are hashed into % bucket count to provide greater concurrency (every set
    37  // requires a write lock on the bucket). Must be a power of 2 (1, 2, 4, 8, 16, ...)
    38  // [16]
    39  func (c *Configuration) Buckets(count uint32) *Configuration {
    40  	if count == 0 || ((count&(^count+1)) == count) == false {
    41  		count = 16
    42  	}
    43  	c.buckets = int(count)
    44  	return c
    45  }
    46  
    47  // The number of items to prune when memory is low
    48  // [500]
    49  func (c *Configuration) ItemsToPrune(count uint32) *Configuration {
    50  	c.itemsToPrune = int(count)
    51  	return c
    52  }
    53  
    54  // The size of the queue for items which should be promoted. If the queue fills
    55  // up, promotions are skipped
    56  // [1024]
    57  func (c *Configuration) PromoteBuffer(size uint32) *Configuration {
    58  	c.promoteBuffer = int(size)
    59  	return c
    60  }
    61  
    62  // The size of the queue for items which should be deleted. If the queue fills
    63  // up, calls to Delete() will block
    64  func (c *Configuration) DeleteBuffer(size uint32) *Configuration {
    65  	c.deleteBuffer = int(size)
    66  	return c
    67  }
    68  
    69  // Give a large cache with a high read / write ratio, it's usually unnecessary
    70  // to promote an item on every Get. GetsPerPromote specifies the number of Gets
    71  // a key must have before being promoted
    72  // [3]
    73  func (c *Configuration) GetsPerPromote(count int32) *Configuration {
    74  	c.getsPerPromote = count
    75  	return c
    76  }
    77  
    78  // Typically, a cache is agnostic about how cached values are use. This is fine
    79  // for a typical cache usage, where you fetch an item from the cache, do something
    80  // (write it out) and nothing else.
    81  
    82  // However, if callers are going to keep a reference to a cached item for a long
    83  // time, things get messy. Specifically, the cache can evict the item, while
    84  // references still exist. Technically, this isn't an issue. However, if you reload
    85  // the item back into the cache, you end up with 2 objects representing the same
    86  // data. This is a waste of space and could lead to weird behavior (the type an
    87  // identity map is meant to solve).
    88  
    89  // By turning tracking on and using the cache's TrackingGet, the cache
    90  // won't evict items which you haven't called Release() on. It's a simple reference
    91  // counter.
    92  func (c *Configuration) Track() *Configuration {
    93  	c.tracking = true
    94  	return c
    95  }
    96  
    97  // OnDelete allows setting a callback function to react to ideam deletion.
    98  // This typically allows to do a cleanup of resources, such as calling a Close() on
    99  // cached object that require some kind of tear-down.
   100  func (c *Configuration) OnDelete(callback func(item *Item)) *Configuration {
   101  	c.onDelete = callback
   102  	return c
   103  }
   104  

View as plain text