Skip to content

Commit

Permalink
✨ feat(api): 增加数据库缓存信息API接口
Browse files Browse the repository at this point in the history
  • Loading branch information
TIANLI0 committed Sep 17, 2023
1 parent acd2981 commit c3c3edd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func main() {
api.POST("/clear_all_cache", clearAllcache)
api.GET("/get_advance", getAdvance)
api.POST("/set_advance", setAdvance)
api.GET("/cache_info", getCacheInfo)
}

// 设置请求处理函数
Expand Down
19 changes: 19 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,22 @@ func clearAllcache(c *gin.Context) {

c.JSON(http.StatusOK, gin.H{"message": "所有缓存已清除"})
}

func getCacheInfo(c *gin.Context) {
keys, err := redisClient.Keys("*:content-type").Result()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "无法获取缓存信息"})
return
}

cacheInfo := make(map[string]int)
for _, key := range keys {
contentType := strings.Split(redisClient.Get(key).Val(), ";")[0]
cacheInfo[contentType]++
}

// 获取数据库大小
dbSize := redisClient.DBSize().Val() / 1024 / 1024.0

c.JSON(http.StatusOK, gin.H{"cache_info": cacheInfo, "db_size": dbSize})
}

0 comments on commit c3c3edd

Please sign in to comment.