Files
go-gin-api/internal/api/controller/tool_handler/func_searchcache.go

78 lines
1.8 KiB
Go
Raw Normal View History

2021-05-05 17:20:48 +08:00
package tool_handler
import (
"net/http"
"github.com/xinliangnote/go-gin-api/internal/api/repository/redis"
"github.com/xinliangnote/go-gin-api/internal/code"
2021-05-05 17:20:48 +08:00
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/pkg/errno"
)
type searchCacheRequest struct {
RedisKey string `form:"redis_key"` // Redis Key
}
type searchCacheResponse struct {
Val string `json:"val"` // 查询后的值
TTL string `json:"ttl"` // 过期时间
}
// SearchCache 查询缓存
// @Summary 查询缓存
// @Description 查询缓存
// @Tags API.tool
// @Accept multipart/form-data
// @Produce json
// @Param redis_key formData string true "Redis Key"
// @Success 200 {object} searchCacheResponse
// @Failure 400 {object} code.Failure
// @Router /api/tool/cache/search [post]
func (h *handler) SearchCache() core.HandlerFunc {
return func(c core.Context) {
req := new(searchCacheRequest)
res := new(searchCacheResponse)
if err := c.ShouldBindForm(req); err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithErr(err),
)
return
}
if b := h.cache.Exists(req.RedisKey); b != true {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.CacheNotExist,
code.Text(code.CacheNotExist)),
2021-05-05 17:20:48 +08:00
)
return
}
val, err := h.cache.Get(req.RedisKey, redis.WithTrace(c.Trace()))
2021-05-05 17:20:48 +08:00
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.CacheGetError,
code.Text(code.CacheGetError)).WithErr(err),
2021-05-05 17:20:48 +08:00
)
return
}
ttl, err := h.cache.TTL(req.RedisKey)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.CacheGetError,
code.Text(code.CacheGetError)).WithErr(err),
2021-05-05 17:20:48 +08:00
)
return
}
res.Val = val
res.TTL = ttl.String()
c.Payload(res)
}
}