genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/models/product.go

37 lines
753 B
Go

package models
import (
"net/http"
"time"
)
type boolMicroServiceCache struct {
LastCheck time.Time
Value bool
}
var productExistCache map[int64]boolMicroServiceCache
func init() {
productExistCache = make(map[int64]boolMicroServiceCache)
}
func ProductExists(id int64) (bool, error) {
if cache, ok := productExistCache[id]; ok {
// cache for 5min
before := time.Now().Add(-time.Minute * 5)
if !cache.LastCheck.Before(before) {
return cache.Value, nil
}
}
// TODO DRAFT for a rest request to a other microservice
res, err := http.Get("http://golang.org")
productExistCache[id] = boolMicroServiceCache{
LastCheck: time.Now(),
Value: (res.StatusCode == http.StatusOK),
}
return productExistCache[id].Value, err
}