Skip to content

Commit 1930024

Browse files
committed
feat: caching for get all Receipts and cache refresh
1 parent f95b38f commit 1930024

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

cache/receipt-cache.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ package cache
33
import (
44
"context"
55
"encoding/json"
6+
"log"
67
"time"
78

89
"github.com/go-redis/redis/v8"
910
"github.com/putukrisna6/golang-api/entity"
1011
)
1112

1213
type ReceiptCache interface {
13-
Set(key string, value entity.Receipt)
14-
Get(key string) entity.Receipt
14+
Set(key string, value []entity.Receipt)
15+
Get(key string) []entity.Receipt
16+
Del(key string)
1517
}
1618

1719
type receiptCache struct {
@@ -26,7 +28,7 @@ func NewReceiptCache(cache *redis.Client, expires time.Duration) ReceiptCache {
2628
}
2729
}
2830

29-
func (rc *receiptCache) Set(key string, value entity.Receipt) {
31+
func (rc *receiptCache) Set(key string, value []entity.Receipt) {
3032
json, err := json.Marshal(value)
3133
if err != nil {
3234
panic(err)
@@ -35,20 +37,26 @@ func (rc *receiptCache) Set(key string, value entity.Receipt) {
3537
rc.cache.Set(context.TODO(), "receipt:"+key, json, rc.expires*time.Second)
3638
}
3739

38-
func (rc *receiptCache) Get(key string) entity.Receipt {
40+
func (rc *receiptCache) Get(key string) []entity.Receipt {
3941
val, errGet := rc.cache.Get(context.TODO(), "receipt:"+key).Result()
4042

4143
if errGet == redis.Nil {
42-
return entity.Receipt{}
44+
return nil
4345
} else if errGet != nil {
4446
panic(errGet)
4547
}
4648

47-
receipt := entity.Receipt{}
49+
var receipt []entity.Receipt
4850
err := json.Unmarshal([]byte(val), &receipt)
4951
if err != nil {
5052
panic(err)
5153
}
5254

5355
return receipt
5456
}
57+
58+
func (rc *receiptCache) Del(key string) {
59+
err := rc.cache.Del(context.TODO(), "receipt:"+key)
60+
61+
log.Println(err)
62+
}

controller/receipt-controller.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ReceiptController interface {
1919
Insert(context *gin.Context)
2020
Update(context *gin.Context)
2121
Delete(context *gin.Context)
22+
RefreshCache(keys ...string)
2223
}
2324

2425
type receiptController struct {
@@ -34,7 +35,12 @@ func NewReceiptController(receiptService service.ReceiptService, receiptCache ca
3435
}
3536

3637
func (c *receiptController) All(context *gin.Context) {
37-
var receipts []entity.Receipt = c.receiptService.All()
38+
var receipts []entity.Receipt = c.receiptCache.Get("all")
39+
if receipts == nil {
40+
receipts = c.receiptService.All()
41+
c.receiptCache.Set("all", receipts)
42+
}
43+
3844
res := helper.BuildValidResponse("OK", receipts)
3945
context.JSON(http.StatusOK, res)
4046
}
@@ -47,18 +53,19 @@ func (c *receiptController) Show(context *gin.Context) {
4753
return
4854
}
4955

50-
var receipt entity.Receipt = c.receiptCache.Get(strconv.FormatUint(id, 10))
51-
if (receipt == entity.Receipt{}) {
56+
var arr []entity.Receipt = c.receiptCache.Get(strconv.FormatUint(id, 10))
57+
if arr == nil {
5258
var receipt entity.Receipt = c.receiptService.Show(id)
5359
if (receipt == entity.Receipt{}) {
5460
res := helper.BuildErrorResponse("failed to retrieve Receipt", "no data with given receiptID", helper.EmptyObj{})
5561
context.AbortWithStatusJSON(http.StatusNotFound, res)
5662
return
5763
}
58-
c.receiptCache.Set(strconv.FormatUint(id, 10), receipt)
64+
arr = append(arr, receipt)
65+
c.receiptCache.Set(strconv.FormatUint(id, 10), arr)
5966
}
6067

61-
res := helper.BuildValidResponse("OK", receipt)
68+
res := helper.BuildValidResponse("OK", arr[0])
6269
context.JSON(http.StatusOK, res)
6370
}
6471

@@ -74,6 +81,8 @@ func (c *receiptController) Insert(context *gin.Context) {
7481
result := c.receiptService.Insert(receiptCreateDTO)
7582
response := helper.BuildValidResponse("OK", result)
7683
context.JSON(http.StatusCreated, response)
84+
85+
c.RefreshCache("all")
7786
}
7887

7988
func (c *receiptController) Update(context *gin.Context) {
@@ -88,6 +97,8 @@ func (c *receiptController) Update(context *gin.Context) {
8897
result := c.receiptService.Update(receiptUpdateDTO)
8998
response := helper.BuildValidResponse("OK", result)
9099
context.JSON(http.StatusOK, response)
100+
101+
c.RefreshCache("all", strconv.FormatUint(receiptUpdateDTO.ID, 10))
91102
}
92103

93104
func (c *receiptController) Delete(context *gin.Context) {
@@ -109,4 +120,12 @@ func (c *receiptController) Delete(context *gin.Context) {
109120
message := fmt.Sprintf("Receipt with ID %v successfuly deleted", receipt.ID)
110121
res := helper.BuildValidResponse(message, helper.EmptyObj{})
111122
context.JSON(http.StatusOK, res)
123+
124+
c.RefreshCache("all", strconv.FormatUint(id, 10))
125+
}
126+
127+
func (c *receiptController) RefreshCache(keys ...string) {
128+
for _, key := range keys {
129+
c.receiptCache.Del(key)
130+
}
112131
}

0 commit comments

Comments
 (0)