Beispiel #1
0
//SetRespCache ...
//save resp to cache
func SetRespCache(c *gin.Context, data []byte) error {
	if c.Request.Method != "GET" {
		return errors.New("request type is not GET")
	}

	if c.Writer.Status() != 200 {
		return errors.New("response code not 200")
	}

	key := keyFromRequest(c.Request)

	resp := &response{}
	resp.Header = c.Writer.Header()
	resp.Data = data
	resp.Status = c.Writer.Status()
	resp.ContentType = c.ContentType()

	value, err := json.Marshal(resp)
	if err != nil {
		cache.Set(key, string(value))
	}
	return nil
}