github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/cache/options.go (about)

     1  package cache
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/binbinly/pkg/codec"
     7  )
     8  
     9  type Option func(*Options)
    10  
    11  type Options struct {
    12  	expire time.Duration
    13  	codec  codec.Encoding
    14  	prefix string
    15  }
    16  
    17  func NewOptions(opt ...Option) Options {
    18  	opts := Options{
    19  		expire: DefaultExpireTime,
    20  		prefix: DefaultPrefix,
    21  		codec:  codec.JSONEncoding{},
    22  	}
    23  
    24  	for _, o := range opt {
    25  		o(&opts)
    26  	}
    27  
    28  	return opts
    29  }
    30  
    31  func WithExpire(d time.Duration) Option {
    32  	return func(o *Options) {
    33  		o.expire = d
    34  	}
    35  }
    36  
    37  func WithPrefix(prefix string) Option {
    38  	return func(o *Options) {
    39  		o.prefix = prefix
    40  	}
    41  }
    42  
    43  func WithCodec(codec codec.Encoding) Option {
    44  	return func(o *Options) {
    45  		o.codec = codec
    46  	}
    47  }