package repo import ( "gim/internal/business/domain/user/model" "gim/pkg/db" "gim/pkg/gerrors" "strconv" "time" "github.com/go-redis/redis" ) const ( MasterKey = "master:" MasterExpire = 30 * time.Minute ) type masterCache struct{} var MasterCache = new(masterCache) // Get 获取用户缓存 func (c *masterCache) Get(masterId int64) (*model.Master, error) { var master model.Master err := db.RedisUtil.Get(MasterKey+strconv.FormatInt(masterId, 10), &master) if err != nil && err != redis.Nil { return nil, gerrors.WrapError(err) } if err == redis.Nil { return nil, nil } return &master, nil } // Set 设置用户缓存 func (c *masterCache) Set(master model.Master) error { err := db.RedisUtil.Set(MasterKey+strconv.FormatInt(master.MasterId, 10), master, MasterExpire) if err != nil { return gerrors.WrapError(err) } return nil } // Del 删除用户缓存 func (c *masterCache) Del(masterId int64) error { _, err := db.RedisCli.Del(MasterKey + strconv.FormatInt(masterId, 10)).Result() if err != nil { return gerrors.WrapError(err) } return nil }