Added redis support

This commit is contained in:
Artyom Belousov 2019-05-16 14:58:24 +03:00
parent 577664c034
commit 98a8040246
4 changed files with 101 additions and 1 deletions

View file

@ -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 {