123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package dao
- import (
- "context"
- "fmt"
- "go-common/app/service/main/push/model"
- "go-common/library/cache/memcache"
- "go-common/library/log"
- "go-common/library/stat/prom"
- )
- var _ _mc
- func (d *Dao) TokenCache(c context.Context, id string) (res *model.Report, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := tokenKey(id)
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:TokenCache")
- log.Errorv(c, log.KV("TokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- res = &model.Report{}
- err = conn.Scan(reply, res)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:TokenCache")
- log.Errorv(c, log.KV("TokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- func (d *Dao) AddTokenCache(c context.Context, id string, val *model.Report) (err error) {
- if val == nil {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- key := tokenKey(id)
- item := &memcache.Item{Key: key, Object: val, Expiration: d.mcReportExpire, Flags: memcache.FlagJSON}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddTokenCache")
- log.Errorv(c, log.KV("AddTokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- func (d *Dao) AddTokensCache(c context.Context, values map[string]*model.Report) (err error) {
- if len(values) == 0 {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- for id, val := range values {
- key := tokenKey(id)
- item := &memcache.Item{Key: key, Object: val, Expiration: d.mcReportExpire, Flags: memcache.FlagJSON}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddTokensCache")
- log.Errorv(c, log.KV("AddTokensCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- }
- return
- }
- func (d *Dao) DelTokenCache(c context.Context, id string) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := tokenKey(id)
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:DelTokenCache")
- log.Errorv(c, log.KV("DelTokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
|