From a5f5cd24cd13f5845ccc9b695582e61c2619de0f Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 15 Jun 2021 21:05:38 +0100 Subject: [PATCH] add 5 minute cache --- currency/handler/currency.go | 28 +++++++++++++++++++++++++++- currency/main.go | 9 ++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/currency/handler/currency.go b/currency/handler/currency.go index a34913d..378ae5d 100644 --- a/currency/handler/currency.go +++ b/currency/handler/currency.go @@ -10,10 +10,12 @@ import ( "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" pb "github.com/micro/services/currency/proto" + "github.com/patrickmn/go-cache" ) type Currency struct { - Api string + Api string + Cache *cache.Cache } func (c *Currency) Rates(ctx context.Context, req *pb.RatesRequest, rsp *pb.RatesResponse) error { @@ -24,6 +26,13 @@ func (c *Currency) Rates(ctx context.Context, req *pb.RatesRequest, rsp *pb.Rate return errors.BadRequest("currency.rates", "code is invalid") } + // try the cache + if rates, ok := c.Cache.Get("rates:" + req.Code); ok { + rsp.Code = req.Code + rsp.Rates = rates.(map[string]float64) + return nil + } + resp, err := http.Get(c.Api + "/latest/" + req.Code) if err != nil { logger.Errorf("Failed to get rates: %v\n", err) @@ -58,6 +67,9 @@ func (c *Currency) Rates(ctx context.Context, req *pb.RatesRequest, rsp *pb.Rate rsp.Rates[code], _ = rate.(float64) } + // set for a period of time + c.Cache.Set("rates:"+req.Code, rsp.Rates, cache.DefaultExpiration) + return nil } @@ -71,6 +83,17 @@ func (c *Currency) Convert(ctx context.Context, req *pb.ConvertRequest, rsp *pb. uri := fmt.Sprintf("%s/pair/%s/%s", c.Api, req.From, req.To) + // try the cache + if req.Amount == 0 { + rate, ok := c.Cache.Get("pair:" + req.From + req.To) + if ok { + rsp.From = req.From + rsp.To = req.To + rsp.Rate = rate.(float64) + return nil + } + } + if req.Amount > 0.0 { uri = fmt.Sprintf("%s/%v", uri, req.Amount) } @@ -101,5 +124,8 @@ func (c *Currency) Convert(ctx context.Context, req *pb.ConvertRequest, rsp *pb. rsp.Rate, _ = respBody["conversion_rate"].(float64) rsp.Amount, _ = respBody["conversion_result"].(float64) + // save for a period of time + c.Cache.Set("pair:"+req.From+req.To, rsp.Rate, cache.DefaultExpiration) + return nil } diff --git a/currency/main.go b/currency/main.go index c541792..1dadf24 100644 --- a/currency/main.go +++ b/currency/main.go @@ -1,12 +1,14 @@ package main import ( - "github.com/micro/services/currency/handler" - pb "github.com/micro/services/currency/proto" + "time" "github.com/micro/micro/v3/service" "github.com/micro/micro/v3/service/config" "github.com/micro/micro/v3/service/logger" + "github.com/micro/services/currency/handler" + pb "github.com/micro/services/currency/proto" + "github.com/patrickmn/go-cache" ) func main() { @@ -35,7 +37,8 @@ func main() { // Register handler pb.RegisterCurrencyHandler(srv.Server(), &handler.Currency{ - Api: api + key, + Api: api + key, + Cache: cache.New(5*time.Minute, 10*time.Minute), }) // Run service