@@ -53,14 +53,14 @@ func SetJSONCache(ctx iris.Context, key string, value interface{}) error {
5353 return SetCache (ctx , key , string (val ))
5454}
5555
56- func GetJSONCache (ctx iris.Context , key string ) (value iris. Map , ok bool ) {
56+ func GetJSONCache (ctx iris.Context , key string ) (value AnalysisData , ok bool ) {
5757 val , ok := GetCache (ctx , key )
5858 if ! ok {
59- return nil , false
59+ return AnalysisData {} , false
6060 }
6161 err := json .Unmarshal ([]byte (val ), & value )
6262 if err != nil {
63- return nil , false
63+ return AnalysisData {} , false
6464 }
6565 return value , true
6666}
@@ -79,22 +79,29 @@ func CachedHandler(h iris.Handler, params ...string) iris.Handler {
7979 return func (ctx iris.Context ) {
8080 key := GenerateKey (ctx , params ... )
8181
82- result , ok := GetJSONCache (ctx , key )
82+ data , ok := GetJSONCache (ctx , key )
8383 if ok {
84- ctx .JSON (result )
85- return
84+ EndBody (ctx , data )
85+ } else {
86+ h (ctx )
8687 }
88+ }
89+ }
8790
88- h (ctx )
91+ func EndBody (ctx iris.Context , data AnalysisData ) {
92+ if data .Err != nil {
93+ ThrowError (ctx , data .Err .Error (), data .Code )
94+ } else {
95+ ctx .JSON (data .Data )
8996 }
9097}
9198
92- func EndBody (ctx iris.Context , value iris. Map , params ... string ) {
93- err := SetJSONCache (ctx , GenerateKey (ctx , params ... ), value )
99+ func EndBodyWithCache (ctx iris.Context , data AnalysisData , params ... string ) {
100+ err := SetJSONCache (ctx , GenerateKey (ctx , params ... ), data )
94101 if err != nil {
95102 logger .Errorf ("Failed to set cache: %s" , err .Error ())
96103 ThrowError (ctx , err .Error (), iris .StatusInternalServerError )
97104 return
98105 }
99- ctx . JSON ( value )
106+ EndBody ( ctx , data )
100107}
0 commit comments