/
cache.go
128 lines (99 loc) · 2.04 KB
/
cache.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
package wxwork
import (
"log"
"os"
"path"
"sync"
"github.com/boltdb/bolt"
)
// Cache 缓存接口,自定义缓存需实现此接口
type Cache interface {
Set(key string, value []byte) error
Get(key string) []byte
Remove(key string) error
}
const (
defaultBoltDBFile = `.data/wework/db/wework.db`
defaultBoltBucket = `wework`
)
// boltCache bolt 缓存器
type boltCache struct {
bolt *bolt.DB
dbfile string
bucket string
mu sync.Mutex
}
// pathExist 检查存放db文件的文件夹是否存在。
// 如果db文件存在运行目录下,则无操作
func pathExist(dbfile string) error {
dir, _ := path.Split(dbfile)
if dir != `` {
if _, err := os.Stat(dir); !os.IsExist(err) {
return os.MkdirAll(dir, os.ModePerm)
}
}
return nil
}
// Bolt new bolt brain ...
func Bolt() Cache {
b := new(boltCache)
dbfile := os.Getenv(`BOLT_DB_FILE`)
if dbfile == `` {
dbfile = defaultBoltDBFile
}
err := pathExist(dbfile)
if err != nil {
return nil
}
bucket := os.Getenv("BOLT_BUCKET")
if bucket == `` {
bucket = defaultBoltBucket
}
db, err := bolt.Open(dbfile, 0600, nil)
if err != nil {
return nil
}
b.bolt = db
b.dbfile = dbfile
b.bucket = bucket
return b
}
// Set save ...
func (b *boltCache) Set(key string, value []byte) error {
b.mu.Lock()
defer b.mu.Unlock()
err := b.bolt.Update(func(tx *bolt.Tx) error {
b, e := tx.CreateBucketIfNotExists([]byte(b.bucket))
if e != nil {
log.Printf("bolt: error saving: %v", e)
return e
}
return b.Put([]byte(key), value)
})
return err
}
// Get find ...
func (b *boltCache) Get(key string) []byte {
b.mu.Lock()
defer b.mu.Unlock()
var found []byte
b.bolt.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(b.bucket))
if b == nil {
return nil
}
found = b.Get([]byte(key))
return nil
})
return found
}
// Remove remove ...
func (b *boltCache) Remove(key string) error {
b.mu.Lock()
defer b.mu.Unlock()
err := b.bolt.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(b.bucket))
return b.Delete([]byte(key))
})
return err
}