github.com/charlienet/go-mixed@v0.3.7/cache/big_cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"github.com/allegro/bigcache/v3"
     8  	"github.com/charlienet/go-mixed/logx"
     9  )
    10  
    11  var _ MemCache = &bigCacheClient{}
    12  
    13  type BigCacheConfig struct {
    14  	Shards             int
    15  	LifeWindow         time.Duration
    16  	CleanWindow        time.Duration
    17  	MaxEntriesInWindow int
    18  	MaxEntrySize       int
    19  	HardMaxCacheSize   int
    20  	log                logx.Logger
    21  }
    22  
    23  type bigCacheClient struct {
    24  	cache *bigcache.BigCache
    25  }
    26  
    27  func NewBigCache(c BigCacheConfig) (*bigCacheClient, error) {
    28  	config := bigcache.DefaultConfig(time.Minute * 10)
    29  
    30  	config.LifeWindow = c.LifeWindow
    31  	config.LifeWindow = c.LifeWindow
    32  	config.CleanWindow = c.CleanWindow
    33  	config.MaxEntriesInWindow = c.MaxEntriesInWindow
    34  	config.MaxEntrySize = c.MaxEntrySize
    35  	config.HardMaxCacheSize = c.HardMaxCacheSize
    36  	config.Logger = c.log
    37  
    38  	if c.Shards > 0 {
    39  		config.Shards = c.Shards
    40  	}
    41  
    42  	bigCache, err := bigcache.NewBigCache(config)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return &bigCacheClient{
    48  		cache: bigCache,
    49  	}, nil
    50  }
    51  
    52  func (c *bigCacheClient) Get(key string) ([]byte, error) {
    53  	return c.cache.Get(key)
    54  }
    55  
    56  func (c *bigCacheClient) Set(key string, entry []byte, expire time.Duration) error {
    57  	return c.cache.Set(key, entry)
    58  }
    59  
    60  func (c *bigCacheClient) Delete(keys ...string) error {
    61  	ks := keys[:]
    62  	for _, k := range ks {
    63  		if err := c.cache.Delete(k); err != nil {
    64  			return err
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func (c *bigCacheClient) Exist(key string) {
    72  }
    73  
    74  func (c *bigCacheClient) IsNotFound(err error) bool {
    75  	return errors.Is(err, bigcache.ErrEntryNotFound)
    76  }