redis.go 3.1 KB

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