channel_affinity_cache.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package controller
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/QuantumNous/new-api/service"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func GetChannelAffinityCacheStats(c *gin.Context) {
  9. stats := service.GetChannelAffinityCacheStats()
  10. c.JSON(http.StatusOK, gin.H{
  11. "success": true,
  12. "message": "",
  13. "data": stats,
  14. })
  15. }
  16. func ClearChannelAffinityCache(c *gin.Context) {
  17. all := strings.TrimSpace(c.Query("all"))
  18. ruleName := strings.TrimSpace(c.Query("rule_name"))
  19. if all == "true" {
  20. deleted := service.ClearChannelAffinityCacheAll()
  21. c.JSON(http.StatusOK, gin.H{
  22. "success": true,
  23. "message": "",
  24. "data": gin.H{
  25. "deleted": deleted,
  26. },
  27. })
  28. return
  29. }
  30. if ruleName == "" {
  31. c.JSON(http.StatusBadRequest, gin.H{
  32. "success": false,
  33. "message": "缺少参数:rule_name,或使用 all=true 清空全部",
  34. })
  35. return
  36. }
  37. deleted, err := service.ClearChannelAffinityCacheByRuleName(ruleName)
  38. if err != nil {
  39. c.JSON(http.StatusBadRequest, gin.H{
  40. "success": false,
  41. "message": err.Error(),
  42. })
  43. return
  44. }
  45. c.JSON(http.StatusOK, gin.H{
  46. "success": true,
  47. "message": "",
  48. "data": gin.H{
  49. "deleted": deleted,
  50. },
  51. })
  52. }