/
dao.go
68 lines (59 loc) · 1.4 KB
/
dao.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
package dao
import (
"context"
"database/sql"
m "github.com/fuwensun/goms/eApi/internal/model"
_ "github.com/go-sql-driver/mysql" // for init()
"github.com/gomodule/redigo/redis"
log "github.com/sirupsen/logrus"
)
// Dao dao interface.
type Dao interface {
Close()
Ping(ctx context.Context) (err error)
//count
ReadPing(ctx context.Context, t string) (*m.Ping, error)
UpdatePing(ctx context.Context, p *m.Ping) error
//user
CreateUser(ctx context.Context, user *m.User) error
ReadUser(ctx context.Context, uid int64) (*m.User, error)
UpdateUser(ctx context.Context, user *m.User) error
DeleteUser(ctx context.Context, uid int64) error
}
// dao dao struct.
type dao struct {
db *sql.DB
redis redis.Conn
}
// New new Dao and return.
func New(cfgpath string) (Dao, func(), error) {
return new(cfgpath)
}
// New new dao and return.
func new(cfgpath string) (*dao, func(), error) {
mdb, cleanDB, err := newDB(cfgpath)
if err != nil {
return nil, nil, err
}
log.Infof("db ok")
mcc, _, err := newCC(cfgpath)
if err != nil {
cleanDB()
return nil, nil, err
}
log.Infof("cc ok")
mdao := &dao{db: mdb, redis: mcc}
return mdao, mdao.Close, nil
}
// Close close the resource.
func (d *dao) Close() {
d.redis.Close()
d.db.Close()
}
// Ping ping the resource.
func (d *dao) Ping(ctx context.Context) (err error) {
if _, err = d.redis.Do("PING"); err != nil {
return
}
return d.db.PingContext(ctx)
}