Fixed caching

This commit is contained in:
Aryom Belousov 2020-11-09 19:21:16 +03:00
parent 7f77f8ebb3
commit f0a83a4f23
4 changed files with 33 additions and 21 deletions

View file

@ -1,7 +1,6 @@
package vk
import (
"encoding/json"
"errors"
"log"
"net/http"
@ -42,35 +41,29 @@ func handleSearch(req *MessageRequest) {
Message(req.Object.UserId, "Карта не найдена")
log.Printf("[info] Could not find card. User input: %s", req.Object.Body)
} else {
prices, err := GetPrices(cardName)
message, err := GetMessage(cardName)
if err != nil {
Message(req.Object.UserId, "Цены временно недоступны, попробуйте позже")
log.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return
}
priceInfo := cardsinfo.FormatCardPrices(cardName, prices)
Message(req.Object.UserId, priceInfo)
Message(req.Object.UserId, message)
}
}
func GetPrices(cardName string) ([]cardsinfo.CardPrice, error) {
func GetMessage(cardName string) (string, error) {
client := caching.GetClient()
val, err := client.Get(cardName)
var prices []cardsinfo.CardPrice
if err != nil {
prices, err = cardsinfo.GetPrices(cardName)
prices, err := cardsinfo.GetPrices(cardName)
if err != nil {
return nil, err
return "", err
}
serialized, err := json.Marshal(prices)
if err != nil {
return nil, err
}
client.Set(cardName, string(serialized))
return prices, nil
message := cardsinfo.FormatCardPrices(cardName, prices)
client.Set(cardName, message)
return message, nil
}
_ = json.Unmarshal([]byte(val), &prices)
return prices, nil
return val, nil
}
func getCardNameByCommand(command string) (string, error) {