package cache import ( "fmt" "go_dreamfactory/lego/sys/redis" "go_dreamfactory/pb" "go_dreamfactory/sys/db" ) const ( //Redis Redis_FriendCache string = "friend:%s" Redis_Friend string = "apply:%s" ) func getRdsFriendKey(userId string) string { return fmt.Sprintf(Redis_FriendCache, userId) } type IFriend interface { Friend_Update(data *pb.Cache_FriendData) (err error) Friend_Total(userId string) int32 Friend_Get(userId string) (*pb.Cache_FriendData, error) } //更新 func (this *Cache) Friend_Update(data *pb.Cache_FriendData) (err error) { if err = db.Defsys.Friend_SaveOrUpdate(data); err == nil { //更新缓存 _, err = this.redis.Set(getRdsFriendKey(data.UserId), data, 0) } return } //好友总数 func (this *Cache) Friend_Total(userId string) int32 { var friend *pb.Cache_FriendData err := this.redis.Get(getRdsFriendKey(userId), &friend) if err != nil { return 0 } return int32(len(friend.FriendIds)) } //好友获取 func (this *Cache) Friend_Get(userId string) (*pb.Cache_FriendData, error) { var d *pb.Cache_FriendData err := this.redis.Get(getRdsFriendKey(userId), &d) if err != nil { if err.Error() == string(redis.RedisNil) { d = &pb.Cache_FriendData{UserId: userId} err = this.Friend_Update(d) if err != nil { return d, nil } } return nil, err } return d, nil }