cache.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package cache
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. . "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/encrypt"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. )
  10. const (
  11. threeDay = 172800
  12. oneDay = 86400
  13. AllSubPushCacheSize = 200
  14. )
  15. type SubPushList struct {
  16. Id string `json:"_id"`
  17. Title string `json:"title"`
  18. Area string `json:"area"`
  19. BuyerClass string `json:"buyerClass"`
  20. Subtype string `json:"subtype"`
  21. Industry string `json:"industry"`
  22. PublishTime int64 `json:"publishTime"`
  23. Ca_index int64 `json:"ca_index"`
  24. Ca_date int64 `json:"ca_date"`
  25. Ca_isvisit int `json:"ca_isvisit"`
  26. Ca_isvip int `json:"ca_isvip"`
  27. Ca_type int `json:"ca_type"`
  28. Ca_fileExists bool `json:"ca_fileExists"`
  29. MatchKeys []string `json:"matchKeys"`
  30. Budget interface{} `json:"budget"`
  31. BidAmount interface{} `json:"bidAmount"`
  32. Collection int `json:"collection"`
  33. Buyer string `json:"buyer"`
  34. ProjectName string `json:"projectName"`
  35. Winner string `json:"winner"`
  36. BidOpenTime int64 `json:"bidOpenTime"`
  37. Source int64 `json:"source"`
  38. }
  39. type SubPush struct {
  40. Date string
  41. Datas []*SubPushList
  42. Count int64
  43. }
  44. type PushCa struct {
  45. Date int64
  46. InfoId string
  47. Visit int
  48. Keys []string
  49. Type int
  50. Isvip int
  51. FileExists bool
  52. Source int64
  53. Info *map[string]interface{}
  54. }
  55. type Cache struct {
  56. Prefix string
  57. UserId string
  58. Ymd string
  59. }
  60. func NewEntnicheCache(userId, ymd string) *Cache {
  61. return &Cache{
  62. Prefix: "entnichepush",
  63. UserId: userId,
  64. Ymd: ymd,
  65. }
  66. }
  67. func NewSubscribeCache(userId, ymd string) *Cache {
  68. return &Cache{
  69. Prefix: "subpush",
  70. UserId: userId,
  71. Ymd: ymd,
  72. }
  73. }
  74. func NewMemberCache(userId, ymd string) *Cache {
  75. return &Cache{
  76. Prefix: "memberpush",
  77. UserId: userId,
  78. Ymd: ymd,
  79. }
  80. }
  81. //
  82. func NewSubPushList(pushCas []*PushCa) []*SubPushList {
  83. subPushList := []*SubPushList{}
  84. for _, p := range pushCas {
  85. info := p.Info
  86. area := ObjToString((*info)["area"])
  87. if area == "A" {
  88. area = "全国"
  89. }
  90. industry := ObjToString((*info)["s_subscopeclass"])
  91. scs := strings.Split(industry, ",")
  92. if len(scs) > 0 {
  93. industry = scs[0]
  94. if industry != "" {
  95. iss := strings.Split(industry, "_")
  96. if len(iss) > 0 {
  97. industry = iss[0]
  98. }
  99. }
  100. }
  101. infotype := ObjToString((*info)["subtype"])
  102. if infotype == "" {
  103. infotype = ObjToString((*info)["toptype"])
  104. }
  105. _id := p.InfoId
  106. if _id == "" {
  107. _id = ObjToString((*info)["_id"])
  108. }
  109. subPushList = append(subPushList, &SubPushList{
  110. Id: EncodeArticleId2ByCheck(_id),
  111. Title: ObjToString((*info)["title"]),
  112. Area: area,
  113. BuyerClass: ObjToString((*info)["buyerclass"]),
  114. Subtype: infotype,
  115. Industry: industry,
  116. PublishTime: Int64All((*info)["publishtime"]),
  117. Ca_date: p.Date,
  118. Ca_isvisit: p.Visit,
  119. Ca_isvip: p.Isvip,
  120. Ca_type: p.Type,
  121. Ca_fileExists: p.FileExists,
  122. MatchKeys: p.Keys,
  123. Budget: (*info)["budget"],
  124. BidAmount: (*info)["bidamount"],
  125. Buyer: ObjToString((*info)["buyer"]),
  126. ProjectName: ObjToString((*info)["projectname"]),
  127. Winner: ObjToString((*info)["s_winner"]),
  128. BidOpenTime: Int64All((*info)["bidopentime"]),
  129. Source: p.Source,
  130. })
  131. }
  132. return subPushList
  133. }
  134. //从pushcache_2_b中取
  135. func (c *Cache) GetTodayCache(userId string) (*SubPush, error) {
  136. pc_a, err := redis.GetNewBytes("pushcache_2_b", c.todayKey(userId))
  137. if err != nil {
  138. return nil, err
  139. }
  140. if pc_a == nil {
  141. return nil, nil
  142. }
  143. var p *SubPush
  144. if err := json.Unmarshal(*pc_a, &p); err != nil {
  145. return nil, err
  146. }
  147. return p, nil
  148. }
  149. //往pushcache_2_b中放
  150. func (c *Cache) PutTodayCache(userId string, pc_a *SubPush) {
  151. redis.Put("pushcache_2_b", c.todayKey(userId), pc_a, oneDay)
  152. }
  153. //获取redis key
  154. func (c *Cache) todayKey(userId string) string {
  155. return fmt.Sprintf("%s_%s", c.Prefix, userId)
  156. }
  157. func (c *Cache) allKey(userId string) string {
  158. return fmt.Sprintf("all_%s_%s", c.Prefix, userId)
  159. }
  160. //查看全部列表缓存
  161. func (c *Cache) PutAllCache(userId string, datas *SubPush) {
  162. redis.Put("pushcache_2_a", c.allKey(userId), datas, oneDay)
  163. }
  164. func (c *Cache) GetAllCache(userId string) (*SubPush, error) {
  165. return c.GetCache("pushcache_2_a", c.allKey(userId))
  166. }
  167. func (c *Cache) GetCache(code, key string) (*SubPush, error) {
  168. pc_a, err := redis.GetNewBytes(code, key)
  169. if err != nil {
  170. return nil, err
  171. }
  172. if pc_a == nil {
  173. return nil, nil
  174. }
  175. var p *SubPush
  176. if err := json.Unmarshal(*pc_a, &p); err != nil {
  177. return nil, err
  178. }
  179. return p, nil
  180. }
  181. func (c *Cache) DoCache(subPushList []*SubPushList) {
  182. subPush_datas := []*SubPushList{}
  183. for i := len(subPushList) - 1; i >= 0; i-- {
  184. subPush_datas = append(subPush_datas, subPushList[i])
  185. }
  186. //更新redis
  187. subPush, err := c.GetTodayCache(c.UserId)
  188. if err == nil && subPush != nil && len(subPush.Datas) > 0 {
  189. if subPush.Date != c.Ymd {
  190. subPush = &SubPush{
  191. Date: c.Ymd,
  192. Datas: subPush_datas,
  193. Count: int64(len(subPush_datas)),
  194. }
  195. } else {
  196. subPush.Datas = append(subPush_datas, subPush.Datas...)
  197. subPush.Count = subPush.Count + int64(len(subPush_datas))
  198. }
  199. c.PutTodayCache(c.UserId, subPush)
  200. }
  201. //全部列表缓存
  202. allCache, err := c.GetAllCache(c.UserId)
  203. if err == nil && allCache != nil && len(allCache.Datas) > 0 {
  204. allCache.Datas = append(subPush_datas, allCache.Datas...)
  205. if len(allCache.Datas) > AllSubPushCacheSize {
  206. allCache.Datas = allCache.Datas[:AllSubPushCacheSize]
  207. }
  208. allCache.Count = allCache.Count + int64(len(subPush_datas))
  209. c.PutAllCache(c.UserId, allCache)
  210. }
  211. }