-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
69 lines (63 loc) · 1.33 KB
/
data.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
package data
import (
"github.com/jinzhu/gorm"
"github.com/kaiiak/shorturl/config"
"github.com/kaiiak/shorturl/data/cache"
"github.com/kaiiak/shorturl/data/db"
)
// Data 数据管理
type Data struct {
db *db.ShortURLDB
cache *cache.Cache
}
// New 控制数据库和缓存
func New(cnf *config.Config) (*Data, error) {
tempDb, err := db.New(cnf.DBType, cnf.DBPath)
if err != nil {
return nil, err
}
tempCache, err := cache.New(cnf.CachePath, cnf.CachePwd)
if err != nil {
return nil, err
}
return &Data{tempDb, tempCache}, nil
}
// Get 获取数据
func (d *Data) Get(shorturlStr string) (string, error) {
v, err := d.cache.Get(shorturlStr)
if err != nil {
if err != cache.ErrNotExist {
return "", err
}
}
if v != "" {
return v, nil
}
v, err = d.db.Get(shorturlStr)
if err != nil {
if err == gorm.ErrRecordNotFound {
return "", ErrNotFound
}
return "", err
}
go func() {
if er := d.cache.Set(shorturlStr, v); er != nil {
// zap.S().Errorf("cache set: [%s], error: [%s]", shorturlStr, err)
}
}()
return v, nil
}
// Set 设置,如果已存在则返回原始的
func (d *Data) Set(raw string) (string, error) {
um, err := d.db.Set(raw)
if err != nil {
return "", err
}
return um.ShortURL, nil
}
// Close 安全的关闭
func (d *Data) Close() error {
d.cache.Close()
d.db.CLose()
return nil
}