mc.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "github.com/pkg/errors"
  6. v1 "go-common/app/service/main/account/api"
  7. mc "go-common/library/cache/memcache"
  8. )
  9. const (
  10. _prefixInfo = "i3_"
  11. _prefixCard = "c3_"
  12. _prefixVip = "v3_"
  13. _prefixProfile = "p3_"
  14. )
  15. func keyInfo(mid int64) string {
  16. return _prefixInfo + strconv.FormatInt(mid, 10)
  17. }
  18. func keyCard(mid int64) string {
  19. return _prefixCard + strconv.FormatInt(mid, 10)
  20. }
  21. func keyVip(mid int64) string {
  22. return _prefixVip + strconv.FormatInt(mid, 10)
  23. }
  24. func keyProfile(mid int64) string {
  25. return _prefixProfile + strconv.FormatInt(mid, 10)
  26. }
  27. // CacheInfo get account info from cache.
  28. func (d *Dao) CacheInfo(c context.Context, mid int64) (v *v1.Info, err error) {
  29. key := keyInfo(mid)
  30. conn := d.mc.Get(c)
  31. defer conn.Close()
  32. r, err := conn.Get(key)
  33. if err != nil {
  34. if err == mc.ErrNotFound {
  35. err = nil
  36. return
  37. }
  38. err = errors.Wrap(err, "dao cache info")
  39. return
  40. }
  41. v = &v1.Info{}
  42. if err = conn.Scan(r, v); err != nil {
  43. err = errors.Wrap(err, "dao cache scan info")
  44. }
  45. return
  46. }
  47. // AddCacheInfo set account info into cache.
  48. func (d *Dao) AddCacheInfo(c context.Context, mid int64, v *v1.Info) (err error) {
  49. item := &mc.Item{
  50. Key: keyInfo(mid),
  51. Object: v,
  52. Flags: mc.FlagProtobuf,
  53. Expiration: d.mcExpire,
  54. }
  55. conn := d.mc.Get(c)
  56. err = conn.Set(item)
  57. conn.Close()
  58. if err != nil {
  59. err = errors.Wrap(err, "dao add info cache")
  60. }
  61. return
  62. }
  63. // CacheInfos multi get account info from cache.
  64. func (d *Dao) CacheInfos(c context.Context, mids []int64) (res map[int64]*v1.Info, err error) {
  65. keys := make([]string, 0, len(mids))
  66. keyMidMap := make(map[string]int64, len(mids))
  67. for _, mid := range mids {
  68. key := keyInfo(mid)
  69. if _, ok := keyMidMap[key]; !ok {
  70. // duplicate mid
  71. keyMidMap[key] = mid
  72. keys = append(keys, key)
  73. }
  74. }
  75. conn := d.mc.Get(c)
  76. defer conn.Close()
  77. rs, err := conn.GetMulti(keys)
  78. if err != nil {
  79. if err == mc.ErrNotFound {
  80. err = nil
  81. return
  82. }
  83. err = errors.Wrap(err, "dao infos cache")
  84. return
  85. }
  86. res = make(map[int64]*v1.Info, len(mids))
  87. for _, r := range rs {
  88. ai := &v1.Info{}
  89. conn.Scan(r, ai)
  90. res[ai.Mid] = ai
  91. }
  92. return
  93. }
  94. // AddCacheInfos set account infos cache.
  95. func (d *Dao) AddCacheInfos(c context.Context, im map[int64]*v1.Info) (err error) {
  96. conn := d.mc.Get(c)
  97. defer conn.Close()
  98. for _, i := range im {
  99. item := &mc.Item{
  100. Key: keyInfo(i.Mid),
  101. Object: i,
  102. Flags: mc.FlagProtobuf,
  103. Expiration: d.mcExpire,
  104. }
  105. err = conn.Set(item)
  106. if err != nil {
  107. err = errors.Wrap(err, "dao add infos cache")
  108. }
  109. }
  110. return
  111. }
  112. // CacheCard get account card from cache.
  113. func (d *Dao) CacheCard(c context.Context, mid int64) (v *v1.Card, err error) {
  114. key := keyCard(mid)
  115. conn := d.mc.Get(c)
  116. defer conn.Close()
  117. r, err := conn.Get(key)
  118. if err != nil {
  119. if err == mc.ErrNotFound {
  120. err = nil
  121. return
  122. }
  123. err = errors.Wrap(err, "dao cache card")
  124. return
  125. }
  126. v = &v1.Card{}
  127. if err = conn.Scan(r, v); err != nil {
  128. err = errors.Wrap(err, "dao cache scan card")
  129. }
  130. return
  131. }
  132. // AddCacheCard set account card into cache.
  133. func (d *Dao) AddCacheCard(c context.Context, mid int64, v *v1.Card) (err error) {
  134. item := &mc.Item{
  135. Key: keyCard(mid),
  136. Object: v,
  137. Flags: mc.FlagProtobuf,
  138. Expiration: d.mcExpire,
  139. }
  140. conn := d.mc.Get(c)
  141. err = conn.Set(item)
  142. conn.Close()
  143. if err != nil {
  144. err = errors.Wrap(err, "dao add card cache")
  145. }
  146. return
  147. }
  148. // CacheCards multi get account cards from cache.
  149. func (d *Dao) CacheCards(c context.Context, mids []int64) (res map[int64]*v1.Card, err error) {
  150. keys := make([]string, 0, len(mids))
  151. keyMidMap := make(map[string]int64, len(mids))
  152. for _, mid := range mids {
  153. key := keyCard(mid)
  154. if _, ok := keyMidMap[key]; !ok {
  155. // duplicate mid
  156. keyMidMap[key] = mid
  157. keys = append(keys, key)
  158. }
  159. }
  160. conn := d.mc.Get(c)
  161. defer conn.Close()
  162. rs, err := conn.GetMulti(keys)
  163. if err != nil {
  164. if err == mc.ErrNotFound {
  165. err = nil
  166. return
  167. }
  168. err = errors.Wrap(err, "dao cards cache")
  169. return
  170. }
  171. res = make(map[int64]*v1.Card, len(mids))
  172. for _, r := range rs {
  173. ai := &v1.Card{}
  174. conn.Scan(r, ai)
  175. res[ai.Mid] = ai
  176. }
  177. return
  178. }
  179. // AddCacheCards set account cards cache.
  180. func (d *Dao) AddCacheCards(c context.Context, cm map[int64]*v1.Card) (err error) {
  181. conn := d.mc.Get(c)
  182. defer conn.Close()
  183. for _, card := range cm {
  184. item := &mc.Item{
  185. Key: keyCard(card.Mid),
  186. Object: card,
  187. Flags: mc.FlagProtobuf,
  188. Expiration: d.mcExpire,
  189. }
  190. err = conn.Set(item)
  191. if err != nil {
  192. err = errors.Wrap(err, "dao add cards cache")
  193. }
  194. }
  195. return
  196. }
  197. // CacheVip get vip cache.
  198. func (d *Dao) CacheVip(c context.Context, mid int64) (v *v1.VipInfo, err error) {
  199. key := keyVip(mid)
  200. conn := d.mc.Get(c)
  201. defer conn.Close()
  202. r, err := conn.Get(key)
  203. if err != nil {
  204. if err == mc.ErrNotFound {
  205. err = nil
  206. return
  207. }
  208. err = errors.Wrap(err, "dao vip cache")
  209. return
  210. }
  211. v = new(v1.VipInfo)
  212. if err = conn.Scan(r, v); err != nil {
  213. err = errors.Wrap(err, "dao vip cache scan")
  214. }
  215. return
  216. }
  217. // AddCacheVip set vip cache.
  218. func (d *Dao) AddCacheVip(c context.Context, mid int64, v *v1.VipInfo) (err error) {
  219. conn := d.mc.Get(c)
  220. conn.Set(&mc.Item{
  221. Key: keyVip(mid),
  222. Object: v,
  223. Flags: mc.FlagProtobuf,
  224. Expiration: d.mcExpire,
  225. })
  226. conn.Close()
  227. if err != nil {
  228. err = errors.Wrap(err, "dao vip add cache")
  229. }
  230. return
  231. }
  232. // CacheVips multi get account cards from cache.
  233. func (d *Dao) CacheVips(c context.Context, mids []int64) (res map[int64]*v1.VipInfo, err error) {
  234. keys := make([]string, 0, len(mids))
  235. keyMidMap := make(map[string]int64, len(mids))
  236. for _, mid := range mids {
  237. key := keyVip(mid)
  238. if _, ok := keyMidMap[key]; !ok {
  239. // duplicate mid
  240. keyMidMap[key] = mid
  241. keys = append(keys, key)
  242. }
  243. }
  244. conn := d.mc.Get(c)
  245. defer conn.Close()
  246. rs, err := conn.GetMulti(keys)
  247. if err != nil {
  248. if err == mc.ErrNotFound {
  249. err = nil
  250. return
  251. }
  252. err = errors.Wrap(err, "dao vips cache")
  253. return
  254. }
  255. res = make(map[int64]*v1.VipInfo, len(mids))
  256. for _, r := range rs {
  257. ai := &v1.VipInfo{}
  258. conn.Scan(r, ai)
  259. res[keyMidMap[r.Key]] = ai
  260. }
  261. return
  262. }
  263. // AddCacheVips set account vips cache.
  264. func (d *Dao) AddCacheVips(c context.Context, vm map[int64]*v1.VipInfo) (err error) {
  265. conn := d.mc.Get(c)
  266. defer conn.Close()
  267. for mid, v := range vm {
  268. item := &mc.Item{
  269. Key: keyVip(mid),
  270. Object: v,
  271. Flags: mc.FlagProtobuf,
  272. Expiration: d.mcExpire,
  273. }
  274. err = conn.Set(item)
  275. if err != nil {
  276. err = errors.Wrap(err, "dao add vips cache")
  277. }
  278. }
  279. return
  280. }
  281. // CacheProfile get profile cache.
  282. func (d *Dao) CacheProfile(c context.Context, mid int64) (v *v1.Profile, err error) {
  283. key := keyProfile(mid)
  284. conn := d.mc.Get(c)
  285. defer conn.Close()
  286. r, err := conn.Get(key)
  287. if err != nil {
  288. if err == mc.ErrNotFound {
  289. err = nil
  290. return
  291. }
  292. err = errors.Wrap(err, "dao profile cache")
  293. return
  294. }
  295. v = new(v1.Profile)
  296. if err = conn.Scan(r, v); err != nil {
  297. err = errors.Wrap(err, "dao profile cache scan")
  298. }
  299. return
  300. }
  301. // AddCacheProfile set profile cache.
  302. func (d *Dao) AddCacheProfile(c context.Context, mid int64, v *v1.Profile) (err error) {
  303. conn := d.mc.Get(c)
  304. conn.Set(&mc.Item{
  305. Key: keyProfile(mid),
  306. Object: v,
  307. Flags: mc.FlagProtobuf,
  308. Expiration: d.mcExpire,
  309. })
  310. conn.Close()
  311. if err != nil {
  312. err = errors.Wrap(err, "dao profile add cache")
  313. }
  314. return
  315. }
  316. // DelCache delete cache.
  317. func (d *Dao) DelCache(c context.Context, mid int64) []error {
  318. conn := d.mc.Get(c)
  319. errs := make([]error, 0, 5)
  320. if err := conn.Delete(keyInfo(mid)); err != nil {
  321. errs = append(errs, errors.Wrap(err, keyInfo(mid)))
  322. }
  323. if err := conn.Delete(keyCard(mid)); err != nil {
  324. errs = append(errs, errors.Wrap(err, keyCard(mid)))
  325. }
  326. if err := conn.Delete(keyVip(mid)); err != nil {
  327. errs = append(errs, errors.Wrap(err, keyVip(mid)))
  328. }
  329. if err := conn.Delete(keyProfile(mid)); err != nil {
  330. errs = append(errs, errors.Wrap(err, keyProfile(mid)))
  331. }
  332. if err := conn.Close(); err != nil {
  333. errs = append(errs, errors.Wrap(err, "conn close"))
  334. }
  335. d.cache.Do(c, func(ctx context.Context) {
  336. d.Info(ctx, mid)
  337. d.Card(ctx, mid)
  338. d.Vip(ctx, mid)
  339. d.Profile(ctx, mid)
  340. })
  341. return errs
  342. }