Skip to content

Commit

Permalink
Merge pull request #2 from nico-iaco/feature/cache
Browse files Browse the repository at this point in the history
added cache to avoid multiple calls to external api
  • Loading branch information
nico-iaco authored Sep 27, 2022
2 parents b712e90 + 6457d85 commit 1570334
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 14 deletions.
42 changes: 42 additions & 0 deletions foodDetails/service/CacheService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package service

import (
"context"
"github.com/go-redis/cache/v8"
"github.com/go-redis/redis/v8"
"time"
)

type CacheService struct {
cache *cache.Cache
}

var cs *CacheService = nil

func NewCacheService(client redis.Client) CacheService {
if cs == nil {
cs = new(CacheService)
cs.cache = cache.New(&cache.Options{
Redis: client,
LocalCache: cache.NewTinyLFU(1000, time.Minute),
})
}
return *cs
}

func (s CacheService) SetObject(key string, object interface{}) error {
ctx := context.TODO()
err := s.cache.Set(&cache.Item{
Ctx: ctx,
Key: key,
Value: object,
TTL: time.Hour,
})
return err
}

func (s CacheService) GetObject(key string, object interface{}) error {
ctx := context.TODO()
err := s.cache.Get(ctx, key, object)
return err
}
38 changes: 26 additions & 12 deletions foodDetails/service/FoodDetailsService.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,53 @@ import (
"food-details-integrator-be/foodDetails/model"
"github.com/mashingan/smapping"
"github.com/openfoodfacts/openfoodfacts-go"
"os"
)

type FoodDetailsService struct {
CacheService CacheService
}

var fds *FoodDetailsService = nil

func New() FoodDetailsService {
func NewFoodDetailsService(cs CacheService) FoodDetailsService {
if fds == nil {
fds = new(FoodDetailsService)
fds.CacheService = cs
}
return *fds
}

func (s FoodDetailsService) GetProductDetails(barcode string) (model.FoodDetails, error) {
api := openfoodfacts.NewClient("world", "", "")
//api.Sandbox()
product, err := api.Product(barcode)
foodDetails := model.FoodDetails{}

if os.Getenv("IS_SANDBOX") == "true" {
api.Sandbox()
}

err := cs.GetObject(barcode, &foodDetails)

if err != nil {
fmt.Printf("error %s \n", err)
return model.FoodDetails{}, err
} else {
mappedField := smapping.MapTags(product, "json")
err := smapping.FillStructByTags(&foodDetails, mappedField, "json")
product, err := api.Product(barcode)

if err != nil {
fmt.Printf("error %s \n", err)
return model.FoodDetails{}, err
} else {
mappedField := smapping.MapTags(product, "json")
err := smapping.FillStructByTags(&foodDetails, mappedField, "json")
if err != nil {
return model.FoodDetails{}, err
}
foodDetails.ImageURL = product.ImageURL.URL.String()
foodDetails.ImageNutritionURL = product.ImageNutritionURL.URL.String()
}
foodDetails.ImageURL = product.ImageURL.URL.String()
foodDetails.ImageNutritionURL = product.ImageNutritionURL.URL.String()

} else {
fmt.Println("cache hit")
}
return foodDetails, nil
err = cs.SetObject(barcode, foodDetails)
return foodDetails, err
}

func (s FoodDetailsService) GetKcalForFoodQuantity(barcode string, quantity float64, unit string) (float64, error) {
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@ require (
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/go-redis/cache/v8 v8.4.3 // indirect
github.com/go-redis/redis/v8 v8.11.3 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mashingan/smapping v0.1.18 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect
golang.org/x/exp v0.0.0-20210916165020-5cb4fee858ee // indirect
golang.org/x/net v0.0.0-20220921203646-d300de134e69 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
Loading

0 comments on commit 1570334

Please sign in to comment.