redis.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package helpers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "github.com/redis/go-redis/v9"
  9. )
  10. type DocAlreadyExists struct {
  11. Key string
  12. Value string
  13. }
  14. func (d *DocAlreadyExists) Error() string {
  15. return fmt.Sprintf("Key: '%s' already exists with value: '%s'", d.Key, d.Value)
  16. }
  17. type DocDoesntExist struct {
  18. Key string
  19. }
  20. func (d *DocDoesntExist) Error() string {
  21. return fmt.Sprintf("Document with ID: '%s' does not exist.", d.Key)
  22. }
  23. type RedisConf struct {
  24. Addr string
  25. Port string
  26. }
  27. type RedisCaller struct {
  28. ctx context.Context
  29. Client *redis.Client
  30. }
  31. /*
  32. Creates a new RedisCaller struct
  33. :param redisCfg: a redis configuration struct
  34. */
  35. func NewRedisClient(redisCfg RedisConf) *RedisCaller {
  36. return &RedisCaller{Client: redis.NewClient(&redis.Options{
  37. Addr: fmt.Sprintf("%s:%v", redisCfg.Addr, redisCfg.Port),
  38. DB: 0, // use default DB
  39. }),
  40. ctx: context.Background(),}
  41. }
  42. /*
  43. retrieves all of the document IDs in the Redis database
  44. */
  45. func (r *RedisCaller) AllDocIds() ([]string, error) {
  46. return r.Client.Keys(r.ctx, "*").Result()
  47. }
  48. /*
  49. Sets the item (id) to the value supplied in value
  50. :param doc: the documents.Document struct to input to the database
  51. */
  52. func (r *RedisCaller) AddDoc(doc Document) error {
  53. val, err := r.Client.Get(r.ctx, doc.Ident).Result()
  54. if err == redis.Nil {
  55. data, err := json.Marshal(&doc)
  56. if err != nil {
  57. return err
  58. }
  59. err = r.Client.Set(r.ctx, doc.Ident, data, 0).Err()
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. } else if err != nil {
  65. return err
  66. }
  67. return &DocAlreadyExists{Key: doc.Ident, Value: val}
  68. }
  69. /*
  70. Gets the item stored at the key (id)
  71. :param id: the id of the object to get
  72. */
  73. func (r *RedisCaller) GetItem(id string) (*Document, error) {
  74. var doc Document
  75. val, err := r.Client.Get(r.ctx, id).Result()
  76. if err == redis.Nil {
  77. return nil, err
  78. } else if err != nil {
  79. return nil, err
  80. }
  81. data := []byte(val)
  82. err = json.Unmarshal(data, &doc)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return &doc, nil
  87. }
  88. /*
  89. Delete the target document in redis
  90. :param id: the id to delete from redis
  91. */
  92. func (r *RedisCaller) DeleteDoc(id string) error {
  93. _, err := r.Client.Get(r.ctx, id).Result()
  94. if err == redis.Nil {
  95. return &DocDoesntExist{id}
  96. } else if err != nil {
  97. return err
  98. }
  99. err = r.Client.Del(r.ctx, id).Err()
  100. if err != nil {
  101. return err
  102. }
  103. return nil
  104. }
  105. /*
  106. Update a value in redis
  107. :param id: the id of the document to edit
  108. */
  109. func (r *RedisCaller) editVal(id string, in interface{}) error {
  110. _, err := r.Client.Get(r.ctx, id).Result()
  111. if err != nil {
  112. if err == redis.Nil {
  113. return &DocDoesntExist{Key: id}
  114. }
  115. return err
  116. }
  117. data, err := json.Marshal(&in)
  118. if err != nil {
  119. return err
  120. }
  121. err = r.Client.Set(r.ctx, id, data, 0).Err()
  122. if err != nil {
  123. return err
  124. }
  125. return nil
  126. }
  127. func (r *RedisCaller) SeedData(seedLoc string) error {
  128. dirs, err := os.ReadDir(seedLoc)
  129. if err != nil {
  130. return err
  131. }
  132. for i := range dirs {
  133. key := strings.Split(dirs[i].Name(), ".")[0]
  134. b, err := os.ReadFile(fmt.Sprintf("%s/%s", seedLoc, dirs[i].Name()))
  135. if err != nil {
  136. return err
  137. }
  138. err = r.Client.Set(r.ctx, key, b, 0).Err()
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }
  145. func (r *RedisCaller) UpdatePost(id string, new Document) error {
  146. return r.editVal(id, new)
  147. }
  148. func (r *RedisCaller) UpdateHeader(id string, new HeaderCollection) error {
  149. return r.editVal(id, new)
  150. }
  151. func (r *RedisCaller) UpdateMenu(id string, new MenuElement) error {
  152. return r.editVal(id, new)
  153. }