go-micro.dev/v5@v5.12.0/cache/options.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"go-micro.dev/v5/logger"
     8  )
     9  
    10  // Options represents the options for the cache.
    11  type Options struct {
    12  	// Context should contain all implementation specific options, using context.WithValue.
    13  	Context context.Context
    14  	// Logger is the be used logger
    15  	Logger logger.Logger
    16  	Items  map[string]Item
    17  	// Address represents the address or other connection information of the cache service.
    18  	Address    string
    19  	Expiration time.Duration
    20  }
    21  
    22  // Option manipulates the Options passed.
    23  type Option func(o *Options)
    24  
    25  // Expiration sets the duration for items stored in the cache to expire.
    26  func Expiration(d time.Duration) Option {
    27  	return func(o *Options) {
    28  		o.Expiration = d
    29  	}
    30  }
    31  
    32  // Items initializes the cache with preconfigured items.
    33  func Items(i map[string]Item) Option {
    34  	return func(o *Options) {
    35  		o.Items = i
    36  	}
    37  }
    38  
    39  // WithAddress sets the cache service address or connection information.
    40  func WithAddress(addr string) Option {
    41  	return func(o *Options) {
    42  		o.Address = addr
    43  	}
    44  }
    45  
    46  // WithContext sets the cache context, for any extra configuration.
    47  func WithContext(c context.Context) Option {
    48  	return func(o *Options) {
    49  		o.Context = c
    50  	}
    51  }
    52  
    53  // WithLogger sets underline logger.
    54  func WithLogger(l logger.Logger) Option {
    55  	return func(o *Options) {
    56  		o.Logger = l
    57  	}
    58  }
    59  
    60  // NewOptions returns a new options struct.
    61  func NewOptions(opts ...Option) Options {
    62  	options := Options{
    63  		Expiration: DefaultExpiration,
    64  		Items:      make(map[string]Item),
    65  		Logger:     logger.DefaultLogger,
    66  	}
    67  
    68  	for _, o := range opts {
    69  		o(&options)
    70  	}
    71  
    72  	return options
    73  }