MCPcopy Create free account
hub / github.com/astercloud/aster / NewStore

Function NewStore

pkg/store/factory.go:40–82  ·  view source on GitHub ↗

NewStore 创建 Store(工厂方法)

(config Config)

Source from the content-addressed store, hash-verified

38
39// NewStore 创建 Store(工厂方法)
40func NewStore(config Config) (Store, error) {
41 switch config.Type {
42 case StoreTypeJSON, "":
43 // 默认使用 JSON Store
44 dataDir := config.DataDir
45 if dataDir == "" {
46 dataDir = ".aster"
47 }
48 return NewJSONStore(dataDir)
49
50 case StoreTypeRedis:
51 if config.RedisAddr == "" {
52 return nil, errors.New("redis_addr is required for redis store")
53 }
54
55 redisConfig := RedisConfig{
56 Addr: config.RedisAddr,
57 Password: config.RedisPassword,
58 DB: config.RedisDB,
59 Prefix: config.RedisPrefix,
60 TTL: config.RedisTTL,
61 }
62
63 return NewRedisStore(redisConfig)
64
65 case StoreTypeMySQL:
66 if config.MySQLDSN == "" {
67 return nil, errors.New("mysql_dsn is required for mysql store")
68 }
69
70 mysqlConfig := MySQLConfig{
71 DSN: config.MySQLDSN,
72 MaxOpenConns: config.MySQLMaxOpenConns,
73 MaxIdleConns: config.MySQLMaxIdleConns,
74 MaxLifetime: config.MySQLMaxLifetime,
75 }
76
77 return NewMySQLStore(mysqlConfig)
78
79 default:
80 return nil, fmt.Errorf("unknown store type: %s", config.Type)
81 }
82}
83
84// MustNewStore 创建 Store,失败时 panic
85func MustNewStore(config Config) Store {

Callers 1

MustNewStoreFunction · 0.70

Calls 3

NewJSONStoreFunction · 0.85
NewRedisStoreFunction · 0.85
NewMySQLStoreFunction · 0.70

Tested by

no test coverage detected