memcache.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package archive
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/app-intl/model/view"
  6. "go-common/app/service/main/archive/api"
  7. "go-common/app/service/main/archive/model/archive"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _prefixRelate = "al_"
  14. _prefix = "a3p_"
  15. _prxfixSt = "stp_"
  16. _prefixViewStatic = "avp_"
  17. _prefixViewContribute = "avpc_"
  18. )
  19. // keyArc is.
  20. func keyArc(aid int64) string {
  21. return _prefix + strconv.FormatInt(aid, 10)
  22. }
  23. // keySt is.
  24. func keySt(aid int64) string {
  25. return _prxfixSt + strconv.FormatInt(aid, 10)
  26. }
  27. // keyView is.
  28. func keyView(aid int64) string {
  29. return _prefixViewStatic + strconv.FormatInt(aid, 10)
  30. }
  31. // keyRl is.
  32. func keyRl(aid int64) string {
  33. return _prefixRelate + strconv.FormatInt(aid, 10)
  34. }
  35. // keyViewContribute is.
  36. func keyViewContribute(mid int64) string {
  37. return _prefixViewContribute + strconv.FormatInt(mid, 10)
  38. }
  39. // pingMC is.
  40. func (d *Dao) pingMC(c context.Context) (err error) {
  41. conn := d.mc.Get(c)
  42. err = conn.Set(&memcache.Item{Key: "ping", Object: []byte{1}, Flags: memcache.FlagJSON, Expiration: 0})
  43. conn.Close()
  44. return
  45. }
  46. // statsCache get stat cache by aids
  47. func (d *Dao) statsCache(c context.Context, aids []int64) (cached map[int64]*api.Stat, missed []int64, err error) {
  48. cached = make(map[int64]*api.Stat, len(aids))
  49. var (
  50. conn = d.mc.Get(c)
  51. keys = make([]string, 0, len(aids))
  52. rs map[string]*memcache.Item
  53. )
  54. defer conn.Close()
  55. for _, aid := range aids {
  56. keys = append(keys, keySt(aid))
  57. }
  58. if rs, err = conn.GetMulti(keys); err != nil {
  59. err = errors.Wrapf(err, "%v", keys)
  60. return
  61. }
  62. for _, item := range rs {
  63. var st = &api.Stat{}
  64. if err = conn.Scan(item, st); err != nil {
  65. log.Error("conn.Scan(%s) error(%v)", item.Value, err)
  66. err = nil
  67. continue
  68. }
  69. cached[st.Aid] = st
  70. }
  71. if len(cached) == len(aids) {
  72. return
  73. }
  74. for _, aid := range aids {
  75. if _, ok := cached[aid]; !ok {
  76. missed = append(missed, aid)
  77. }
  78. }
  79. return
  80. }
  81. // arcsCache get archives cache.
  82. func (d *Dao) arcsCache(c context.Context, aids []int64) (cached map[int64]*api.Arc, missed []int64, err error) {
  83. var (
  84. keys = make([]string, 0, len(aids))
  85. conn = d.mc.Get(c)
  86. aidmap = make(map[string]int64, len(aids))
  87. rs map[string]*memcache.Item
  88. a *api.Arc
  89. )
  90. cached = make(map[int64]*api.Arc, len(aids))
  91. defer conn.Close()
  92. for _, aid := range aids {
  93. k := keyArc(aid)
  94. if _, ok := aidmap[k]; !ok {
  95. keys = append(keys, k)
  96. aidmap[k] = aid
  97. }
  98. }
  99. if rs, err = conn.GetMulti(keys); err != nil {
  100. err = errors.Wrapf(err, "%v", keys)
  101. return
  102. }
  103. for k, r := range rs {
  104. a = &api.Arc{}
  105. if err = conn.Scan(r, a); err != nil {
  106. log.Error("conn.Scan(%s) error(%v)", r.Value, err)
  107. err = nil
  108. continue
  109. }
  110. cached[aidmap[k]] = a
  111. // delete hit key
  112. delete(aidmap, k)
  113. }
  114. // missed key
  115. missed = make([]int64, 0, len(aidmap))
  116. for _, aid := range aidmap {
  117. missed = append(missed, aid)
  118. }
  119. return
  120. }
  121. // arcCache get archive cache.
  122. func (d *Dao) arcCache(c context.Context, aid int64) (a *api.Arc, err error) {
  123. conn := d.mc.Get(c)
  124. key := keyArc(aid)
  125. defer conn.Close()
  126. r, err := conn.Get(key)
  127. if err != nil {
  128. if err == memcache.ErrNotFound {
  129. err = nil
  130. return
  131. }
  132. err = errors.Wrapf(err, "conn.Get(%s)", key)
  133. return
  134. }
  135. a = &api.Arc{}
  136. if err = conn.Scan(r, a); err != nil {
  137. a = nil
  138. err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
  139. }
  140. return
  141. }
  142. // viewCache get view cache from remote memecache .
  143. func (d *Dao) viewCache(c context.Context, aid int64) (vs *archive.View3, err error) {
  144. conn := d.mc.Get(c)
  145. key := keyView(aid)
  146. defer conn.Close()
  147. r, err := conn.Get(key)
  148. if err != nil {
  149. if err == memcache.ErrNotFound {
  150. err = nil
  151. return
  152. }
  153. err = errors.Wrapf(err, "conn.Get(%s)", key)
  154. return
  155. }
  156. vs = &archive.View3{Archive3: &archive.Archive3{}}
  157. if err = conn.Scan(r, vs); err != nil {
  158. vs = nil
  159. err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
  160. }
  161. return
  162. }
  163. // statCache get a archive stat from cache.
  164. func (d *Dao) statCache(c context.Context, aid int64) (st *api.Stat, err error) {
  165. conn := d.mc.Get(c)
  166. key := keySt(aid)
  167. defer conn.Close()
  168. r, err := conn.Get(key)
  169. if err != nil {
  170. if err == memcache.ErrNotFound {
  171. err = nil
  172. return
  173. }
  174. err = errors.Wrapf(err, "conn.Get(%s)", key)
  175. return
  176. }
  177. st = &api.Stat{}
  178. if err = conn.Scan(r, st); err != nil {
  179. st = nil
  180. err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
  181. }
  182. return
  183. }
  184. // RelatesCache get relates.
  185. func (d *Dao) RelatesCache(c context.Context, aid int64) (rls []*view.Relate, err error) {
  186. conn := d.mc.Get(c)
  187. key := keyRl(aid)
  188. defer conn.Close()
  189. r, err := conn.Get(key)
  190. if err != nil {
  191. if err == memcache.ErrNotFound {
  192. err = nil
  193. return
  194. }
  195. err = errors.Wrapf(err, "conn.Get(%s)", key)
  196. return
  197. }
  198. if err = conn.Scan(r, &rls); err != nil {
  199. err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
  200. }
  201. return
  202. }
  203. // AddRelatesCache add relates
  204. func (d *Dao) AddRelatesCache(aid int64, rls []*view.Relate) {
  205. d.addCache(func() {
  206. d.addRelatesCache(context.Background(), aid, rls)
  207. })
  208. }
  209. // addRelatesCache add relates cache.
  210. func (d *Dao) addRelatesCache(c context.Context, aid int64, rls []*view.Relate) (err error) {
  211. conn := d.mc.Get(c)
  212. key := keyRl(aid)
  213. item := &memcache.Item{Key: key, Object: rls, Flags: memcache.FlagJSON, Expiration: d.expireRlt}
  214. if err = conn.Set(item); err != nil {
  215. err = errors.Wrapf(err, "conn.Set(%s,%v,%d)", key, rls, d.expireRlt)
  216. }
  217. conn.Close()
  218. return
  219. }
  220. // ViewContributeCache get archive cache.
  221. func (d *Dao) ViewContributeCache(c context.Context, mid int64) (aids []int64, err error) {
  222. conn := d.mc.Get(c)
  223. key := keyViewContribute(mid)
  224. defer conn.Close()
  225. r, err := conn.Get(key)
  226. if err != nil {
  227. if err == memcache.ErrNotFound {
  228. err = nil
  229. return
  230. }
  231. err = errors.Wrapf(err, "conn.Get(%s)", key)
  232. return
  233. }
  234. if err = conn.Scan(r, &aids); err != nil {
  235. err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
  236. }
  237. return
  238. }