From f0a83a4f238e28f81f548c3b1e2a18f267c5b981 Mon Sep 17 00:00:00 2001 From: Aryom Belousov Date: Mon, 9 Nov 2020 19:21:16 +0300 Subject: [PATCH] Fixed caching --- .gitlab-ci.yml | 3 ++- cardsinfo/prices.go | 12 ++++++++---- docker-compose.yaml | 14 ++++++++++++++ vk/handlers.go | 25 +++++++++---------------- 4 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 docker-compose.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 87b7a50..140ca49 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/cardsinfo/prices.go b/cardsinfo/prices.go index 5481374..36802fa 100644 --- a/cardsinfo/prices.go +++ b/cardsinfo/prices.go @@ -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 } diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..342d661 --- /dev/null +++ b/docker-compose.yaml @@ -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" diff --git a/vk/handlers.go b/vk/handlers.go index 0a89ab8..f41308c 100644 --- a/vk/handlers.go +++ b/vk/handlers.go @@ -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) {