/
store.go
58 lines (51 loc) · 1.4 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package store
import (
"strings"
"time"
"github.com/jaimeteb/chatto/fsm"
"github.com/jaimeteb/chatto/internal/fsm/store/cache"
"github.com/jaimeteb/chatto/internal/fsm/store/config"
"github.com/jaimeteb/chatto/internal/fsm/store/redis"
"github.com/jaimeteb/chatto/internal/fsm/store/sql"
log "github.com/sirupsen/logrus"
)
var defaultDuration = -1 * time.Second
// Store interface for FSM Store modes
type Store interface {
Exists(string) bool
Get(string) *fsm.FSM
Set(string, *fsm.FSM)
}
// New loads a Store according to the configuration
func New(cfg *config.StoreConfig) Store {
var machines Store
if cfg.Purge == defaultDuration && cfg.TTL != defaultDuration {
cfg.Purge = cfg.TTL
}
switch strings.ToLower(cfg.Type) {
case "redis":
redisStore, err := redis.NewStore(cfg)
if err != nil {
log.Errorf("Error: %v", err)
log.Warn("Couldn't connect to Redis, using CacheStoreFSM instead")
machines = cache.NewStore(cfg)
} else {
log.Info("Connected to RedisStoreFSM")
machines = redisStore
}
case "sql":
sqlStore, err := sql.NewStore(cfg)
if err != nil {
log.Errorf("Error: %v", err)
log.Warn("Couldn't connect to SQL database, using CacheStoreFSM instead")
machines = cache.NewStore(cfg)
} else {
log.Info("Connected to SQLStoreFSM")
machines = sqlStore
}
default:
log.Info("Connected to CacheStoreFSM")
machines = cache.NewStore(cfg)
}
return machines
}