Skip to content

Instantly share code, notes, and snippets.

@trongdth
Created November 3, 2019 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trongdth/f48fcfdfc1ae31484edfeaf28b3c5e38 to your computer and use it in GitHub Desktop.
Save trongdth/f48fcfdfc1ae31484edfeaf28b3c5e38 to your computer and use it in GitHub Desktop.
package servers
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/trongdth/go_microservices/entry-cache/daos"
"github.com/trongdth/go_microservices/entry-cache/services"
pb "github.com/trongdth/go_protobuf"
)
// User : struct
type User struct {
userSvc *services.UserSvc
ud *daos.User
}
// NewUserServer :
func NewUserServer(userSvc *services.UserSvc, ud *daos.User) *User {
return &User{
userSvc: userSvc,
ud: ud,
}
}
// CreateUser : context, user request
func (u *User) CreateUser(ctx context.Context, req *pb.UserReq) (*pb.UserRes, error) {
return u.userSvc.CreateUser(ctx, req)
}
// ReadUser : context, user request
func (u *User) ReadUser(ctx context.Context, req *pb.UserReq) (*pb.UserRes, error) {
if req.Req.Message == pb.Message_QUERY_USER_ID {
ID := req.User.GetId()
cache, _ := u.ud.GetUser(ID)
if cache != nil {
var userInfo *pb.UserInfo
err := json.Unmarshal([]byte(cache.(string)), &userInfo)
if err == nil {
fmt.Println("return data from cache")
return &pb.UserRes{
Result: true,
User: userInfo,
}, nil
}
}
}
res, err := u.userSvc.ReadUser(ctx, req)
if res != nil {
err := u.ud.SetUser(res.GetUser().Id, res.GetUser())
if err != nil {
log.Println(err)
}
}
return res, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment