2021-10-07 16:23:34 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ebosas/microservices/internal/models"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cache is a data structure for the cache API template.
|
|
|
|
type Cache struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
Total int `json:"total"`
|
|
|
|
Messages []models.Message `json:"messages"`
|
|
|
|
}
|
|
|
|
|
2021-10-08 21:40:10 +03:00
|
|
|
// GetCache gets cached messages from Redis,
|
|
|
|
// returns both a Cache struct and a JSON string.
|
|
|
|
func GetCache(c *redis.Client) (*Cache, string, error) {
|
2021-10-07 16:23:34 +03:00
|
|
|
cacheJSON, err := GetCacheJSON(c)
|
|
|
|
if err != nil {
|
2021-10-08 21:40:10 +03:00
|
|
|
return &Cache{}, "", fmt.Errorf("get cache json: %v", err)
|
2021-10-07 16:23:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cache Cache
|
|
|
|
err = json.Unmarshal([]byte(cacheJSON), &cache)
|
|
|
|
if err != nil {
|
2021-10-08 21:40:10 +03:00
|
|
|
return &Cache{}, "", fmt.Errorf("unmarshal cache: %v", err)
|
2021-10-07 16:23:34 +03:00
|
|
|
}
|
|
|
|
|
2021-10-08 21:40:10 +03:00
|
|
|
return &cache, cacheJSON, nil
|
2021-10-07 16:23:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCacheJSON reads cached messages from Redis, returns JSON.
|
|
|
|
func GetCacheJSON(c *redis.Client) (string, error) {
|
|
|
|
messages, err := c.LRange(context.Background(), "messages", 0, -1).Result()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("lrange redis: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-08 21:40:10 +03:00
|
|
|
total, err := c.Get(context.Background(), "total").Result()
|
|
|
|
if err == redis.Nil {
|
|
|
|
total = "0"
|
|
|
|
} else if err != nil {
|
2021-10-07 16:23:34 +03:00
|
|
|
return "", fmt.Errorf("get redis: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheJSON := "{\"count\":" + fmt.Sprint(len(messages)) + ",\"total\":" + total + ",\"messages\":[" + strings.Join(messages, ",") + "]}"
|
|
|
|
|
|
|
|
return cacheJSON, nil
|
|
|
|
}
|