like

package
v0.0.0-...-f5e7097 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

通用点赞业务组件实现

使用set和hash实现

点赞

  • 用于增量储存变动的文章id set
fmt.Sprintf("%s_like_change_set", l.RedisKey) 
  • 用于增量储存变动的文章id对应的用户id set
fmt.Sprintf("%s_like_change_user_set_%s", l.RedisKey, likeId) 
  • 用于增量储存文章-用户的点赞属性 如点赞时间,点赞类型(点赞,取消赞)
fmt.Sprintf("%s_like_attr_%s_%s", l.RedisKey, likeId, userId) 
  • 用于文章的所有点赞用户id
fmt.Sprintf("%s_like_set_%s", l.RedisKey, likeId) 
  • 用于文章的点赞总数
fmt.Sprintf("%s_like_%s_counter", l.RedisKey, likeId) 

提供以下接口

type ILike interface {
	Like(likeId string, userId string) (err error) //点赞
	UnLike(likeId string, userId string) (err error) //取消赞
	IsLike(likeId string, userId string) (flag bool, err error) //是否点赞
	Count(likeId string) (count int64, err error) //文章的点赞总数
}

点赞异步入库

使用方式:传入一个实现IStorageAction的对象就可以进行入库操作 NewStorage(redisKey, new(defaultStorageAction)).AsyncStorage()

type IStorageAction interface {
	Like(insertList []InsertData)            //入库为赞的操作
	UnLike(delData DelData)                  //入库为取消赞时的操作
	InsOrUpCount(likeId string, count int64) //入库修改数量
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DelData

type DelData struct {
	LikeId  string
	UserIds []string
}

type ICacheLike

type ICacheLike interface {
	AsyncStorage() //异步入库
}

func NewStorage

func NewStorage(redisKey string, action IStorageAction) ICacheLike

入库操作方法 传入IStorageAction进行mysql入库操作

type ILike

type ILike interface {
	Like(likeId string, userId string) (err error)              //点赞
	UnLike(likeId string, userId string) (err error)            //取消赞
	IsLike(likeId string, userId string) (flag bool, err error) //是否点赞
	Count(likeId string) (count int64, err error)               //文章的点赞总数
}

点赞接口

func New

func New(ctx context.Context, opt *Option) ILike

type IStorageAction

type IStorageAction interface {
	Like(insertList []InsertData)            //入库为赞的操作
	UnLike(delData DelData)                  //入库为取消赞时的操作
	InsOrUpCount(likeId string, count int64) //入库修改数量
}

type InsertData

type InsertData struct {
	LikeId string
	UserId string

	CustomAttr g.MapStrAny
	// contains filtered or unexported fields
}

type Option

type Option struct {
	RedisKey       string                                         //redis key前缀
	CustomAttrFunc func(likeId string, userId string) g.MapStrAny //自定义增量属性hash的数据 用于异步入库时使用
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL