dao.go 888 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3
package dao

import (
4 5 6 7
	"context"
	"encoding/json"
	"fmt"
	"gomicro-base/common/logz"
kzkzzzz's avatar
kzkzzzz committed
8
	"gomicro-base/common/mysql"
9
	"gomicro-base/common/redis"
kzkzzzz's avatar
kzkzzzz committed
10 11 12 13 14 15 16
	"gomicro-base/service/member/internal/conf"
	"gomicro-base/service/member/internal/model"
	"gorm.io/gorm"
)

// Dao 查询数据库
type Dao struct {
17 18
	DB    *gorm.DB
	Redis *redis.WrapClient
kzkzzzz's avatar
kzkzzzz committed
19 20 21 22
}

func New(c *conf.Config) (d *Dao) {
	d = &Dao{
23 24
		DB:    mysql.NewDB(c.Mysql),
		Redis: redis.NewRedis(c.Redis),
kzkzzzz's avatar
kzkzzzz committed
25 26 27 28
	}
	return
}

29 30 31 32 33 34 35 36 37 38 39 40 41 42
func (d *Dao) FindCacheByPk(ctx context.Context, userIdKey string) (*model.CacheUser, error) {
	b, err := d.Redis.Get(ctx, userIdKey).Bytes()
	if err != nil {
		logz.Errorf("token error -1: %s", err)
		return nil, fmt.Errorf("token error -1")
	}

	cs := &model.CacheUser{}
	err = json.Unmarshal(b, cs)
	if err != nil {
		logz.Errorf("token error -2: %s", err)
		return nil, fmt.Errorf("token error -2")
	}
	return cs, nil
kzkzzzz's avatar
kzkzzzz committed
43
}