index_order.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/interface/main/space/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _indexOrderKeyFmt = "spc_io_%d"
  12. _indexOrderSQL = `SELECT index_order FROM dede_member_up_settings%d WHERE mid = ?`
  13. _indexOrderAddSQL = `INSERT INTO dede_member_up_settings%d (mid,index_order) VALUES (?,?) ON DUPLICATE KEY UPDATE index_order = ?`
  14. )
  15. func indexOrderHit(mid int64) int64 {
  16. return mid % 10
  17. }
  18. func indexOrderKey(mid int64) string {
  19. return fmt.Sprintf(_indexOrderKeyFmt, mid)
  20. }
  21. // IndexOrder get index order info.
  22. func (d *Dao) IndexOrder(c context.Context, mid int64) (indexOrder string, err error) {
  23. var row = d.db.QueryRow(c, fmt.Sprintf(_indexOrderSQL, indexOrderHit(mid)), mid)
  24. if err = row.Scan(&indexOrder); err != nil {
  25. if err == sql.ErrNoRows {
  26. err = nil
  27. } else {
  28. log.Error("IndexOrder row.Scan() error(%v)", err)
  29. }
  30. }
  31. return
  32. }
  33. // IndexOrderModify index order modify.
  34. func (d *Dao) IndexOrderModify(c context.Context, mid int64, orderStr string) (err error) {
  35. if _, err = d.db.Exec(c, fmt.Sprintf(_indexOrderAddSQL, indexOrderHit(mid)), mid, orderStr, orderStr); err != nil {
  36. log.Error("IndexOrderModify error d.db.Exec(%d,%s) error(%v)", mid, orderStr, err)
  37. }
  38. return
  39. }
  40. // IndexOrderCache get index order cache.
  41. func (d *Dao) IndexOrderCache(c context.Context, mid int64) (data []*model.IndexOrder, err error) {
  42. var (
  43. conn = d.mc.Get(c)
  44. key = indexOrderKey(mid)
  45. )
  46. defer conn.Close()
  47. reply, err := conn.Get(key)
  48. if err != nil {
  49. if err == memcache.ErrNotFound {
  50. err = nil
  51. return
  52. }
  53. log.Error("IndexOrderCache conn.Get(%v) error(%v)", key, err)
  54. return
  55. }
  56. if err = conn.Scan(reply, &data); err != nil {
  57. log.Error("IndexOrderCache reply.Scan(%s) error(%v)", reply.Value, err)
  58. }
  59. return
  60. }
  61. // SetIndexOrderCache set index order cache.
  62. func (d *Dao) SetIndexOrderCache(c context.Context, mid int64, data []*model.IndexOrder) (err error) {
  63. conn := d.mc.Get(c)
  64. defer conn.Close()
  65. item := &memcache.Item{Key: indexOrderKey(mid), Object: data, Flags: memcache.FlagJSON, Expiration: d.mcSettingExpire}
  66. if err = conn.Set(item); err != nil {
  67. log.Error("SetIndexOrderCache conn.Set(%s) error(%v)", indexOrderKey(mid), err)
  68. }
  69. return
  70. }
  71. // DelIndexOrderCache delete index order cache.
  72. func (d *Dao) DelIndexOrderCache(c context.Context, mid int64) (err error) {
  73. conn := d.mc.Get(c)
  74. defer conn.Close()
  75. key := indexOrderKey(mid)
  76. if err = conn.Delete(key); err != nil {
  77. if err == memcache.ErrNotFound {
  78. err = nil
  79. return
  80. }
  81. log.Error("DelIndexOrderCache conn.Delete(%s) error(%v)", indexOrderKey(mid), err)
  82. }
  83. return
  84. }