mc_subtitle.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/dm2/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _fmtSubtitle = "s_subtitle_%d_%d"
  11. _fmtVideoSubtitle = "s_video_%d_%d"
  12. _fmtSubtitleDraft = "s_draft_%v_%v_%v_%v"
  13. _fmtSubtitleSubject = "s_subtitle_allow_%d"
  14. _fmtSubtitleReportTag = "s_subtitle_report_%d_%d" // s_subtitle_report_bid_rid
  15. )
  16. func (d *Dao) subtitleKey(oid int64, subtitleID int64) string {
  17. return fmt.Sprintf(_fmtSubtitle, oid, subtitleID)
  18. }
  19. func (d *Dao) subtitleVideoKey(oid int64, tp int32) string {
  20. return fmt.Sprintf(_fmtVideoSubtitle, oid, tp)
  21. }
  22. func (d *Dao) subtitleDraftKey(oid int64, tp int32, mid int64, lan uint8) string {
  23. return fmt.Sprintf(_fmtSubtitleDraft, oid, tp, mid, lan)
  24. }
  25. func (d *Dao) subtitleSubjectKey(aid int64) string {
  26. return fmt.Sprintf(_fmtSubtitleSubject, aid)
  27. }
  28. func (d *Dao) subtitleReportTagKey(bid, rid int64) string {
  29. return fmt.Sprintf(_fmtSubtitleReportTag, bid, rid)
  30. }
  31. // SetVideoSubtitleCache .
  32. func (d *Dao) SetVideoSubtitleCache(c context.Context, oid int64, tp int32, res *model.VideoSubtitleCache) (err error) {
  33. var (
  34. item *memcache.Item
  35. conn = d.subtitleMc.Get(c)
  36. key = d.subtitleVideoKey(oid, tp)
  37. )
  38. defer conn.Close()
  39. item = &memcache.Item{
  40. Key: key,
  41. Object: res,
  42. Flags: memcache.FlagJSON | memcache.FlagGzip,
  43. Expiration: d.subtitleMcExpire,
  44. }
  45. if err = conn.Set(item); err != nil {
  46. log.Error("conn.Set(%v) error(%v)", item, err)
  47. }
  48. return
  49. }
  50. // VideoSubtitleCache .
  51. func (d *Dao) VideoSubtitleCache(c context.Context, oid int64, tp int32) (res *model.VideoSubtitleCache, err error) {
  52. var (
  53. item *memcache.Item
  54. conn = d.subtitleMc.Get(c)
  55. key = d.subtitleVideoKey(oid, tp)
  56. )
  57. defer conn.Close()
  58. if item, err = conn.Get(key); err != nil {
  59. if err == memcache.ErrNotFound {
  60. err = nil
  61. res = nil
  62. return
  63. }
  64. log.Error("memcache.Get(%s) error(%v)", key, err)
  65. return
  66. }
  67. if err = conn.Scan(item, &res); err != nil {
  68. log.Error("mc.Scan() error(%v)", err)
  69. }
  70. return
  71. }
  72. // DelVideoSubtitleCache .
  73. func (d *Dao) DelVideoSubtitleCache(c context.Context, oid int64, tp int32) (err error) {
  74. var (
  75. key = d.subtitleVideoKey(oid, tp)
  76. conn = d.subtitleMc.Get(c)
  77. )
  78. defer conn.Close()
  79. if err = conn.Delete(key); err != nil {
  80. if err == memcache.ErrNotFound {
  81. err = nil
  82. } else {
  83. log.Error("memcache.Delete(%s) error(%v)", key, err)
  84. }
  85. }
  86. return
  87. }
  88. // SubtitleDraftCache .
  89. func (d *Dao) SubtitleDraftCache(c context.Context, oid int64, tp int32, mid int64, lan uint8) (subtitle *model.Subtitle, err error) {
  90. var (
  91. item *memcache.Item
  92. conn = d.subtitleMc.Get(c)
  93. key = d.subtitleDraftKey(oid, tp, mid, lan)
  94. )
  95. defer conn.Close()
  96. if item, err = conn.Get(key); err != nil {
  97. if err == memcache.ErrNotFound {
  98. err = nil
  99. } else {
  100. log.Error("memcache.Get(%s) error(%v)", key, err)
  101. }
  102. return
  103. }
  104. if err = conn.Scan(item, &subtitle); err != nil {
  105. log.Error("mc.Scan() error(%v)", err)
  106. }
  107. return
  108. }
  109. // SetSubtitleDraftCache .
  110. func (d *Dao) SetSubtitleDraftCache(c context.Context, subtitle *model.Subtitle) (err error) {
  111. var (
  112. item *memcache.Item
  113. conn = d.subtitleMc.Get(c)
  114. key = d.subtitleDraftKey(subtitle.Oid, subtitle.Type, subtitle.Mid, subtitle.Lan)
  115. )
  116. defer conn.Close()
  117. item = &memcache.Item{
  118. Key: key,
  119. Object: subtitle,
  120. Flags: memcache.FlagJSON | memcache.FlagGzip,
  121. Expiration: d.subtitleMcExpire,
  122. }
  123. if err = conn.Set(item); err != nil {
  124. log.Error("conn.Set(%v) error(%v)", item, err)
  125. }
  126. return
  127. }
  128. // DelSubtitleDraftCache .
  129. func (d *Dao) DelSubtitleDraftCache(c context.Context, oid int64, tp int32, mid int64, lan uint8) (err error) {
  130. var (
  131. key = d.subtitleDraftKey(oid, tp, mid, lan)
  132. conn = d.subtitleMc.Get(c)
  133. )
  134. defer conn.Close()
  135. if err = conn.Delete(key); err != nil {
  136. if err == memcache.ErrNotFound {
  137. err = nil
  138. } else {
  139. log.Error("memcache.Delete(%s) error(%v)", key, err)
  140. }
  141. }
  142. return
  143. }
  144. // SubtitlesCache .
  145. func (d *Dao) SubtitlesCache(c context.Context, oid int64, subtitleIds []int64) (res map[int64]*model.Subtitle, missed []int64, err error) {
  146. var (
  147. conn = d.subtitleMc.Get(c)
  148. keys []string
  149. subtitleIDMap = make(map[string]int64)
  150. )
  151. res = make(map[int64]*model.Subtitle)
  152. defer conn.Close()
  153. for _, subtitleID := range subtitleIds {
  154. k := d.subtitleKey(oid, subtitleID)
  155. if _, ok := subtitleIDMap[k]; !ok {
  156. keys = append(keys, k)
  157. subtitleIDMap[k] = subtitleID
  158. }
  159. }
  160. rs, err := conn.GetMulti(keys)
  161. if err != nil {
  162. log.Error("conn.GetMulti(%v) error(%v)", keys, err)
  163. return
  164. }
  165. for k, r := range rs {
  166. st := &model.Subtitle{}
  167. if err = conn.Scan(r, st); err != nil {
  168. log.Error("conn.Scan(%s) error(%v)", r.Value, err)
  169. err = nil
  170. continue
  171. }
  172. res[subtitleIDMap[k]] = st
  173. // delete hit key
  174. delete(subtitleIDMap, k)
  175. }
  176. // missed key
  177. missed = make([]int64, 0, len(subtitleIDMap))
  178. for _, subtitleID := range subtitleIDMap {
  179. missed = append(missed, subtitleID)
  180. }
  181. return
  182. }
  183. // SubtitleCache .
  184. func (d *Dao) SubtitleCache(c context.Context, oid int64, subtitleID int64) (subtitle *model.Subtitle, err error) {
  185. var (
  186. item *memcache.Item
  187. conn = d.subtitleMc.Get(c)
  188. key = d.subtitleKey(oid, subtitleID)
  189. )
  190. defer conn.Close()
  191. if item, err = conn.Get(key); err != nil {
  192. if err == memcache.ErrNotFound {
  193. err = nil
  194. } else {
  195. log.Error("memcache.Get(%s) error(%v)", key, err)
  196. }
  197. return
  198. }
  199. if err = conn.Scan(item, &subtitle); err != nil {
  200. log.Error("mc.Scan() error(%v)", err)
  201. }
  202. return
  203. }
  204. // SetSubtitleCache .
  205. func (d *Dao) SetSubtitleCache(c context.Context, subtitle *model.Subtitle) (err error) {
  206. var (
  207. item *memcache.Item
  208. conn = d.subtitleMc.Get(c)
  209. key = d.subtitleKey(subtitle.Oid, subtitle.ID)
  210. )
  211. defer conn.Close()
  212. item = &memcache.Item{
  213. Key: key,
  214. Object: subtitle,
  215. Flags: memcache.FlagJSON | memcache.FlagGzip,
  216. Expiration: d.subtitleMcExpire,
  217. }
  218. if err = conn.Set(item); err != nil {
  219. log.Error("conn.Set(%v) error(%v)", item, err)
  220. }
  221. return
  222. }
  223. // DelSubtitleCache .
  224. func (d *Dao) DelSubtitleCache(c context.Context, oid int64, subtitleID int64) (err error) {
  225. var (
  226. key = d.subtitleKey(oid, subtitleID)
  227. conn = d.subtitleMc.Get(c)
  228. )
  229. defer conn.Close()
  230. if err = conn.Delete(key); err != nil {
  231. if err == memcache.ErrNotFound {
  232. err = nil
  233. } else {
  234. log.Error("memcache.Delete(%s) error(%v)", key, err)
  235. }
  236. }
  237. return
  238. }
  239. // SetSubtitleSubjectCache .
  240. func (d *Dao) SetSubtitleSubjectCache(c context.Context, subtitleSubject *model.SubtitleSubject) (err error) {
  241. var (
  242. key = d.subtitleSubjectKey(subtitleSubject.Aid)
  243. conn = d.subtitleMc.Get(c)
  244. item *memcache.Item
  245. )
  246. defer conn.Close()
  247. item = &memcache.Item{
  248. Key: key,
  249. Object: subtitleSubject,
  250. Flags: memcache.FlagJSON | memcache.FlagGzip,
  251. Expiration: d.subtitleMcExpire,
  252. }
  253. if err = conn.Set(item); err != nil {
  254. log.Error("conn.Set(%v) error(%v)", item, err)
  255. }
  256. return
  257. }
  258. // SubtitleSubjectCache .
  259. func (d *Dao) SubtitleSubjectCache(c context.Context, aid int64) (subtitleSubject *model.SubtitleSubject, err error) {
  260. var (
  261. item *memcache.Item
  262. conn = d.subtitleMc.Get(c)
  263. key = d.subtitleSubjectKey(aid)
  264. )
  265. defer conn.Close()
  266. if item, err = conn.Get(key); err != nil {
  267. if err == memcache.ErrNotFound {
  268. err = nil
  269. } else {
  270. log.Error("memcache.Get(%s) error(%v)", key, err)
  271. }
  272. return
  273. }
  274. if err = conn.Scan(item, &subtitleSubject); err != nil {
  275. log.Error("mc.Scan() error(%v)", err)
  276. }
  277. return
  278. }
  279. // DelSubtitleSubjectCache .
  280. func (d *Dao) DelSubtitleSubjectCache(c context.Context, aid int64) (err error) {
  281. var (
  282. key = d.subtitleSubjectKey(aid)
  283. conn = d.subtitleMc.Get(c)
  284. )
  285. defer conn.Close()
  286. if err = conn.Delete(key); err != nil {
  287. if err == memcache.ErrNotFound {
  288. err = nil
  289. } else {
  290. log.Error("memcache.Delete(%s) error(%v)", key, err)
  291. }
  292. }
  293. return
  294. }
  295. // SubtitleWorlFlowTagCache .
  296. func (d *Dao) SubtitleWorlFlowTagCache(c context.Context, bid, rid int64) (data []*model.WorkFlowTag, err error) {
  297. var (
  298. item *memcache.Item
  299. key = d.subtitleReportTagKey(bid, rid)
  300. conn = d.subtitleMc.Get(c)
  301. )
  302. defer conn.Close()
  303. if item, err = conn.Get(key); err != nil {
  304. if err == memcache.ErrNotFound {
  305. err = nil
  306. } else {
  307. log.Error("memcache.Get(%s) error(%v)", key, err)
  308. }
  309. return
  310. }
  311. if err = conn.Scan(item, &data); err != nil {
  312. log.Error("mc.Scan() error(%v)", err)
  313. }
  314. return
  315. }
  316. // SetSubtitleWorlFlowTagCache .
  317. func (d *Dao) SetSubtitleWorlFlowTagCache(c context.Context, bid, rid int64, data []*model.WorkFlowTag) (err error) {
  318. var (
  319. key = d.subtitleReportTagKey(bid, rid)
  320. conn = d.subtitleMc.Get(c)
  321. item *memcache.Item
  322. )
  323. defer conn.Close()
  324. if len(data) == 0 {
  325. return
  326. }
  327. item = &memcache.Item{
  328. Key: key,
  329. Object: data,
  330. Flags: memcache.FlagJSON | memcache.FlagGzip,
  331. Expiration: d.subtitleMcExpire,
  332. }
  333. if err = conn.Set(item); err != nil {
  334. log.Error("conn.Set(%v) error(%v)", item, err)
  335. }
  336. return
  337. }