github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/app/bcache/option.go (about)

     1  package bcache
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     7  	"github.com/songzhibin97/go-baseutils/base/bmap"
     8  	"github.com/songzhibin97/go-baseutils/base/options"
     9  )
    10  
    11  type Config[K comparable, V any] struct {
    12  	// defaultExpire 默认超时时间
    13  	defaultExpire time.Duration
    14  
    15  	// interval 间隔时间
    16  	interval time.Duration
    17  	// fn 哨兵周期执行的函数
    18  	setSentinelFn func()
    19  
    20  	// capture 捕获删除对象时间 会返回kv值用于用户自定义处理
    21  	capture func(k K, v V)
    22  
    23  	member bmap.AnyBMap[K, V]
    24  
    25  	comparator bcomparator.Comparator[K]
    26  }
    27  
    28  // SetInternal 设置间隔时间
    29  func SetInternal[K comparable, V any](interval time.Duration) options.Option[*Config[K, V]] {
    30  	return func(c *Config[K, V]) {
    31  		c.interval = interval
    32  	}
    33  }
    34  
    35  // SetDefaultExpire 设置默认的超时时间
    36  func SetDefaultExpire[K comparable, V any](expire time.Duration) options.Option[*Config[K, V]] {
    37  	return func(c *Config[K, V]) {
    38  		c.defaultExpire = expire
    39  	}
    40  }
    41  
    42  // SetSentinelFn 设置周期的执行函数,默认(不设置)是扫描全局清除过期的k
    43  func SetSentinelFn[K comparable, V any](fn func()) options.Option[*Config[K, V]] {
    44  	return func(c *Config[K, V]) {
    45  		c.setSentinelFn = fn
    46  	}
    47  }
    48  
    49  // SetCapture 设置触发删除后的捕获函数, 数据删除后回调用设置的捕获函数
    50  func SetCapture[K comparable, V any](capture func(k K, v V)) options.Option[*Config[K, V]] {
    51  	return func(c *Config[K, V]) {
    52  		c.capture = capture
    53  	}
    54  }