123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- // Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
- /*
- Package testdata is a generated mc cache package.
- It is generated from:
- type _mc interface {
- // mc: -key=articleKey
- CacheArticles(c context.Context, keys []int64) (map[int64]*Article, error)
- // mc: -key=articleKey
- CacheArticle(c context.Context, key int64) (*Article, error)
- // mc: -key=keyMid
- CacheArticle1(c context.Context, key int64, mid int64) (*Article, error)
- // mc: -key=noneKey
- CacheNone(c context.Context) (*Article, error)
- // mc: -key=articleKey
- CacheString(c context.Context, key int64) (string, error)
- // mc: -key=articleKey -expire=d.articleExpire -encode=json
- AddCacheArticles(c context.Context, values map[int64]*Article) error
- // 这里也支持自定义注释 会替换默认的注释
- // mc: -key=articleKey -expire=d.articleExpire -encode=json|gzip
- AddCacheArticle(c context.Context, key int64, value *Article) error
- // mc: -key=keyMid -expire=d.articleExpire -encode=gob
- AddCacheArticle1(c context.Context, key int64, value *Article, mid int64) error
- // mc: -key=noneKey
- AddCacheNone(c context.Context, value *Article) error
- // mc: -key=articleKey -expire=d.articleExpire
- AddCacheString(c context.Context, key int64, value string) error
- // mc: -key=articleKey
- DelCacheArticles(c context.Context, keys []int64) error
- // mc: -key=articleKey
- DelCacheArticle(c context.Context, key int64) error
- // mc: -key=keyMid
- DelCacheArticle1(c context.Context, key int64, mid int64) error
- // mc: -key=noneKey
- DelCacheNone(c context.Context) error
- }
- */
- package testdata
- import (
- "context"
- "fmt"
- "go-common/library/cache/memcache"
- "go-common/library/log"
- "go-common/library/stat/prom"
- )
- var _ _mc
- // CacheArticles get data from mc
- func (d *Dao) CacheArticles(c context.Context, ids []int64) (res map[int64]*Article, err error) {
- l := len(ids)
- if l == 0 {
- return
- }
- keysMap := make(map[string]int64, l)
- keys := make([]string, 0, l)
- for _, id := range ids {
- key := articleKey(id)
- keysMap[key] = id
- keys = append(keys, key)
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- replies, err := conn.GetMulti(keys)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheArticles")
- log.Errorv(c, log.KV("CacheArticles", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
- return
- }
- for key, reply := range replies {
- var v *Article
- v = &Article{}
- err = conn.Scan(reply, v)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheArticles")
- log.Errorv(c, log.KV("CacheArticles", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- if res == nil {
- res = make(map[int64]*Article, len(keys))
- }
- res[keysMap[key]] = v
- }
- return
- }
- // CacheArticle get data from mc
- func (d *Dao) CacheArticle(c context.Context, id int64) (res *Article, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := articleKey(id)
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:CacheArticle")
- log.Errorv(c, log.KV("CacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- res = &Article{}
- err = conn.Scan(reply, res)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheArticle")
- log.Errorv(c, log.KV("CacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // CacheArticle1 get data from mc
- func (d *Dao) CacheArticle1(c context.Context, id int64, mid int64) (res *Article, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := keyMid(id, mid)
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:CacheArticle1")
- log.Errorv(c, log.KV("CacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- res = &Article{}
- err = conn.Scan(reply, res)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheArticle1")
- log.Errorv(c, log.KV("CacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // CacheNone get data from mc
- func (d *Dao) CacheNone(c context.Context) (res *Article, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := noneKey()
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:CacheNone")
- log.Errorv(c, log.KV("CacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- res = &Article{}
- err = conn.Scan(reply, res)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheNone")
- log.Errorv(c, log.KV("CacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // CacheString get data from mc
- func (d *Dao) CacheString(c context.Context, id int64) (res string, err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := articleKey(id)
- reply, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:CacheString")
- log.Errorv(c, log.KV("CacheString", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- err = conn.Scan(reply, &res)
- if err != nil {
- prom.BusinessErrCount.Incr("mc:CacheString")
- log.Errorv(c, log.KV("CacheString", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // AddCacheArticles Set data to mc
- func (d *Dao) AddCacheArticles(c context.Context, values map[int64]*Article) (err error) {
- if len(values) == 0 {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- for id, val := range values {
- key := articleKey(id)
- item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagJSON}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheArticles")
- log.Errorv(c, log.KV("AddCacheArticles", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- }
- return
- }
- // AddCacheArticle 这里也支持自定义注释 会替换默认的注释
- func (d *Dao) AddCacheArticle(c context.Context, id int64, val *Article) (err error) {
- if val == nil {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- key := articleKey(id)
- item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagJSON | memcache.FlagGzip}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheArticle")
- log.Errorv(c, log.KV("AddCacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // AddCacheArticle1 Set data to mc
- func (d *Dao) AddCacheArticle1(c context.Context, id int64, val *Article, mid int64) (err error) {
- if val == nil {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- key := keyMid(id, mid)
- item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagGOB}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheArticle1")
- log.Errorv(c, log.KV("AddCacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // AddCacheNone Set data to mc
- func (d *Dao) AddCacheNone(c context.Context, val *Article) (err error) {
- if val == nil {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- key := noneKey()
- item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagJSON}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheNone")
- log.Errorv(c, log.KV("AddCacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // AddCacheString Set data to mc
- func (d *Dao) AddCacheString(c context.Context, id int64, val string) (err error) {
- if len(val) == 0 {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- key := articleKey(id)
- bs := []byte(val)
- item := &memcache.Item{Key: key, Value: bs, Expiration: d.articleExpire, Flags: memcache.FlagRAW}
- if err = conn.Set(item); err != nil {
- prom.BusinessErrCount.Incr("mc:AddCacheString")
- log.Errorv(c, log.KV("AddCacheString", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // DelCacheArticles delete data from mc
- func (d *Dao) DelCacheArticles(c context.Context, ids []int64) (err error) {
- if len(ids) == 0 {
- return
- }
- conn := d.mc.Get(c)
- defer conn.Close()
- for _, id := range ids {
- key := articleKey(id)
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- continue
- }
- prom.BusinessErrCount.Incr("mc:DelCacheArticles")
- log.Errorv(c, log.KV("DelCacheArticles", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- }
- return
- }
- // DelCacheArticle delete data from mc
- func (d *Dao) DelCacheArticle(c context.Context, id int64) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := articleKey(id)
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:DelCacheArticle")
- log.Errorv(c, log.KV("DelCacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // DelCacheArticle1 delete data from mc
- func (d *Dao) DelCacheArticle1(c context.Context, id int64, mid int64) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := keyMid(id, mid)
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:DelCacheArticle1")
- log.Errorv(c, log.KV("DelCacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
- // DelCacheNone delete data from mc
- func (d *Dao) DelCacheNone(c context.Context) (err error) {
- conn := d.mc.Get(c)
- defer conn.Close()
- key := noneKey()
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- prom.BusinessErrCount.Incr("mc:DelCacheNone")
- log.Errorv(c, log.KV("DelCacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
- return
- }
- return
- }
|