forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
76 lines (68 loc) · 1.45 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gou
import (
"fmt"
"io"
"os"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/helper"
"github.com/yaoapp/gou/kv"
"github.com/yaoapp/gou/kv/lru"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
)
// LoadStore load kv store
func LoadStore(source string, name string) (kv.Store, error) {
var input io.Reader = nil
if strings.HasPrefix(source, "file://") {
filename := strings.TrimPrefix(source, "file://")
file, err := os.Open(filename)
if err != nil {
exception.Err(err, 400).Throw()
}
defer file.Close()
input = file
} else {
input = strings.NewReader(source)
}
config, err := helper.ReadFile(input)
if err != nil {
return nil, err
}
store := Store{}
err = jsoniter.Unmarshal(config, &store)
if err != nil {
return nil, err
}
typ := strings.ToLower(store.Type)
switch typ {
case "lru":
s := 1024
if size, ok := store.Option["size"]; ok {
switch size.(type) {
case string:
size = os.Getenv(size.(string))
}
s = any.Of(size).CInt()
if s == 0 {
s = 1024
}
}
cache, err := lru.New(s)
if err != nil {
return nil, err
}
kv.Pools[name] = cache
return cache, err
default:
return nil, fmt.Errorf("Store %s Type:%s does not support yet", name, typ)
}
}
// SelectStore Select loaded kv store
func SelectStore(name string) kv.Store {
store, has := kv.Pools[name]
if !has {
exception.New("Store:%s does not load", 500, name).Throw()
}
return store
}