-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (53 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/nikolasmelui/go-ecommerce-data-transfer-microservice/cache"
"github.com/nikolasmelui/go-ecommerce-data-transfer-microservice/cconfig"
"github.com/nikolasmelui/go-ecommerce-data-transfer-microservice/entity"
)
func main() {
sourceClient := entity.NewClient(cconfig.Config.SourceURL)
targetClient := entity.NewClient(cconfig.Config.TargetURL)
var productsResponse entity.ProductsResponse
err := sourceClient.Get("/products", &productsResponse)
if err != nil {
log.Fatalf("Error: %v", err.Error())
}
cacher := cache.NewRedisConnection(cconfig.Config.RedisHost, cconfig.Config.RedisPassword, cconfig.Config.RedisDB, 120)
products := productsResponse.Products
for i, product := range products {
fmt.Printf("%d ----------\n", i)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
var productCache cache.Cachable
productCache = &cache.ProductCache{}
err = cacher.Get(ctx, product.ID, &productCache)
if err != nil {
log.Fatalf("Error: %v", err.Error())
}
var productToCache cache.Cachable
productToCache = &cache.ProductCache{
Data: product,
Hash: "",
}
productToCache.SetHash()
productCacheHash := productCache.GetHash()
productToCacheHash := productToCache.GetHash()
// Chech the instance is not in the cache or hashes is different
if (len(productCacheHash) < 1) || (productCacheHash != productToCacheHash) {
err := cacher.Set(ctx, product.ID, &productToCache)
if err != nil {
log.Fatalf("Error: %v", err.Error())
}
productToSet := product.GetDataToSet()
err = targetClient.Set("/products", productToSet)
if err != nil {
log.Fatalf("Error: %v", err.Error())
}
}
time.Sleep(50 * time.Millisecond)
}
}