作者 庄敏学
... ... @@ -55,7 +55,7 @@ func main() {
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
handler.RegisterExternalHandlers(server, ctx)
db.Migrate(ctx.DB)
//启动消费队列
startConsume(ctx)
... ...
package handler
import (
"github.com/zeromicro/go-zero/rest"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/openapi/rd"
)
func RegisterExternalHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(rd.Routers(serverCtx.Redis, serverCtx.Config.Name))
}
... ...
package rd
import (
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"net/http"
"strings"
)
func Routers(rd *redis.Redis, appName string) []rest.Route {
return []rest.Route{
{
Method: http.MethodGet,
Path: "/rd/clean",
Handler: CleanCache(rd, appName),
},
}
}
func CleanCache(rd *redis.Redis, appName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
success int
)
if strings.TrimSpace(appName) == "" {
return
}
keyPattern := fmt.Sprintf("%s*", appName)
list, err := rd.Keys(keyPattern)
if err != nil {
return
}
for _, key := range list {
if _, err = rd.Del(key); err == nil {
success++
}
}
logx.Infof("清理缓存:%d/%d", success, len(list))
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("清理缓存:%d/%d", success, len(list))))
return
}
}
... ...