Redis -> YDB

This commit is contained in:
Artyom Belousov 2023-05-27 22:10:31 +03:00
parent 1f4888069f
commit a33680d527
21 changed files with 448 additions and 322 deletions

View file

@ -1,41 +1,107 @@
package caching
import (
"context"
"encoding/json"
"fmt"
"path"
"time"
"github.com/go-redis/redis"
"github.com/pkg/errors"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type CacheClient struct {
Storage *redis.Client
Storage table.Client
Expiration time.Duration
Prefix string
}
func NewClient(addr string, passwd string, expiration time.Duration, db int) *CacheClient {
return &CacheClient{
Storage: redis.NewClient(&redis.Options{
Addr: addr,
Password: passwd,
DB: db,
}),
Expiration: expiration,
}
func (client *CacheClient) Init(ctx context.Context) error {
return client.Storage.Do(ctx, func(ctx context.Context, s table.Session) error {
return s.CreateTable(
ctx,
path.Join(client.Prefix, "cache"),
options.WithColumn("card", types.TypeString),
options.WithColumn("prices", types.Optional(types.TypeJSON)),
options.WithColumn("created_at", types.Optional(types.TypeTimestamp)),
options.WithTimeToLiveSettings(
options.NewTTLSettings().ColumnDateType("created_at").ExpireAfter(client.Expiration),
),
options.WithPrimaryKeyColumn("card"),
)
})
}
func (client *CacheClient) Set(key string, prices []cardsinfo.ScgCardPrice) {
func (client *CacheClient) Set(ctx context.Context, key string, prices []cardsinfo.ScgCardPrice) error {
const query = `
DECLARE $cacheData AS List<Struct<
card: String,
prices: Json,
created_at: Timestamp>>;
INSERT INTO cache SELECT cd.card AS card, cd.prices AS prices, cd.created_at AS created_at FROM AS_TABLE($cacheData) cd LEFT OUTER JOIN cache c ON cd.card = c.card WHERE c.card IS NULL`
value, _ := json.Marshal(prices)
client.Storage.Set(key, value, client.Expiration)
return client.Storage.Do(ctx, func(ctx context.Context, s table.Session) error {
_, _, err := s.Execute(ctx, writeTx(), query, table.NewQueryParameters(
table.ValueParam("$cacheData", types.ListValue(
types.StructValue(
types.StructFieldValue("card", types.StringValueFromString(key)),
types.StructFieldValue("prices", types.JSONValueFromBytes(value)),
types.StructFieldValue("created_at", types.TimestampValueFromTime(time.Now())),
))),
))
return err
})
}
func (client *CacheClient) Get(key string) ([]cardsinfo.ScgCardPrice, error) {
c, err := client.Storage.Get(key).Result()
func (client *CacheClient) Get(ctx context.Context, key string) ([]cardsinfo.ScgCardPrice, error) {
const query = `
DECLARE $card AS String;
SELECT UNWRAP(prices) AS prices FROM cache WHERE card = $card`
var pricesStr string
err := client.Storage.Do(ctx, func(ctx context.Context, s table.Session) error {
_, res, err := s.Execute(ctx, readTx(), query, table.NewQueryParameters(
table.ValueParam("$card", types.StringValueFromString(key)),
))
if err != nil {
return err
}
ok := res.NextResultSet(ctx)
if !ok {
return errors.New("no key")
}
ok = res.NextRow()
if !ok {
return errors.New("no key")
}
err = res.ScanNamed(
named.Required("prices", &pricesStr),
)
return err
})
if err != nil {
return nil, errors.Wrap(err, "No such key in cache")
fmt.Println(err.Error())
return nil, errors.Wrap(err, "Failed to get key")
}
var prices []cardsinfo.ScgCardPrice
json.Unmarshal([]byte(c), &prices)
json.Unmarshal([]byte(pricesStr), &prices)
return prices, nil
}
func writeTx() *table.TransactionControl {
return table.TxControl(table.BeginTx(
table.WithSerializableReadWrite(),
), table.CommitTx())
}
func readTx() *table.TransactionControl {
return table.TxControl(table.BeginTx(
table.WithOnlineReadOnly(),
), table.CommitTx())
}

View file

@ -1,64 +0,0 @@
package caching
import (
"fmt"
"testing"
"time"
"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) {
c := NewClient("addr", "123", time.Hour, 1)
assert.Equal(t, time.Hour, c.Expiration)
assert.Equal(t, 1, c.Storage.Options().DB)
assert.Equal(t, "addr", c.Storage.Options().Addr)
assert.Equal(t, "123", c.Storage.Options().Password)
}
func TestGetSet(t *testing.T) {
client, s := getTestClient()
defer s.Close()
keyName := "test_key"
value := []cardsinfo.ScgCardPrice{
{
Price: "1",
Edition: "Alpha",
Link: "scg",
},
}
client.Set(keyName, value)
val, err := client.Get(keyName)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
func TestExpiration(t *testing.T) {
client, s := getTestClient()
defer s.Close()
client.Expiration = time.Millisecond
keyName := "test_key"
var value []cardsinfo.ScgCardPrice
client.Set(keyName, value)
s.FastForward(time.Millisecond * 2)
val, err := client.Get(keyName)
assert.Nil(t, val)
assert.NotNil(t, err)
}
func getTestClient() (*CacheClient, *miniredis.Miniredis) {
s, _ := miniredis.Run()
fmt.Println(s.Addr())
c := redis.NewClient(&redis.Options{
Addr: s.Addr(),
})
return &CacheClient{
Storage: c,
Expiration: 0,
}, s
}