123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package dao
- import (
- "context"
- "fmt"
- "strconv"
- "go-common/app/job/main/member-cache/model"
- "go-common/library/cache/memcache"
- "go-common/library/log"
- "github.com/pkg/errors"
- )
- const (
- _expPrefix = "exp_%d"
- _expExpire = 86400
- )
- func expKey(mid int64) string {
- return fmt.Sprintf(_expPrefix, mid)
- }
- func keyInfo(mid int64) string {
- return model.CacheKeyInfo + strconv.FormatInt(mid, 10)
- }
- func (d *Dao) mcBaseKey(mid int64) (key string) {
- return fmt.Sprintf(model.CacheKeyBase, mid)
- }
- func (d *Dao) moralKey(mid int64) (key string) {
- return fmt.Sprintf(model.CacheKeyMoral, mid)
- }
- func realnameInfoKey(mid int64) string {
- return fmt.Sprintf("realname_info_%d", mid)
- }
- func realnameApplyStatusKey(mid int64) string {
- return fmt.Sprintf("realname_apply_%d", mid)
- }
- // DelMoralCache delete moral cache.
- func (d *Dao) DelMoralCache(c context.Context, mid int64) (err error) {
- key := d.moralKey(mid)
- conn := d.memberMemcache.Get(c)
- defer conn.Close()
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- log.Error("conn.Delete(%s) error(%v)", key, err)
- }
- return
- }
- // DelBaseInfoCache delete baseInfo cache.
- func (d *Dao) DelBaseInfoCache(c context.Context, mid int64) (err error) {
- key := d.mcBaseKey(mid)
- conn := d.memberMemcache.Get(c)
- defer conn.Close()
- if err = conn.Delete(key); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- return
- }
- log.Error("conn.Delete(%s) error(%v)", key, err)
- }
- return
- }
- // DelInfoCache delete account info from cache.
- func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
- conn := d.memberMemcache.Get(c)
- err = conn.Delete(keyInfo(mid))
- conn.Close()
- if err == memcache.ErrNotFound {
- err = nil
- }
- return
- }
- // SetExpCache set user exp cache.
- func (d *Dao) SetExpCache(c context.Context, mid, exp int64) (err error) {
- conn := d.memberMemcache.Get(c)
- defer conn.Close()
- if err = conn.Set(&memcache.Item{
- Key: expKey(mid),
- Value: []byte(strconv.FormatInt(exp, 10)),
- Expiration: _expExpire,
- }); err != nil {
- log.Error("setexpcache mid %d err %v ", mid, err)
- }
- return
- }
- // DelExpCache set user exp cache.
- func (d *Dao) DelExpCache(c context.Context, mid int64) error {
- conn := d.memberMemcache.Get(c)
- defer conn.Close()
- if err := conn.Delete(expKey(mid)); err != nil {
- if err == memcache.ErrNotFound {
- return nil
- }
- return errors.WithStack(err)
- }
- return nil
- }
- // DeleteRealnameCache delete all realname cache
- func (d *Dao) DeleteRealnameCache(c context.Context, mid int64) (err error) {
- var (
- key1 = realnameInfoKey(mid)
- key2 = realnameApplyStatusKey(mid)
- conn = d.memberMemcache.Get(c)
- )
- defer conn.Close()
- if err = conn.Delete(key1); err != nil {
- if err != memcache.ErrNotFound {
- err = errors.Wrapf(err, "conn.Delete(%s)", key1)
- return
- }
- err = nil
- }
- if err = conn.Delete(key2); err != nil {
- if err == memcache.ErrNotFound {
- err = nil
- } else {
- err = errors.Wrapf(err, "conn.Delete(%s)", key2)
- }
- return
- }
- return
- }
|