-
Notifications
You must be signed in to change notification settings - Fork 1
/
product_service.go
125 lines (113 loc) · 3.24 KB
/
product_service.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package v1
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/go-redis/redis"
)
var (
errMissingProductName = errors.New("missing product name")
errMissingProductCategory = errors.New("missing product category")
errMissingTTLDuration = errors.New("missing TTL duration")
errEmptyProduct = errors.New("product is empty")
)
//StoreProduct save AmazonProduct into Redis with key product:{ASIN}
func StoreProduct(c *redis.Client, scrapedProduct *AmazonProduct) (err error) {
var product = make(map[string]interface{})
if scrapedProduct.Asin == "" {
return ErrMissingASIN
}
if scrapedProduct.Name == "" {
return errMissingProductName
}
if len(scrapedProduct.Categories) == 0 {
return errMissingProductCategory
}
product["asin"] = scrapedProduct.Asin
product["name"] = scrapedProduct.Name
product["categories"] = strings.Join(scrapedProduct.Categories, ";")
product["ranks"] = strings.Join(scrapedProduct.Ranks, ";")
product["dimensions"] = strings.Join(scrapedProduct.Dimensions, ";")
product["created_at"] = scrapedProduct.CreatedAt
err = c.HMSet("product:"+scrapedProduct.Asin, product).Err()
if err != nil {
return err
}
return nil
}
//FetchProduct get AmazonProduct from Redis
//Todo: Fetch product will be used in Phase 2 for limited caching
func FetchProduct(c *redis.Client, asin string) (product AmazonProduct, err error) {
if asin == "" {
err = ErrMissingASIN
return
}
//Check if product key exists
exist, err := c.Exists("product:" + asin).Result()
if err != nil {
return
} else if exist == 0 {
err = fmt.Errorf("product key: product:%s doesn't exist", asin)
return
}
name, err := c.HGet("product:"+asin, "name").Result()
if err != nil {
return
}
categories, err := c.HGet("product:"+asin, "categories").Result()
if err != nil {
return
}
createdAt, err := c.HGet("product:"+asin, "created_at").Result()
if err != nil {
return
}
product.Asin = asin
product.Name = name
product.Categories = strings.Split(categories, ";")
product.CreatedAt = createdAt
//ranks are not required, and it can be nil
ranks, _ := c.HGet("product:"+asin, "ranks").Result()
if len(ranks) > 0 {
product.Ranks = strings.Split(ranks, ";")
}
//dimensions are not required, and it can be nil
dimensions, _ := c.HGet("product:"+asin, "dimensions").Result()
if len(dimensions) > 0 {
product.Dimensions = strings.Split(dimensions, ";")
}
return
}
//GetProductFromCache tries to grab cached product
//from cacheProduct with key cacheProduct:{ASIN}
func GetProductFromCache(c *redis.Client, asin string) (product AmazonProduct, err error) {
val, err := c.Get("cacheProduct:" + asin).Result()
if err != nil {
return
}
err = json.Unmarshal([]byte(val), &product)
if err != nil {
return
}
return
}
//AddProductToCache cache AmazonProduct with key cacheProduct:{ASIN}
func AddProductToCache(c *redis.Client, scrapedProduct *AmazonProduct, duration time.Duration) (err error) {
if scrapedProduct.Name == "" {
err = errEmptyProduct
return
}
productJSON, err := json.Marshal(scrapedProduct)
if err != nil {
return
}
if duration == 0 {
err = errMissingTTLDuration
return
}
//store value as JSON string
err = c.Set("cacheProduct:"+scrapedProduct.Asin, string(productJSON), duration).Err()
return
}