From 98a804024679186abe20746e1efdceb5d5ca11ad Mon Sep 17 00:00:00 2001 From: Artyom Belousov Date: Thu, 16 May 2019 14:58:24 +0300 Subject: [PATCH] Added redis support --- caching/client.go | 39 +++++++++++++++++++++++++++++++++++++++ caching/secrets.go | 9 +++++++++ tests/cache_test.go | 36 ++++++++++++++++++++++++++++++++++++ vk/handlers.go | 18 +++++++++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 caching/client.go create mode 100644 caching/secrets.go create mode 100644 tests/cache_test.go diff --git a/caching/client.go b/caching/client.go new file mode 100644 index 0000000..57643d9 --- /dev/null +++ b/caching/client.go @@ -0,0 +1,39 @@ +package caching + +import ( + "github.com/go-redis/redis" + "time" +) + +type CacheClient struct { + storage *redis.Client + Expiration time.Duration +} + +var client *CacheClient + +func GetClient() *CacheClient { + if client != nil { + return client + } + client = new(CacheClient) + client.Init() + return client +} + +func (client *CacheClient) Init() { + client.storage = redis.NewClient(&redis.Options{ + Addr: HOST_NAME, + Password: PASSWORD, + DB: 0, + }) + client.Expiration = CACHE_EXPIRATION +} + +func (client *CacheClient) Set(key string, value string) { + client.storage.Set(key, value, client.Expiration) +} + +func (client *CacheClient) Get(key string) (string, error) { + return client.storage.Get(key).Result() +} diff --git a/caching/secrets.go b/caching/secrets.go new file mode 100644 index 0000000..ab1951b --- /dev/null +++ b/caching/secrets.go @@ -0,0 +1,9 @@ +package caching + +import ( + "time" +) + +const HOST_NAME = "localhost:6379" +const PASSWORD = "" +const CACHE_EXPIRATION = time.Hour * 24 diff --git a/tests/cache_test.go b/tests/cache_test.go new file mode 100644 index 0000000..63088a1 --- /dev/null +++ b/tests/cache_test.go @@ -0,0 +1,36 @@ +package tests + +import ( + "github.com/flygrounder/mtg-price-vk/caching" + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestGetSet(t *testing.T) { + client := getTestClient() + keyName := "test_key" + value := "test_value" + client.Set(keyName, value) + val, err := client.Get(keyName) + assert.Nil(t, err) + assert.Equal(t, value, val) +} + +func TestExpiration(t *testing.T) { + client := getTestClient() + client.Expiration = time.Millisecond + keyName := "test_key" + value := "test_value" + client.Set(keyName, value) + time.Sleep(time.Millisecond * 2) + val, err := client.Get(keyName) + assert.Zero(t, val) + assert.NotNil(t, err) +} + +func getTestClient() *caching.CacheClient { + client := new(caching.CacheClient) + client.Init() + return client +} diff --git a/vk/handlers.go b/vk/handlers.go index e158a93..b03c60d 100644 --- a/vk/handlers.go +++ b/vk/handlers.go @@ -1,7 +1,9 @@ package vk import ( + "encoding/json" "errors" + "github.com/flygrounder/mtg-price-vk/caching" "github.com/flygrounder/mtg-price-vk/cardsinfo" "github.com/gin-gonic/gin" "net/http" @@ -39,7 +41,7 @@ func handleSearch(c *gin.Context, req *MessageRequest) { } else if cardName == "" { Message(req.Object.UserId, "Карта не найдена") } else { - prices, _ := cardsinfo.GetSCGPrices(cardName) + prices := GetPrices(cardName) elements := min(CARDSLIMIT, len(prices)) prices = prices[:elements] priceInfo := cardsinfo.FormatCardPrices(cardName, prices) @@ -47,6 +49,20 @@ func handleSearch(c *gin.Context, req *MessageRequest) { } } +func GetPrices(cardName string) []cardsinfo.CardPrice { + client := caching.GetClient() + val, err := client.Get(cardName) + var prices []cardsinfo.CardPrice + if err != nil { + prices, _ = cardsinfo.GetSCGPrices(cardName) + serialized, _ := json.Marshal(prices) + client.Set(cardName, string(serialized)) + return prices + } + json.Unmarshal([]byte(val), &prices) + return prices +} + func getCardNameByCommand(command string) (string, error) { var name string switch {