@@ -19,6 +19,7 @@ type ReceiptController interface {
19
19
Insert (context * gin.Context )
20
20
Update (context * gin.Context )
21
21
Delete (context * gin.Context )
22
+ RefreshCache (keys ... string )
22
23
}
23
24
24
25
type receiptController struct {
@@ -34,7 +35,12 @@ func NewReceiptController(receiptService service.ReceiptService, receiptCache ca
34
35
}
35
36
36
37
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
+
38
44
res := helper .BuildValidResponse ("OK" , receipts )
39
45
context .JSON (http .StatusOK , res )
40
46
}
@@ -47,18 +53,19 @@ func (c *receiptController) Show(context *gin.Context) {
47
53
return
48
54
}
49
55
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 {
52
58
var receipt entity.Receipt = c .receiptService .Show (id )
53
59
if (receipt == entity.Receipt {}) {
54
60
res := helper .BuildErrorResponse ("failed to retrieve Receipt" , "no data with given receiptID" , helper.EmptyObj {})
55
61
context .AbortWithStatusJSON (http .StatusNotFound , res )
56
62
return
57
63
}
58
- c .receiptCache .Set (strconv .FormatUint (id , 10 ), receipt )
64
+ arr = append (arr , receipt )
65
+ c .receiptCache .Set (strconv .FormatUint (id , 10 ), arr )
59
66
}
60
67
61
- res := helper .BuildValidResponse ("OK" , receipt )
68
+ res := helper .BuildValidResponse ("OK" , arr [ 0 ] )
62
69
context .JSON (http .StatusOK , res )
63
70
}
64
71
@@ -74,6 +81,8 @@ func (c *receiptController) Insert(context *gin.Context) {
74
81
result := c .receiptService .Insert (receiptCreateDTO )
75
82
response := helper .BuildValidResponse ("OK" , result )
76
83
context .JSON (http .StatusCreated , response )
84
+
85
+ c .RefreshCache ("all" )
77
86
}
78
87
79
88
func (c * receiptController ) Update (context * gin.Context ) {
@@ -88,6 +97,8 @@ func (c *receiptController) Update(context *gin.Context) {
88
97
result := c .receiptService .Update (receiptUpdateDTO )
89
98
response := helper .BuildValidResponse ("OK" , result )
90
99
context .JSON (http .StatusOK , response )
100
+
101
+ c .RefreshCache ("all" , strconv .FormatUint (receiptUpdateDTO .ID , 10 ))
91
102
}
92
103
93
104
func (c * receiptController ) Delete (context * gin.Context ) {
@@ -109,4 +120,12 @@ func (c *receiptController) Delete(context *gin.Context) {
109
120
message := fmt .Sprintf ("Receipt with ID %v successfuly deleted" , receipt .ID )
110
121
res := helper .BuildValidResponse (message , helper.EmptyObj {})
111
122
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
+ }
112
131
}
0 commit comments