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

@ -11,8 +11,9 @@ deploy:
services:
- docker:19.03.12-dind
script:
- docker build . -t $DOCKER_IMAGE_TAG:$CI_JOB_ID$CI_COMMIT_SHORT_SHA
- docker build . -t $DOCKER_IMAGE_TAG -t $DOCKER_IMAGE_TAG:$CI_JOB_ID$CI_COMMIT_SHORT_SHA
- docker login $REGISTRY_NAME -u $DOCKER_USER -p $DOCKER_PASSWORD
- docker push $DOCKER_IMAGE_TAG:$CI_JOB_ID$CI_COMMIT_SHORT_SHA
- docker push $DOCKER_IMAGE_TAG
only:
- master

View file

@ -14,7 +14,14 @@ const scgDomain = "https://starcitygames.com"
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query=%v"
func GetPrices(name string) ([]CardPrice, error) {
return GetPricesScg(name)
prices, err := GetPricesScg(name)
if err != nil {
return nil, err
}
if len(prices) > 5 {
return prices[:5], nil
}
return prices, nil
}
func GetPricesScg(name string) ([]CardPrice, error) {
@ -69,8 +76,5 @@ func GetPricesTcg(name string) ([]CardPrice, error) {
}
prices = append(prices, cardPrice)
}
if len(prices) > 5 {
return prices[:5], nil
}
return prices, nil
}

14
docker-compose.yaml Normal file
View file

@ -0,0 +1,14 @@
version: "3.8"
services:
web:
image: flygrounder/go-mtg-vk:latest
environment:
- VK_TOKEN
- VK_SECRET_KEY
- VK_GROUP_ID
- VK_CONFIRMATION_STRING
ports:
- "8888:80"
redis:
image: "redis:6.0.9"

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