123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package kfc
- import (
- "context"
- "fmt"
- "strconv"
- "go-common/app/interface/main/activity/model/kfc"
- "go-common/library/cache/memcache"
- "go-common/library/log"
- "go-common/library/stat/prom"
- )
- var _ _mc
- func (d *Dao) CacheKfcCoupon(c context.Context, id int64) (res *kfc.BnjKfcCoupon, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := kfcKey(id)
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:CacheKfcCoupon")
- log.Errorv(c, log.KV("CacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- res = &kfc.BnjKfcCoupon{}
- err = conn.Scan(reply, res)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheKfcCoupon")
- log.Errorv(c, log.KV("CacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- func (d *Dao) AddCacheKfcCoupon(c context.Context, id int64, val *kfc.BnjKfcCoupon) (err error) {
- if val == nil {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- key := kfcKey(id)
- item := &memcache.Item{Key: key, Object: val, Expiration: d.mcKfcExpire, Flags: memcache.FlagProtobuf}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheKfcCoupon")
- log.Errorv(c, log.KV("AddCacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- func (d *Dao) DelCacheKfcCoupon(c context.Context, id int64) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := kfcKey(id)
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:DelCacheKfcCoupon")
- log.Errorv(c, log.KV("DelCacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- func (d *Dao) CacheKfcCode(c context.Context, id string) (res int64, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := kfcCodeKey(id)
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:CacheKfcCode")
- log.Errorv(c, log.KV("CacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- var v string
- err = conn.Scan(reply, &v)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheKfcCode")
- log.Errorv(c, log.KV("CacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- r, err := strconv.ParseInt(v, 10, 64)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheKfcCode")
- log.Errorv(c, log.KV("CacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- res = int64(r)
- return
- }
- func (d *Dao) AddCacheKfcCode(c context.Context, id string, val int64) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := kfcCodeKey(id)
- bs := []byte(strconv.FormatInt(int64(val), 10))
- item := &memcache.Item{Key: key, Value: bs, Expiration: d.mcKfcCodeExpire, Flags: memcache.FlagRAW}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheKfcCode")
- log.Errorv(c, log.KV("AddCacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
|