-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkvDb.go
191 lines (174 loc) · 4.01 KB
/
kvDb.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package go_utils
import (
"fmt"
"github.com/coreos/etcd/raft"
"github.com/dgraph-io/badger"
"io/ioutil"
"log"
"os"
"runtime"
"time"
)
// https://colobu.com/2017/10/11/badger-a-performant-k-v-store/
// https://juejin.cn/post/6844903814571491335
// key-value db chache
type KvCachedb struct {
DbConn *badger.DB
}
var Cache1 *KvCachedb
// 获取一个
func NewKvCachedb() *KvCachedb {
r := &KvCachedb{}
r.InitKv()
return r
}
func (r *KvCachedb) InitKv() *KvCachedb {
if nil != r.DbConn {
return r
}
CacheName11 := ".DbCache"
s1 := GetVal(CacheName)
if "" != s1 {
CacheName11 = s1
}
if runtime.GOOS == "windows" {
os.RemoveAll(CacheName11)
}
Mkdirs(CacheName11)
r.init(CacheName11)
return r
}
// SetDiscardTs sets a timestamp at or below which, any invalid or deleted versions can be discarded from the LSM tree, and thence from the value log to reclaim disk space. Can only be used with managed transactions.
func (r *KvCachedb) SetExpiresAt(ExpiresAt uint64) {
r.DbConn.SetDiscardTs(ExpiresAt)
}
// init db name
func (r *KvCachedb) init(szDb string) error {
opts := badger.DefaultOptions(szDb)
opts.CompactL0OnClose = true
opts.EventLogging = false
opts.Logger = nil
opts.LevelOneSize = 256 << 10
opts.LevelSizeMultiplier = 20
log1 := &raft.DefaultLogger{}
if GetVal("ProductMod") == "release" {
log1.Logger = log.New(ioutil.Discard, "", 0)
} else {
log1.Logger = log.New(os.Stderr, "kv-DB", log.LstdFlags)
}
opts.WithLogger(log1)
db, err := badger.Open(opts)
if nil != err {
log.Printf("InitConfigFile k-v db cannot open multiple processes at the same time, or please delete the %s directory and try again: %v", szDb, err)
return err
}
r.DbConn = db
return nil
}
// 遍历所有数据
func (r *KvCachedb) ScanAllData(f func(k string, value []byte)) {
Cache1.DbConn.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) error {
fmt.Printf("key=%s, value=%s\n", k, v)
f(string(k), v)
return nil
})
if err != nil {
return err
}
}
return nil
})
}
func (r *KvCachedb) Delete(key string) error {
err := r.DbConn.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte(key))
})
return err
}
func (r *KvCachedb) Close() {
if nil != r.DbConn {
r.DbConn.Close()
}
}
// https://www.modb.pro/db/87317
func (r *KvCachedb) GetKeyForData(key string) (szRst []byte) {
data, err := r.Get(key)
if nil != err {
//log.Println("GetKeyForData ", key, " is err ", err)
return []byte{}
}
return data
}
// https://www.modb.pro/db/87317
func (r *KvCachedb) Get(key string) (szRst []byte, err error) {
err = r.DbConn.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}
// val, err := item.Value()
err = item.Value(func(val []byte) error {
szRst = val
return nil
})
return err
})
return szRst, err
}
func PutAny[T any](key string, data T) {
if "" == key {
return
}
d, err := Json.Marshal(data)
if nil == err && nil != Cache1 {
Cache1.Put(key, d)
}
}
func GetAny[T any](key string) (T, error) {
var t1 T
data, err := Cache1.Get(key)
if nil == err {
Json.Unmarshal(data, &t1)
return t1, nil
}
return t1, err
}
// r.DbConn.RunValueLogGC()
func (r *KvCachedb) PutWithTTL(key string, data []byte, ttl time.Duration) {
err := r.DbConn.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(key), data).WithMeta(byte(1)).WithTTL(ttl)
err := txn.SetEntry(e)
if err == badger.ErrTxnTooBig {
_ = txn.Commit()
}
return err
})
if err != nil {
}
}
func (r *KvCachedb) Put(key string, data []byte) {
err := r.DbConn.Update(func(txn *badger.Txn) error {
err := txn.Set([]byte(key), data)
if err == badger.ErrTxnTooBig {
_ = txn.Commit()
}
return err
})
if err != nil {
}
}
// 调整初始化顺序
// 初始化 kvDb
func init() {
RegInitFunc(func() {
Cache1 = NewKvCachedb()
})
}