manager.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package cache
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/gofiber/fiber/v2"
  6. "github.com/gofiber/fiber/v2/internal/memory"
  7. )
  8. // go:generate msgp
  9. // msgp -file="manager.go" -o="manager_msgp.go" -tests=false -unexported
  10. type item struct {
  11. body []byte
  12. ctype []byte
  13. cencoding []byte
  14. status int
  15. exp uint64
  16. headers map[string][]byte
  17. // used for finding the item in an indexed heap
  18. heapidx int
  19. }
  20. //msgp:ignore manager
  21. type manager struct {
  22. pool sync.Pool
  23. memory *memory.Storage
  24. storage fiber.Storage
  25. }
  26. func newManager(storage fiber.Storage) *manager {
  27. // Create new storage handler
  28. manager := &manager{
  29. pool: sync.Pool{
  30. New: func() interface{} {
  31. return new(item)
  32. },
  33. },
  34. }
  35. if storage != nil {
  36. // Use provided storage if provided
  37. manager.storage = storage
  38. } else {
  39. // Fallback to memory storage
  40. manager.memory = memory.New()
  41. }
  42. return manager
  43. }
  44. // acquire returns an *entry from the sync.Pool
  45. func (m *manager) acquire() *item {
  46. return m.pool.Get().(*item) //nolint:forcetypeassert // We store nothing else in the pool
  47. }
  48. // release and reset *entry to sync.Pool
  49. func (m *manager) release(e *item) {
  50. // don't release item if we using memory storage
  51. if m.storage != nil {
  52. return
  53. }
  54. e.body = nil
  55. e.ctype = nil
  56. e.status = 0
  57. e.exp = 0
  58. e.headers = nil
  59. m.pool.Put(e)
  60. }
  61. // get data from storage or memory
  62. func (m *manager) get(key string) *item {
  63. var it *item
  64. if m.storage != nil {
  65. it = m.acquire()
  66. raw, err := m.storage.Get(key)
  67. if err != nil {
  68. return it
  69. }
  70. if raw != nil {
  71. if _, err := it.UnmarshalMsg(raw); err != nil {
  72. return it
  73. }
  74. }
  75. return it
  76. }
  77. if it, _ = m.memory.Get(key).(*item); it == nil { //nolint:errcheck // We store nothing else in the pool
  78. it = m.acquire()
  79. return it
  80. }
  81. return it
  82. }
  83. // get raw data from storage or memory
  84. func (m *manager) getRaw(key string) []byte {
  85. var raw []byte
  86. if m.storage != nil {
  87. raw, _ = m.storage.Get(key) //nolint:errcheck // TODO: Handle error here
  88. } else {
  89. raw, _ = m.memory.Get(key).([]byte) //nolint:errcheck // TODO: Handle error here
  90. }
  91. return raw
  92. }
  93. // set data to storage or memory
  94. func (m *manager) set(key string, it *item, exp time.Duration) {
  95. if m.storage != nil {
  96. if raw, err := it.MarshalMsg(nil); err == nil {
  97. _ = m.storage.Set(key, raw, exp) //nolint:errcheck // TODO: Handle error here
  98. }
  99. // we can release data because it's serialized to database
  100. m.release(it)
  101. } else {
  102. m.memory.Set(key, it, exp)
  103. }
  104. }
  105. // set data to storage or memory
  106. func (m *manager) setRaw(key string, raw []byte, exp time.Duration) {
  107. if m.storage != nil {
  108. _ = m.storage.Set(key, raw, exp) //nolint:errcheck // TODO: Handle error here
  109. } else {
  110. m.memory.Set(key, raw, exp)
  111. }
  112. }
  113. // delete data from storage or memory
  114. func (m *manager) del(key string) {
  115. if m.storage != nil {
  116. _ = m.storage.Delete(key) //nolint:errcheck // TODO: Handle error here
  117. } else {
  118. m.memory.Delete(key)
  119. }
  120. }