...

Source file src/github.com/jellydator/ttlcache/v3/options.go

Documentation: github.com/jellydator/ttlcache/v3

     1  package ttlcache
     2  
     3  import "time"
     4  
     5  // Option sets a specific cache option.
     6  type Option[K comparable, V any] interface {
     7  	apply(opts *options[K, V])
     8  }
     9  
    10  // optionFunc wraps a function and implements the Option interface.
    11  type optionFunc[K comparable, V any] func(*options[K, V])
    12  
    13  // apply calls the wrapped function.
    14  func (fn optionFunc[K, V]) apply(opts *options[K, V]) {
    15  	fn(opts)
    16  }
    17  
    18  // options holds all available cache configuration options.
    19  type options[K comparable, V any] struct {
    20  	capacity              uint64
    21  	ttl                   time.Duration
    22  	loader                Loader[K, V]
    23  	disableTouchOnHit     bool
    24  	enableVersionTracking bool
    25  }
    26  
    27  // applyOptions applies the provided option values to the option struct.
    28  func applyOptions[K comparable, V any](v *options[K, V], opts ...Option[K, V]) {
    29  	for i := range opts {
    30  		opts[i].apply(v)
    31  	}
    32  }
    33  
    34  // WithCapacity sets the maximum capacity of the cache.
    35  // It has no effect when used with Get().
    36  func WithCapacity[K comparable, V any](c uint64) Option[K, V] {
    37  	return optionFunc[K, V](func(opts *options[K, V]) {
    38  		opts.capacity = c
    39  	})
    40  }
    41  
    42  // WithTTL sets the TTL of the cache.
    43  // It has no effect when used with Get().
    44  func WithTTL[K comparable, V any](ttl time.Duration) Option[K, V] {
    45  	return optionFunc[K, V](func(opts *options[K, V]) {
    46  		opts.ttl = ttl
    47  	})
    48  }
    49  
    50  // WithVersion activates item version tracking.
    51  // If version tracking is disabled, the version is always -1.
    52  // It has no effect when used with Get().
    53  func WithVersion[K comparable, V any](enable bool) Option[K, V] {
    54  	return optionFunc[K, V](func(opts *options[K, V]) {
    55  		opts.enableVersionTracking = enable
    56  	})
    57  }
    58  
    59  // WithLoader sets the loader of the cache.
    60  // When passing into Get(), it sets an ephemeral loader that
    61  // is used instead of the cache's default one.
    62  func WithLoader[K comparable, V any](l Loader[K, V]) Option[K, V] {
    63  	return optionFunc[K, V](func(opts *options[K, V]) {
    64  		opts.loader = l
    65  	})
    66  }
    67  
    68  // WithDisableTouchOnHit prevents the cache instance from
    69  // extending/touching an item's expiration timestamp when it is being
    70  // retrieved.
    71  // When used with Get(), it overrides the default value of the
    72  // cache.
    73  func WithDisableTouchOnHit[K comparable, V any]() Option[K, V] {
    74  	return optionFunc[K, V](func(opts *options[K, V]) {
    75  		opts.disableTouchOnHit = true
    76  	})
    77  }
    78  

View as plain text