Added different formatting for telegram bot

This commit is contained in:
Artyom Belousov 2021-06-06 17:21:29 +03:00
parent 35f8fa5a57
commit 89623f5f6a
18 changed files with 233 additions and 173 deletions

View file

@ -1,9 +1,12 @@
package caching
import (
"encoding/json"
"time"
"github.com/go-redis/redis"
"github.com/pkg/errors"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type CacheClient struct {
@ -22,10 +25,17 @@ func NewClient(addr string, passwd string, expiration time.Duration, db int) *Ca
}
}
func (client *CacheClient) Set(key string, value string) {
func (client *CacheClient) Set(key string, prices []cardsinfo.ScgCardPrice) {
value, _ := json.Marshal(prices)
client.Storage.Set(key, value, client.Expiration)
}
func (client *CacheClient) Get(key string) (string, error) {
return client.Storage.Get(key).Result()
func (client *CacheClient) Get(key string) ([]cardsinfo.ScgCardPrice, error) {
c, err := client.Storage.Get(key).Result()
if err != nil {
return nil, errors.Wrap(err, "No such key in cache")
}
var prices []cardsinfo.ScgCardPrice
json.Unmarshal([]byte(c), &prices)
return prices, nil
}

View file

@ -8,6 +8,7 @@ import (
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
func TestGetClient(t *testing.T) {
@ -23,7 +24,13 @@ func TestGetSet(t *testing.T) {
defer s.Close()
keyName := "test_key"
value := "test_value"
value := []cardsinfo.ScgCardPrice{
{
Price: "1",
Edition: "Alpha",
Link: "scg",
},
}
client.Set(keyName, value)
val, err := client.Get(keyName)
assert.Nil(t, err)
@ -36,11 +43,11 @@ func TestExpiration(t *testing.T) {
client.Expiration = time.Millisecond
keyName := "test_key"
value := "test_value"
var value []cardsinfo.ScgCardPrice
client.Set(keyName, value)
s.FastForward(time.Millisecond * 2)
val, err := client.Get(keyName)
assert.Zero(t, val)
assert.Nil(t, val)
assert.NotNil(t, err)
}