123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- package archive
- import (
- "context"
- "strconv"
- "go-common/app/interface/main/app-intl/model/view"
- "go-common/app/service/main/archive/api"
- "go-common/app/service/main/archive/model/archive"
- "go-common/library/cache/memcache"
- "go-common/library/log"
- "github.com/pkg/errors"
- )
- const (
- _prefixRelate = "al_"
- _prefix = "a3p_"
- _prxfixSt = "stp_"
- _prefixViewStatic = "avp_"
- _prefixViewContribute = "avpc_"
- )
- // keyArc is.
- func keyArc(aid int64) string {
- return _prefix + strconv.FormatInt(aid, 10)
- }
- // keySt is.
- func keySt(aid int64) string {
- return _prxfixSt + strconv.FormatInt(aid, 10)
- }
- // keyView is.
- func keyView(aid int64) string {
- return _prefixViewStatic + strconv.FormatInt(aid, 10)
- }
- // keyRl is.
- func keyRl(aid int64) string {
- return _prefixRelate + strconv.FormatInt(aid, 10)
- }
- // keyViewContribute is.
- func keyViewContribute(mid int64) string {
- return _prefixViewContribute + strconv.FormatInt(mid, 10)
- }
- // pingMC is.
- func (d *Dao) pingMC(c context.Context) (err error) {
- conn := d.mc.Get(c)
- err = conn.Set(&memcache.Item{Key: "ping", Object: []byte{1}, Flags: memcache.FlagJSON, Expiration: 0})
- conn.Close()
- return
- }
- // statsCache get stat cache by aids
- func (d *Dao) statsCache(c context.Context, aids []int64) (cached map[int64]*api.Stat, missed []int64, err error) {
- cached = make(map[int64]*api.Stat, len(aids))
- var (
- conn = d.mc.Get(c)
- keys = make([]string, 0, len(aids))
- rs map[string]*memcache.Item
- )
- defer conn.Close()
- for _, aid := range aids {
- keys = append(keys, keySt(aid))
- }
- if rs, err = conn.GetMulti(keys); err != nil {
- err = errors.Wrapf(err, "%v", keys)
- return
- }
- for _, item := range rs {
- var st = &api.Stat{}
- if err = conn.Scan(item, st); err != nil {
- log.Error("conn.Scan(%s) error(%v)", item.Value, err)
- err = nil
- continue
- }
- cached[st.Aid] = st
- }
- if len(cached) == len(aids) {
- return
- }
- for _, aid := range aids {
- if _, ok := cached[aid]; !ok {
- missed = append(missed, aid)
- }
- }
- return
- }
- // arcsCache get archives cache.
- func (d *Dao) arcsCache(c context.Context, aids []int64) (cached map[int64]*api.Arc, missed []int64, err error) {
- var (
- keys = make([]string, 0, len(aids))
- conn = d.mc.Get(c)
- aidmap = make(map[string]int64, len(aids))
- rs map[string]*memcache.Item
- a *api.Arc
- )
- cached = make(map[int64]*api.Arc, len(aids))
- defer conn.Close()
- for _, aid := range aids {
- k := keyArc(aid)
- if _, ok := aidmap[k]; !ok {
- keys = append(keys, k)
- aidmap[k] = aid
- }
- }
- if rs, err = conn.GetMulti(keys); err != nil {
- err = errors.Wrapf(err, "%v", keys)
- return
- }
- for k, r := range rs {
- a = &api.Arc{}
- if err = conn.Scan(r, a); err != nil {
- log.Error("conn.Scan(%s) error(%v)", r.Value, err)
- err = nil
- continue
- }
- cached[aidmap[k]] = a
- // delete hit key
- delete(aidmap, k)
- }
- // missed key
- missed = make([]int64, 0, len(aidmap))
- for _, aid := range aidmap {
- missed = append(missed, aid)
- }
- return
- }
- // arcCache get archive cache.
- func (d *Dao) arcCache(c context.Context, aid int64) (a *api.Arc, err error) {
- conn := d.mc.Get(c)
- key := keyArc(aid)
- defer conn.Close()
- r, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- return
- }
- a = &api.Arc{}
- if err = conn.Scan(r, a); err != nil {
- a = nil
- err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
- }
- return
- }
- // viewCache get view cache from remote memecache .
- func (d *Dao) viewCache(c context.Context, aid int64) (vs *archive.View3, err error) {
- conn := d.mc.Get(c)
- key := keyView(aid)
- defer conn.Close()
- r, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- return
- }
- vs = &archive.View3{Archive3: &archive.Archive3{}}
- if err = conn.Scan(r, vs); err != nil {
- vs = nil
- err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
- }
- return
- }
- // statCache get a archive stat from cache.
- func (d *Dao) statCache(c context.Context, aid int64) (st *api.Stat, err error) {
- conn := d.mc.Get(c)
- key := keySt(aid)
- defer conn.Close()
- r, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- return
- }
- st = &api.Stat{}
- if err = conn.Scan(r, st); err != nil {
- st = nil
- err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
- }
- return
- }
- // RelatesCache get relates.
- func (d *Dao) RelatesCache(c context.Context, aid int64) (rls []*view.Relate, err error) {
- conn := d.mc.Get(c)
- key := keyRl(aid)
- defer conn.Close()
- r, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- return
- }
- if err = conn.Scan(r, &rls); err != nil {
- err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
- }
- return
- }
- // AddRelatesCache add relates
- func (d *Dao) AddRelatesCache(aid int64, rls []*view.Relate) {
- d.addCache(func() {
- d.addRelatesCache(context.Background(), aid, rls)
- })
- }
- // addRelatesCache add relates cache.
- func (d *Dao) addRelatesCache(c context.Context, aid int64, rls []*view.Relate) (err error) {
- conn := d.mc.Get(c)
- key := keyRl(aid)
- item := &memcache.Item{Key: key, Object: rls, Flags: memcache.FlagJSON, Expiration: d.expireRlt}
- if err = conn.Set(item); err != nil {
- err = errors.Wrapf(err, "conn.Set(%s,%v,%d)", key, rls, d.expireRlt)
- }
- conn.Close()
- return
- }
- // ViewContributeCache get archive cache.
- func (d *Dao) ViewContributeCache(c context.Context, mid int64) (aids []int64, err error) {
- conn := d.mc.Get(c)
- key := keyViewContribute(mid)
- defer conn.Close()
- r, err := conn.Get(key)
- if err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- err = errors.Wrapf(err, "conn.Get(%s)", key)
- return
- }
- if err = conn.Scan(r, &aids); err != nil {
- err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
- }
- return
- }
|