memcache.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package fav
  2. import (
  3. "context"
  4. "fmt"
  5. favmdl "go-common/app/service/main/favorite/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "strconv"
  9. )
  10. const (
  11. _folder = "f_%d_%d" // key:f_{mid%100}_{fid},value:{*Fodler}.pb
  12. _relationFids = "rof_%d_%d_%d" // key:rof_{type}_{mid}_{oid},value:{[]int64}.pb
  13. _oidCount = "oc_%d_%d" // key:oc_{type}_{oid},value:int64
  14. _batchOids = "bo_%d_%d" // key:oc_{type}_{mid},value:{[]int64}.pb
  15. _recentOids = "rcto_%d_%d" // key:rcto_{type}_{mid},value:{[]int64}.pb
  16. _recentRes = "rctr_%d_%d" // key:rcto_{type}_{mid},value:{[]*Resource}.pb
  17. )
  18. // folderMcKey
  19. func folderMcKey(mid, fid int64) string {
  20. return fmt.Sprintf(_folder, mid%100, fid)
  21. }
  22. // relationFidsKey
  23. func relationFidsKey(typ int8, mid, oid int64) string {
  24. return fmt.Sprintf(_relationFids, typ, mid, oid)
  25. }
  26. func oidCountKey(typ int8, oid int64) string {
  27. return fmt.Sprintf(_oidCount, typ, oid)
  28. }
  29. func batchOidsKey(typ int8, mid int64) string {
  30. return fmt.Sprintf(_batchOids, typ, mid)
  31. }
  32. func recentOidsKey(typ int8, mid int64) string {
  33. return fmt.Sprintf(_recentOids, typ, mid)
  34. }
  35. func recentResKey(typ int8, mid int64) string {
  36. return fmt.Sprintf(_recentRes, typ, mid)
  37. }
  38. // pingMC ping mc is ok.
  39. func (d *Dao) pingMC(c context.Context) error {
  40. conn := d.mc.Get(c)
  41. defer conn.Close()
  42. item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}
  43. return conn.Set(&item)
  44. }
  45. // SetFoldersMc add folders mc cache.
  46. func (d *Dao) SetFoldersMc(c context.Context, vs ...*favmdl.Folder) (err error) {
  47. conn := d.mc.Get(c)
  48. defer conn.Close()
  49. for _, v := range vs {
  50. if v == nil {
  51. continue
  52. }
  53. item := &memcache.Item{Key: folderMcKey(v.Mid, v.ID), Object: v, Flags: memcache.FlagProtobuf, Expiration: d.mcExpire}
  54. if err = conn.Set(item); err != nil {
  55. log.Error("conn.Set(%s) error(%v)", folderMcKey(v.Mid, v.ID), err)
  56. return
  57. }
  58. }
  59. return
  60. }
  61. // FolderMc return one folder from mc.
  62. func (d *Dao) FolderMc(c context.Context, typ int8, mid, fid int64) (f *favmdl.Folder, err error) {
  63. var (
  64. key = folderMcKey(mid, fid)
  65. item *memcache.Item
  66. conn = d.mc.Get(c)
  67. )
  68. defer conn.Close()
  69. if item, err = conn.Get(key); err != nil {
  70. if err == memcache.ErrNotFound {
  71. err = nil
  72. } else {
  73. log.Error("memcache.Get(%s) error(%v)", key, err)
  74. }
  75. return
  76. }
  77. f = new(favmdl.Folder)
  78. if err = conn.Scan(item, f); err != nil {
  79. log.Error("conn.Scan(%s) error(%v)", item.Value, err)
  80. f = nil
  81. }
  82. return
  83. }
  84. // SetRelaitonFidsMc set fids binary data to mc.
  85. func (d *Dao) SetRelaitonFidsMc(c context.Context, typ int8, mid, oid int64, fids []int64) (err error) {
  86. key := relationFidsKey(typ, mid, oid)
  87. conn := d.mc.Get(c)
  88. defer conn.Close()
  89. bytes := favmdl.ToBytes(fids)
  90. item := &memcache.Item{Key: key, Value: bytes, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
  91. if err = conn.Set(item); err != nil {
  92. log.Error("conn.Set(%s) error(%v)", key, err)
  93. return
  94. }
  95. return
  96. }
  97. // RelaitonFidsMc return fids from mc.
  98. func (d *Dao) RelaitonFidsMc(c context.Context, typ int8, mid, oid int64) (fids []int64, err error) {
  99. var (
  100. key = relationFidsKey(typ, mid, oid)
  101. item *memcache.Item
  102. conn = d.mc.Get(c)
  103. b []byte
  104. )
  105. defer conn.Close()
  106. if item, err = conn.Get(key); err != nil {
  107. if err == memcache.ErrNotFound {
  108. err = nil
  109. } else {
  110. log.Error("memcache.Get(%s) error(%v)", key, err)
  111. }
  112. return
  113. }
  114. if err = conn.Scan(item, &b); err != nil {
  115. log.Error("conn.Scan(%s) error(%v)", item.Value, err)
  116. fids = nil
  117. return
  118. }
  119. if fids, err = favmdl.ToInt64s(b); err != nil {
  120. log.Error("fs.SetIndex(%v) error(%v)", b, err)
  121. err = nil
  122. fids = nil
  123. }
  124. return
  125. }
  126. // DelRelationFidsMc delete oid's fid mc cache.
  127. func (d *Dao) DelRelationFidsMc(c context.Context, typ int8, mid, oid int64) (err error) {
  128. var (
  129. key = relationFidsKey(typ, mid, oid)
  130. conn = d.mc.Get(c)
  131. )
  132. defer conn.Close()
  133. if err = conn.Delete(key); err != nil {
  134. if err == memcache.ErrNotFound {
  135. err = nil
  136. } else {
  137. log.Error("conn.Delete(%s) error(%v)", key, err)
  138. }
  139. }
  140. return
  141. }
  142. // SetOidCountMc return oid count from mc.
  143. func (d *Dao) SetOidCountMc(c context.Context, typ int8, oid, count int64) (err error) {
  144. var (
  145. key = oidCountKey(typ, oid)
  146. conn = d.mc.Get(c)
  147. )
  148. defer conn.Close()
  149. bs := []byte(strconv.FormatInt(int64(count), 10))
  150. item := &memcache.Item{Key: key, Value: bs, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
  151. if err = conn.Set(item); err != nil {
  152. log.Error("conn.Set(%s) error(%v)", key, err)
  153. return
  154. }
  155. return
  156. }
  157. // DelBatchOidsMc delete oids mc cache.
  158. func (d *Dao) DelBatchOidsMc(c context.Context, typ int8, mid int64) (err error) {
  159. key := batchOidsKey(typ, mid)
  160. conn := d.mc.Get(c)
  161. defer conn.Close()
  162. if err = conn.Delete(key); err != nil {
  163. if err == memcache.ErrNotFound {
  164. err = nil
  165. } else {
  166. log.Error("conn.Delete(%s) error(%v)", key, err)
  167. }
  168. }
  169. return
  170. }
  171. // DelRecentOidsMc delete recent oids mc cache.
  172. func (d *Dao) DelRecentOidsMc(c context.Context, typ int8, mid int64) (err error) {
  173. key := recentOidsKey(typ, mid)
  174. conn := d.mc.Get(c)
  175. defer conn.Close()
  176. if err = conn.Delete(key); err != nil {
  177. if err == memcache.ErrNotFound {
  178. err = nil
  179. } else {
  180. log.Error("conn.Delete(%s) error(%v)", key, err)
  181. }
  182. }
  183. return
  184. }
  185. // DelRecentResMc delete recent oids mc cache.
  186. func (d *Dao) DelRecentResMc(c context.Context, typ int8, mid int64) (err error) {
  187. key := recentResKey(typ, mid)
  188. conn := d.mc.Get(c)
  189. defer conn.Close()
  190. if err = conn.Delete(key); err != nil {
  191. if err == memcache.ErrNotFound {
  192. err = nil
  193. } else {
  194. log.Error("conn.Delete(%s) error(%v)", key, err)
  195. }
  196. }
  197. return
  198. }