github.com/xgzlucario/GigaCache@v0.0.0-20240508025442-54204e9c8a6b/options.go (about)

     1  package cache
     2  
     3  import "errors"
     4  
     5  // Options is the configuration of GigaCache.
     6  type Options struct {
     7  	// ShardCount is shard numbers of cache.
     8  	ShardCount uint32
     9  
    10  	// Default size of the bucket initial.
    11  	IndexSize  int
    12  	BufferSize int
    13  
    14  	// EvictInterval indicates the frequency of execution of the eliminate algorithm.
    15  	// the higher the frequency, the more expired key-value pairs will be evicted,
    16  	// but accordingly it will slow down the overall performance,
    17  	// because the system needs to spend more time on probing and evicting.
    18  	EvictInterval int
    19  
    20  	// DisableEvict
    21  	// Set `true` when you don't need any expiration times.
    22  	DisableEvict bool
    23  
    24  	// Migrate threshold for a bucket to trigger a migration.
    25  	MigrateRatio float64
    26  
    27  	// HashFn is custom hash function, default is runtime.memhash.
    28  	HashFn HashFn
    29  }
    30  
    31  var DefaultOptions = Options{
    32  	ShardCount:    1024,
    33  	IndexSize:     1024,
    34  	BufferSize:    64 * KB,
    35  	EvictInterval: 3,
    36  	DisableEvict:  false,
    37  	MigrateRatio:  0.4,
    38  	HashFn:        MemHash,
    39  }
    40  
    41  func checkOptions(options Options) error {
    42  	if options.ShardCount == 0 {
    43  		return errors.New("cache/options: invalid shard count")
    44  	}
    45  	if options.EvictInterval < 0 {
    46  		return errors.New("cache/options: invalid evict interval")
    47  	}
    48  	return nil
    49  }