mem_store.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package cache
  2. import (
  3. "f1-game/internal/extend/math"
  4. "strings"
  5. cstring "github.com/cherry-game/cherry/extend/string"
  6. cutils "github.com/cherry-game/cherry/extend/utils"
  7. )
  8. type item struct {
  9. ID int64
  10. Name string
  11. Name2 string
  12. }
  13. type MemStore struct {
  14. data []item
  15. }
  16. func NewMemStore() *MemStore {
  17. return &MemStore{
  18. data: make([]item, 0),
  19. }
  20. }
  21. // Add 添加新元素
  22. func (m *MemStore) Add(id int64, name string, name2 string) {
  23. // 检查是否已存在, 如果存在则不重复添加
  24. for _, item := range m.data {
  25. if item.ID == id {
  26. return
  27. }
  28. }
  29. m.data = append(m.data, item{ID: id, Name: name, Name2: name2})
  30. }
  31. // Update 更新Name数据
  32. func (m *MemStore) Update(id int64, newName string) bool {
  33. for i, item := range m.data {
  34. if item.ID == id {
  35. m.data[i].Name = newName
  36. return true
  37. }
  38. }
  39. return false
  40. }
  41. // Update 更新Name2数据
  42. func (m *MemStore) UpdateName2(id int64, newName2 string) bool {
  43. for i, item := range m.data {
  44. if item.ID == id {
  45. m.data[i].Name2 = newName2
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. // Query 查询方法
  52. func (m MemStore) Query(keyword string, pageNum, pageSize int32) (total int32, data []int64) {
  53. id := int64(0)
  54. if cutils.IsNumeric(keyword) {
  55. id, _ = cstring.ToInt64(keyword)
  56. }
  57. if pageSize == 0 {
  58. total = m.countFilteredItems(keyword, id)
  59. return total, nil
  60. }
  61. if pageNum < 1 {
  62. pageNum = 1
  63. }
  64. if pageSize < 1 {
  65. pageSize = 10
  66. }
  67. k := strings.ToLower(keyword)
  68. total = m.countFilteredItems(keyword, id)
  69. start := (pageNum - 1) * pageSize
  70. if start >= total {
  71. return total, nil
  72. }
  73. data = make([]int64, 0, math.Min(pageSize, total-start))
  74. currentIndex := int32(0)
  75. for _, item := range m.data {
  76. if k == "" || item.ID == id ||
  77. strings.Contains(strings.ToLower(item.Name), k) ||
  78. strings.Contains(strings.ToLower(item.Name2), k) {
  79. if currentIndex >= start && currentIndex < start+pageSize {
  80. data = append(data, item.ID)
  81. }
  82. currentIndex++
  83. if int32(len(data)) >= pageSize {
  84. break
  85. }
  86. }
  87. }
  88. return total, data
  89. }
  90. // 计算符合条件总条目数
  91. func (m MemStore) countFilteredItems(keyword string, id int64) int32 {
  92. if keyword == "" {
  93. return int32(len(m.data))
  94. }
  95. k := strings.ToLower(keyword)
  96. count := int32(0)
  97. for _, item := range m.data {
  98. if strings.Contains(strings.ToLower(item.Name), k) || strings.Contains(strings.ToLower(item.Name2), k) || item.ID == id {
  99. count++
  100. }
  101. }
  102. return count
  103. }
  104. // func main() {
  105. // ms := NewMemStore()
  106. // // 写入示例数据
  107. // start := time.Now()
  108. // for i := int64(1); i <= 1000000; i++ {
  109. // ms.Add(i, fmt.Sprintf("user-%07d", i))
  110. // }
  111. // fmt.Printf("写入完成,耗时 %v\n", time.Since(start))
  112. // // 查询测试
  113. // start = time.Now()
  114. // total, page := ms.Query("456", 1, 10)
  115. // fmt.Printf("查询耗时 %v,total=%d\n", time.Since(start), total)
  116. // for idx, v := range page {
  117. // fmt.Println(idx, "-", v.ID, v.Name)
  118. // }
  119. // // 测试只获取总数
  120. // start = time.Now()
  121. // total, _ = ms.Query("123", 1, 0)
  122. // fmt.Printf("仅计数耗时 %v,total=%d\n", time.Since(start), total)
  123. // }