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

     1  package cache
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/wfusion/gofusion/common/utils"
     9  	"github.com/wfusion/gofusion/common/utils/compress"
    10  	"github.com/wfusion/gofusion/common/utils/inspect"
    11  	"github.com/wfusion/gofusion/common/utils/serialize"
    12  	"github.com/wfusion/gofusion/config"
    13  )
    14  
    15  // Construct cache only check some configures
    16  func Construct(ctx context.Context, confs map[string]*Conf, _ ...utils.OptionExtender) func() {
    17  	for _, conf := range confs {
    18  		addInstance(ctx, conf)
    19  	}
    20  
    21  	return func() {
    22  
    23  	}
    24  }
    25  
    26  func addInstance(ctx context.Context, conf *Conf) {
    27  	switch conf.CacheType {
    28  	case cacheTypeLocal:
    29  	case cacheTypeRemote:
    30  		if conf.RemoteType != remoteTypeRedis {
    31  			panic(UnknownRemoteType)
    32  		}
    33  
    34  		parsedSerializeType := serialize.ParseAlgorithm(conf.SerializeType)
    35  		parsedCompressType := compress.ParseAlgorithm(conf.Compress)
    36  		if !parsedSerializeType.IsValid() && !parsedCompressType.IsValid() {
    37  			panic(UnknownSerializeType)
    38  		}
    39  	case cacheTypeRemoteLocal:
    40  		panic(ErrNotImplement)
    41  
    42  	default:
    43  		panic(UnknownCacheType)
    44  	}
    45  
    46  	if utils.IsStrNotBlank(conf.Callback) && inspect.FuncOf(conf.Callback) == nil {
    47  		panic(errors.Errorf("not found callback function: %s", conf.Callback))
    48  	}
    49  }
    50  
    51  func init() {
    52  	config.AddComponent(config.ComponentCache, Construct, config.WithFlag(&flagString))
    53  }