github.com/wfusion/gofusion@v1.1.14/cache/local.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"syscall"
     7  	"time"
     8  
     9  	"github.com/bluele/gcache"
    10  
    11  	"github.com/wfusion/gofusion/config"
    12  	"github.com/wfusion/gofusion/log"
    13  )
    14  
    15  func newGCache(size int, strategy string, log log.Loggable) *gCache {
    16  	cacheBuilder := gcache.New(size)
    17  	switch strategy {
    18  	case gcache.TYPE_ARC:
    19  		cacheBuilder = cacheBuilder.ARC()
    20  	case gcache.TYPE_LFU:
    21  		cacheBuilder = cacheBuilder.LFU()
    22  	case gcache.TYPE_LRU:
    23  		cacheBuilder = cacheBuilder.LRU()
    24  	case gcache.TYPE_SIMPLE:
    25  		cacheBuilder = cacheBuilder.Simple()
    26  	default:
    27  		cacheBuilder = cacheBuilder.ARC()
    28  	}
    29  
    30  	return &gCache{
    31  		log:      log,
    32  		instance: cacheBuilder.Build(),
    33  	}
    34  }
    35  
    36  type gCache struct {
    37  	log      log.Loggable
    38  	instance gcache.Cache
    39  }
    40  
    41  func (g *gCache) get(ctx context.Context, keys ...string) (cached map[string]any, missed []string) {
    42  	cached = make(map[string]any, len(keys))
    43  	missed = make([]string, 0, len(keys))
    44  	for _, k := range keys {
    45  		v, err := g.instance.Get(k)
    46  		if err != nil {
    47  			missed = append(missed, k)
    48  			if !errors.Is(err, gcache.KeyNotFoundError) && g.log != nil {
    49  				g.log.Info(ctx, "%v [Gofusion] %s call gcache get failed "+
    50  					"when get cache from gcache [err[%s] key[%s]]", syscall.Getpid(), config.ComponentCache, err, k)
    51  			}
    52  			continue
    53  		}
    54  		cached[k] = v
    55  	}
    56  	return
    57  }
    58  
    59  func (g *gCache) set(ctx context.Context, kvs map[string]any, expired map[string]time.Duration) (failure []string) {
    60  	failure = make([]string, 0, len(kvs))
    61  	for k, v := range kvs {
    62  		var err error
    63  		if exp, ok := expired[k]; ok {
    64  			err = g.instance.SetWithExpire(k, v, exp)
    65  		} else {
    66  			err = g.instance.Set(k, v)
    67  		}
    68  
    69  		if err != nil {
    70  			failure = append(failure, k)
    71  			if g.log != nil {
    72  				g.log.Info(ctx, "%v [Gofusion] %s call gcache set/set_with_expire failed "+
    73  					"when set cache into gcache [err[%s] key[%s]]", syscall.Getpid(), config.ComponentCache, err, k)
    74  			}
    75  		}
    76  	}
    77  	return
    78  }
    79  
    80  func (g *gCache) del(ctx context.Context, keys ...string) (failure []string) {
    81  	failure = make([]string, 0, len(keys))
    82  	for _, k := range keys {
    83  		if g.instance.Remove(k) {
    84  			continue
    85  		}
    86  
    87  		if _, err := g.instance.Get(k); !errors.Is(err, gcache.KeyNotFoundError) {
    88  			failure = append(failure, k)
    89  			if g.log != nil {
    90  				g.log.Info(ctx, "%v [Gofusion] %s call gcache remove failed "+
    91  					"when delete cache from gcache [err[%s] key[%s]]", syscall.Getpid(), config.ComponentCache, err, k)
    92  			}
    93  		}
    94  	}
    95  	return
    96  }