Skip to content

Commit

Permalink
update err hand for eRedis
Browse files Browse the repository at this point in the history
  • Loading branch information
fuwensun committed Mar 10, 2020
1 parent 7f30e61 commit 6aedeb3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion eRedis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ curl -X PUT -d "name=xxx&sex=1" localhost:8080/user/123
# 使用 http 方法 GET /user/user, 参数 uid=123
curl -X GET localhost:8080/user/123
curl -X GET localhost:8080/user/?uid=123
curl -X GET localhost:8080/user?uid=123
# 使用 http 方法 DELETE /user/user, 参数 uid=123
curl -X DELETE localhost:8080/user/123
Expand Down
2 changes: 1 addition & 1 deletion eRedis/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Printf("get a signal %s", s.String())
log.Printf("get a signal: %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
Expand Down
17 changes: 7 additions & 10 deletions eRedis/internal/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func New(confpath string) Dao {
var dc DBConfig
pathname := filepath.Join(confpath, DBconffile)
if err := conf.GetConf(pathname, &dc); err != nil {
log.Printf("failed to get db config file! error: %v", err)
log.Printf("get db config file: %v", err)
}

if dc.DSN != "" {
Expand All @@ -82,21 +82,19 @@ func New(confpath string) Dao {
log.Printf("get env db DSN: %v", dsn)
}
}
log.Printf("db DSN: %v", DSN)

mdb, err := sql.Open("mysql", DSN)
if err != nil {
log.Panicf("failed to open db! error: %v", err)
log.Panicf("failed to open db: %v", err)
}
if err := mdb.Ping(); err != nil {
log.Panicf("failed to ping db! error: %v", err)
log.Panicf("failed to ping db: %v", err)
}

//rd
var rc RDConfig
pathname = filepath.Join(confpath, RDconffile)
if err := conf.GetConf(pathname, &rc); err != nil {
log.Printf("failed to get rc config file! error: %v", err)
log.Printf("get rc config file: %v", err)
}

if rc.Addr != "" {
Expand All @@ -108,17 +106,16 @@ func New(confpath string) Dao {
log.Printf("get env cc ADDR: %v", addr)
}
}
log.Printf("rc addr: %v", ADDR)

mrd, err := redis.Dial("tcp", ADDR)
if err != nil {
log.Panicf("failed to conn redis! error: %v", err)
log.Panicf("failed to conn redis: %v", err)
}
if _, err := mrd.Do("PING"); err != nil {
log.Panicf("failed to ping redis! error: %v", err)
log.Panicf("failed to ping redis: %v", err)
}
if _, err := mrd.Do("FLUSHDB"); err != nil {
log.Panicf("failed to flush redis! error: %v", err)
log.Panicf("failed to flush redis: %v", err)
}

return &dao{
Expand Down
6 changes: 3 additions & 3 deletions eRedis/internal/dao/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
func (d *dao) UpdatePingCount(c context.Context, t model.PingType, v model.PingCount) error {
db := d.db
if _, err := db.Exec(_updatePingCount, v, t); err != nil {
err = fmt.Errorf("mysql exec update: %w", err)
err = fmt.Errorf("db exec update: %w", err)
return err
}
return nil
Expand All @@ -26,13 +26,13 @@ func (d *dao) ReadPingCount(c context.Context, t model.PingType) (pc model.PingC
rows, err := db.Query(_readPingCount, t)
defer rows.Close()
if err != nil {
err = fmt.Errorf("mysql query: %w", err)
err = fmt.Errorf("db query: %w", err)
return
}
if rows.Next() {
err = rows.Scan(&pc)
if err != nil {
err = fmt.Errorf("mysql rows scan: %w", err)
err = fmt.Errorf("db rows scan: %w", err)
return
}
}
Expand Down
53 changes: 29 additions & 24 deletions eRedis/internal/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ func (d *dao) existUserCache(c context.Context, uid int64) (bool, error) {
key := model.GetRedisKey(uid)
exist, err := redis.Bool(rd.Do("EXISTS", key))
if err != nil {
err = fmt.Errorf("redis Do EXISTS err: %w", err)
err = fmt.Errorf("cc do EXISTS: %w", err)
return exist, err
}
log.Printf("redis exist=%v key=%v", exist, key)
log.Printf("cc exist=%v key=%v", exist, key)
return exist, nil
}

func (d *dao) setUserCache(c context.Context, user *model.User) error {
rd := d.redis
key := model.GetRedisKey(user.Uid)
if _, err := rd.Do("HMSET", redis.Args{}.Add(key).AddFlat(user)...); err != nil {
err = fmt.Errorf("redis Do HMSET err: %w", err)
err = fmt.Errorf("cc do HMSET: %w", err)
return err
}
log.Printf("redis set key=%v, value=%v", key, user)
log.Printf("cc set key=%v, value=%v", key, user)
return nil
}

Expand All @@ -52,63 +52,63 @@ func (d *dao) getUserCache(c context.Context, uid int64) (model.User, error) {
key := model.GetRedisKey(uid)
value, err := redis.Values(rd.Do("HGETALL", key))
if err != nil {
err = fmt.Errorf("redis Do HGETALL err: %w", err)
err = fmt.Errorf("cc do HGETALL: %w", err)
return user, err
}
if err = redis.ScanStruct(value, &user); err != nil {
err = fmt.Errorf("redis ScanStruct err: %w", err)
err = fmt.Errorf("cc ScanStruct: %w", err)
return user, err
}
log.Printf("redis get key=%v, value=%v", key, user)
log.Printf("cc get key=%v, value=%v", key, user)
return user, nil
}

func (d *dao) delUserCache(c context.Context, uid int64) error {
rd := d.redis
key := model.GetRedisKey(uid)
if _, err := rd.Do("DEL", key); err != nil {
err = fmt.Errorf("redis Do DEL err: %w", err)
err = fmt.Errorf("cc do DEL: %w", err)
return err
}
log.Printf("redis delete key=%v", key)
log.Printf("cc delete key=%v", key)
return nil
}

func (d *dao) createUserDB(c context.Context, user *model.User) error {
db := d.db
result, err := db.Exec(_createUser, user.Uid, user.Name, user.Sex)
if err != nil {
err = fmt.Errorf("mysql exec insert err: %w", err)
err = fmt.Errorf("db exec insert: %w", err)
return err
}
num, err := result.RowsAffected()
if err != nil {
err = fmt.Errorf("mysql rows affected err: %w", err)
err = fmt.Errorf("db rows affected: %w", err)
return err
}
if num == 0 {
return model.ErrFailedCreateData
}
log.Printf("mysql insert user=%v ", user)
log.Printf("db insert user=%v ", user)
return nil
}

func (d *dao) updateUserDB(c context.Context, user *model.User) error {
db := d.db
result, err := db.Exec(_updateUser, user.Name, user.Sex, user.Uid)
if err != nil {
err = fmt.Errorf("mysql exec update err: %w", err)
err = fmt.Errorf("db exec update: %w", err)
return err
}
num, err := result.RowsAffected()
if err != nil {
err = fmt.Errorf("mysql rows affected err: %w", err)
err = fmt.Errorf("db rows affected: %w", err)
return err
}
if num == 0 {
return model.ErrNotFoundData
}
log.Printf("mysql update user=%v, affected=%v ", user, num)
log.Printf("db update user=%v, affected=%v ", user, num)
return nil
}

Expand All @@ -118,15 +118,15 @@ func (d *dao) readUserDB(c context.Context, uid int64) (model.User, error) {
rows, err := db.Query(_readUser, uid)
defer rows.Close()
if err != nil {
err = fmt.Errorf("mysql query err: %w", err)
err = fmt.Errorf("db query: %w", err)
return user, err
}
if rows.Next() {
if err = rows.Scan(&user.Uid, &user.Name, &user.Sex); err != nil {
err = fmt.Errorf("mysql rows scan err: %w", err)
err = fmt.Errorf("db rows scan: %w", err)
return user, err
}
log.Printf("mysql read user=%v ", user)
log.Printf("db read user=%v ", user)
return user, nil
}
//???
Expand All @@ -137,25 +137,25 @@ func (d *dao) deleteUserDB(c context.Context, uid int64) error {
db := d.db
result, err := db.Exec(_deleteUser, uid)
if err != nil {
err = fmt.Errorf("mysql exec delete err: %w", err)
err = fmt.Errorf("db exec delete: %w", err)
return err
}
num, err := result.RowsAffected()
if err != nil {
err = fmt.Errorf("mysql rows affected err: %w", err)
err = fmt.Errorf("db rows affected: %w", err)
return err
}
if num == 0 {
return model.ErrNotFoundData
}
log.Printf("mysql delete user uid=%v, affected=%v ", uid, num)
log.Printf("db delete user uid=%v, affected=%v ", uid, num)
return nil
}

//
func (d *dao) CreateUser(c context.Context, user *model.User) error {
if err := d.createUserDB(c, user); err != nil {
err = fmt.Errorf("create user,db err: %w", err)
err = fmt.Errorf("create user in db: %w", err)
return err
}
return nil
Expand All @@ -164,11 +164,11 @@ func (d *dao) CreateUser(c context.Context, user *model.User) error {
//
func (d *dao) UpdateUser(c context.Context, user *model.User) error {
if err := d.updateUserDB(c, user); err != nil {
err = fmt.Errorf("update user,db err: %w", err)
err = fmt.Errorf("update user in db: %w", err)
return err
}
if err := d.delUserCache(c, user.Uid); err != nil {
err = fmt.Errorf("delete user,cache err: %w", err)
err = fmt.Errorf("delete user in cc: %w", err)
return err
}
return nil
Expand All @@ -182,16 +182,19 @@ func (d *dao) ReadUser(c context.Context, uid int64) (model.User, error) {
//cache 命中,返回
if exist {
if user, err := d.getUserCache(c, uid); err != nil {
err = fmt.Errorf("get user from cc: %w", err)
return user, err
}
return user, nil
}
//cache 没命中,读 DB
if user, err = d.readUserDB(c, uid); err != nil {
err = fmt.Errorf("read user from db: %w", err)
return user, err
}
//回种 cache
if err = d.setUserCache(c, &user); err != nil {
err = fmt.Errorf("set user to cc: %w", err)
return user, err
}
//DB 读到的值
Expand All @@ -200,9 +203,11 @@ func (d *dao) ReadUser(c context.Context, uid int64) (model.User, error) {

func (d *dao) DeleteUser(c context.Context, uid int64) error {
if err := d.deleteUserDB(c, uid); err != nil {
err = fmt.Errorf("del user in db: %w", err)
return err
}
if err := d.delUserCache(c, uid); err != nil {
err = fmt.Errorf("del user in cc: %w", err)
return err
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions eRedis/internal/server/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func createUser(c *gin.Context) {
user.Name = namestr

if err = svc.CreateUser(c, &user); err != nil {
log.Printf("http create user, err: %v", err)
log.Printf("http create user: %v", err)
c.JSON(http.StatusNotFound, gin.H{
"error": "create failed!",
"uid": user.Uid,
Expand Down Expand Up @@ -76,7 +76,7 @@ func updateUser(c *gin.Context) {
user.Name = namestr
err = svc.UpdateUser(c, &user)
if err != nil {
log.Printf("http update user,err: %v\n", err)
log.Printf("http update user: %v\n", err)
c.JSON(http.StatusNotFound, gin.H{
"error": "data not found!",
"uid": user.Uid,
Expand Down Expand Up @@ -109,7 +109,7 @@ func readUser(c *gin.Context) {
}
user, err := svc.ReadUser(c, uid)
if err != nil {
log.Printf("http read user,err: %v\n", err)
log.Printf("http read user: %v\n", err)
c.JSON(http.StatusNotFound, gin.H{
"error": "data not found!",
"uid": uidstr,
Expand All @@ -136,7 +136,7 @@ func deleteUser(c *gin.Context) {
return
}
if err = svc.DeleteUser(c, uid); err != nil {
log.Printf("http delete user,err: %v\n", err)
log.Printf("http delete user: %v\n", err)
c.JSON(http.StatusNotFound, gin.H{
"error": "data not found!",
"uid": uidstr,
Expand Down

0 comments on commit 6aedeb3

Please sign in to comment.