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: services:
- docker:19.03.12-dind - docker:19.03.12-dind
script: 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 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:$CI_JOB_ID$CI_COMMIT_SHORT_SHA
- docker push $DOCKER_IMAGE_TAG
only: only:
- master - master

View file

@ -14,7 +14,14 @@ const scgDomain = "https://starcitygames.com"
const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query=%v" const scgSearchUrlTemplate = "https://starcitygames.hawksearch.com/sites/starcitygames/?search_query=%v"
func GetPrices(name string) ([]CardPrice, error) { 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) { func GetPricesScg(name string) ([]CardPrice, error) {
@ -69,8 +76,5 @@ func GetPricesTcg(name string) ([]CardPrice, error) {
} }
prices = append(prices, cardPrice) prices = append(prices, cardPrice)
} }
if len(prices) > 5 {
return prices[:5], nil
}
return prices, 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 package vk
import ( import (
"encoding/json"
"errors" "errors"
"log" "log"
"net/http" "net/http"
@ -42,35 +41,29 @@ func handleSearch(req *MessageRequest) {
Message(req.Object.UserId, "Карта не найдена") Message(req.Object.UserId, "Карта не найдена")
log.Printf("[info] Could not find card. User input: %s", req.Object.Body) log.Printf("[info] Could not find card. User input: %s", req.Object.Body)
} else { } else {
prices, err := GetPrices(cardName) message, err := GetMessage(cardName)
if err != nil { if err != nil {
Message(req.Object.UserId, "Цены временно недоступны, попробуйте позже") Message(req.Object.UserId, "Цены временно недоступны, попробуйте позже")
log.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName) log.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return return
} }
priceInfo := cardsinfo.FormatCardPrices(cardName, prices) Message(req.Object.UserId, message)
Message(req.Object.UserId, priceInfo)
} }
} }
func GetPrices(cardName string) ([]cardsinfo.CardPrice, error) { func GetMessage(cardName string) (string, error) {
client := caching.GetClient() client := caching.GetClient()
val, err := client.Get(cardName) val, err := client.Get(cardName)
var prices []cardsinfo.CardPrice
if err != nil { if err != nil {
prices, err = cardsinfo.GetPrices(cardName) prices, err := cardsinfo.GetPrices(cardName)
if err != nil { if err != nil {
return nil, err return "", err
} }
serialized, err := json.Marshal(prices) message := cardsinfo.FormatCardPrices(cardName, prices)
if err != nil { client.Set(cardName, message)
return nil, err return message, nil
} }
client.Set(cardName, string(serialized)) return val, nil
return prices, nil
}
_ = json.Unmarshal([]byte(val), &prices)
return prices, nil
} }
func getCardNameByCommand(command string) (string, error) { func getCardNameByCommand(command string) (string, error) {