mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
add 5 minute cache
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user