123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- package dao
- import (
- "context"
- "fmt"
- "go-common/app/service/main/vip/model"
- gmc "go-common/library/cache/memcache"
- "go-common/library/log"
- "github.com/pkg/errors"
- )
- const (
- _vipInfo = "vo:%d"
- _open = "open:%d"
- _pointTip = "pointTip:%d:%d"
- _sign = "sign:%d"
- _vipfrozen = "vipfrozen:%d"
- )
- func pointTip(mid, id int64) string {
- return fmt.Sprintf(_pointTip, mid, id)
- }
- func openCode(mid int64) string {
- return fmt.Sprintf(_open, mid)
- }
- func vipInfoKey(mid int64) string {
- return fmt.Sprintf(_vipInfo, mid)
- }
- func signVip(mid int64) string {
- return fmt.Sprintf(_sign, mid)
- }
- func vipfrozen(mid int64) string {
- return fmt.Sprintf(_vipfrozen, mid)
- }
- //GetVipFrozen set vip frozen.
- func (d *Dao) GetVipFrozen(c context.Context, mid int64) (val int, err error) {
- var (
- key = vipfrozen(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item, err := conn.Get(key)
- if err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- d.errProm.Incr("vipfrozen_mc")
- return
- }
- if err = conn.Scan(item, &val); err != nil {
- err = errors.WithStack(err)
- return
- }
- return
- }
- //DelVipFrozen del vip frozen.
- func (d *Dao) DelVipFrozen(c context.Context, mid int64) (err error) {
- return d.DelCache(c, vipfrozen(mid))
- }
- // DelVipInfoCache delete vipinfo cache.
- func (d *Dao) DelVipInfoCache(c context.Context, mid int64) (err error) {
- return d.DelCache(c, vipInfoKey(mid))
- }
- // pingMC ping memcache.
- func (d *Dao) pingMC(c context.Context) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- if err = conn.Set(&gmc.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}); err != nil {
- log.Error("conn.Store(set, ping, 1) error(%v)", err)
- }
- return
- }
- // SetVipInfoCache set vip info cache.
- func (d *Dao) SetVipInfoCache(c context.Context, mid int64, v *model.VipInfo) (err error) {
- var (
- key = vipInfoKey(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item := &gmc.Item{Key: key, Object: v, Expiration: d.mcExpire, Flags: gmc.FlagProtobuf}
- if err = conn.Set(item); err != nil {
- err = errors.Wrapf(err, " conn.Set(%s)", key)
- d.errProm.Incr("vipinfo_mc")
- }
- return
- }
- // VipInfoCache get vip info.
- func (d *Dao) VipInfoCache(c context.Context, mid int64) (v *model.VipInfo, err error) {
- var (
- key = vipInfoKey(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item, err := conn.Get(key)
- if err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- d.errProm.Incr("vipinfo_mc")
- return
- }
- v = new(model.VipInfo)
- if err = conn.Scan(item, v); err != nil {
- err = errors.Wrapf(err, "conn.Scan(%s)", key)
- }
- return
- }
- //DelCache del cache.
- func (d *Dao) DelCache(c context.Context, key string) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- if err = conn.Delete(key); err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- } else {
- log.Error("conn.Delete(%s) error(%v)", key, err)
- d.errProm.Incr("del_mc")
- }
- }
- return
- }
- // GetOpenCodeCount get open code count.
- func (d *Dao) GetOpenCodeCount(c context.Context, mid int64) (val int, err error) {
- var (
- key = openCode(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item, err := conn.Get(key)
- if err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- d.errProm.Incr("opencode_mc")
- return
- }
- if err = conn.Scan(item, &val); err != nil {
- err = errors.WithStack(err)
- return
- }
- return
- }
- //SetOpenCode set open code.
- func (d *Dao) SetOpenCode(c context.Context, mid int64, count int) (err error) {
- var (
- key = openCode(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item := &gmc.Item{Key: key, Object: count, Expiration: d.mcExpire, Flags: gmc.FlagJSON}
- if err = conn.Set(item); err != nil {
- err = errors.WithStack(err)
- return
- }
- return
- }
- //GetPointTip get pointTip.
- func (d *Dao) GetPointTip(c context.Context, mid, id int64) (val int, err error) {
- var (
- key = pointTip(mid, id)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item, err := conn.Get(key)
- if err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- d.errProm.Incr("opencode_mc")
- return
- }
- if err = conn.Scan(item, &val); err != nil {
- return
- }
- return
- }
- //SetPointTip set point tip.
- func (d *Dao) SetPointTip(c context.Context, mid, id int64, val int, expired int32) (err error) {
- var (
- key = pointTip(mid, id)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item := &gmc.Item{Key: key, Object: val, Expiration: expired, Flags: gmc.FlagJSON}
- if err = conn.Set(item); err != nil {
- err = errors.WithStack(err)
- return
- }
- return
- }
- //SetSignVip .
- func (d *Dao) SetSignVip(c context.Context, mid int64, t int) (err error) {
- var (
- key = signVip(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item := &gmc.Item{Key: key, Object: 1, Expiration: int32(t), Flags: gmc.FlagJSON}
- if err = conn.Set(item); err != nil {
- err = errors.WithStack(err)
- return
- }
- return
- }
- //GetSignVip .
- func (d *Dao) GetSignVip(c context.Context, mid int64) (val int, err error) {
- var (
- key = signVip(mid)
- )
- conn := d.mc.Get(c)
- defer conn.Close()
- item, err := conn.Get(key)
- if err != nil {
- if err == gmc.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- d.errProm.Incr("signvip_mc")
- return
- }
- if err = conn.Scan(item, &val); err != nil {
- return
- }
- return
- }
- // AddTransferLock add lock.
- func (d *Dao) AddTransferLock(c context.Context, key string) (succeed bool) {
- var (
- conn = d.mc.Get(c)
- )
- defer conn.Close()
- item := &gmc.Item{
- Key: key,
- Value: []byte("0"),
- Expiration: 3600,
- }
- if err := conn.Add(item); err != nil {
- if err != gmc.ErrNotStored {
- log.Error("conn.Add(%s) error(%v)", key, err)
- }
- } else {
- succeed = true
- }
- return
- }
|