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

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"time"
     7  
     8  	"github.com/wfusion/gofusion/common/constraint"
     9  	"github.com/wfusion/gofusion/common/utils"
    10  )
    11  
    12  var (
    13  	UnknownCacheType     = errors.New("unknown cache type")
    14  	UnknownCompress      = errors.New("unknown compress type")
    15  	UnknownSerializeType = errors.New("unknown serialize type")
    16  	UnknownRemoteType    = errors.New("unknown remote type")
    17  	ErrNotImplement      = errors.New("not implement")
    18  	ErrCacheNotFound     = errors.New("not found cache to use")
    19  	ErrCallbackNotFound  = errors.New("not found callback function")
    20  )
    21  
    22  type Cachable[K constraint.Sortable, T any, TS ~[]T] interface {
    23  	Get(ctx context.Context, keys []K, cb callback[K, T]) TS
    24  	GetAll(ctx context.Context, cb callback[K, T]) TS
    25  	Set(ctx context.Context, kv map[K]T, opts ...utils.OptionExtender) (failure []K)
    26  	Del(ctx context.Context, keys ...K) (failure []K)
    27  	Clear(ctx context.Context) (failure []K)
    28  }
    29  
    30  type callback[K constraint.Sortable, T any] func(ctx context.Context, missed []K) (
    31  	rs map[K]T, opts []utils.OptionExtender)
    32  
    33  type option[K constraint.Sortable] struct {
    34  	expired    time.Duration
    35  	keyExpired map[K]time.Duration
    36  }
    37  
    38  func Expired[K constraint.Sortable](expired time.Duration) utils.OptionFunc[option[K]] {
    39  	return func(o *option[K]) {
    40  		o.expired = expired
    41  	}
    42  }
    43  
    44  func KeyExpired[K constraint.Sortable](keyExpired map[K]time.Duration) utils.OptionFunc[option[K]] {
    45  	return func(o *option[K]) {
    46  		o.keyExpired = keyExpired
    47  	}
    48  }
    49  
    50  type cacheType string
    51  
    52  const (
    53  	// cacheTypeLocal local cache, base on gcache
    54  	cacheTypeLocal cacheType = "local"
    55  	// cacheTypeRemote remote cache should be serialized
    56  	cacheTypeRemote cacheType = "remote"
    57  	// cacheTypeRemoteLocal remove cache version and local cache data, not implement now
    58  	cacheTypeRemoteLocal cacheType = "remote_local"
    59  )
    60  
    61  type remoteType string
    62  
    63  const (
    64  	remoteTypeRedis remoteType = "redis"
    65  )
    66  
    67  type Conf struct {
    68  	Size           int        `yaml:"size" json:"size" toml:"size" default:"10000"`
    69  	Expired        string     `yaml:"expired" json:"expired" toml:"expired" default:"1h"`
    70  	Version        int        `yaml:"version" json:"version" toml:"version"`
    71  	CacheType      cacheType  `yaml:"type" json:"type" toml:"type" default:"local"`
    72  	RemoteType     remoteType `yaml:"remote_type" json:"remote_type" toml:"remote_type" default:"redis"`
    73  	RemoteInstance string     `yaml:"remote_instance" json:"remote_instance" toml:"remote_instance"`
    74  	LocalEvictType string     `yaml:"local_evict_type" json:"local_evict_type" toml:"local_evict_type" default:"arc"`
    75  	Compress       string     `yaml:"compress" json:"compress" toml:"compress"`
    76  	SerializeType  string     `yaml:"serialize_type" json:"serialize_type" toml:"serialize_type"`
    77  	Callback       string     `yaml:"callback" json:"callback" toml:"callback"`
    78  	LogInstance    string     `yaml:"log_instance" json:"log_instance" toml:"log_instance" default:"default"`
    79  }