Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

45 linhas
1.1 KiB

  1. package presenseredis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/redis/go-redis/v9"
  7. )
  8. // Get Map from Redis
  9. //
  10. // Deprecated: there is only one map now and we know the type
  11. func LoadRedisMap[K comparable, V any, M map[K]V](client *redis.Client, ctx context.Context, key string) M {
  12. redisValue, err := client.Get(ctx, key).Result()
  13. resMap := make(M)
  14. if err == redis.Nil {
  15. fmt.Printf("No list found for key %s, starting empty\n", key)
  16. } else if err != nil {
  17. fmt.Printf("Error in connecting to Redis: %v, key: %s returning empty map\n", err, key)
  18. } else {
  19. if err := json.Unmarshal([]byte(redisValue), &resMap); err != nil {
  20. fmt.Printf("Error in unmarshalling JSON for key: %s\n", key)
  21. }
  22. }
  23. return resMap
  24. }
  25. // Set Map in Redis
  26. //
  27. // Deprecated: hashmaps are used now
  28. func SaveRedisMap(client *redis.Client, ctx context.Context, key string, data interface{}) {
  29. eData, err := json.Marshal(data)
  30. if err != nil {
  31. fmt.Println("Error in marshalling, key: ", key)
  32. }
  33. err = client.Set(ctx, key, eData, 0).Err()
  34. if err != nil {
  35. fmt.Println("Error in persisting in Redis, key: ", key)
  36. }
  37. }