优化分页

This commit is contained in:
2026-04-12 00:02:32 +08:00
parent 4eaf134efc
commit eed4dfd1b5
6 changed files with 62 additions and 19 deletions
-7
View File
File diff suppressed because one or more lines are too long
+7
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -5,8 +5,8 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SESE 爬取管理</title> <title>SESE 爬取管理</title>
<script type="module" crossorigin src="/assets/index-B5dBfm7U.js"></script> <script type="module" crossorigin src="/assets/index-CbQDv6fc.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-t-HtfReR.css"> <link rel="stylesheet" crossorigin href="/assets/index-c8sW61xI.css">
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
+51 -8
View File
@@ -122,6 +122,29 @@ func (c *urlKeywordsCache) ListAll() []*urlKeywordsEntry {
return entries return entries
} }
// ListPage 返回分页缓存条目(按访问时间从旧到新,支持跳过头部条目)
func (c *urlKeywordsCache) ListPage(page, pageSize int) []*urlKeywordsEntry {
c.mu.RLock()
defer c.mu.RUnlock()
total := len(c.items)
offset := (page - 1) * pageSize
if offset >= total {
return []*urlKeywordsEntry{}
}
// 遍历到起始位置
elem := c.order.Front()
for i := 0; i < offset && elem != nil; i++ {
elem = elem.Next()
}
// 收集 pageSize 条
entries := make([]*urlKeywordsEntry, 0, pageSize)
for i := 0; i < pageSize && elem != nil; i++ {
entries = append(entries, elem.Value.(*urlKeywordsEntry))
elem = elem.Next()
}
return entries
}
// Server 是搜索 HTTP 服务器,同时内嵌收获服务(统一在同一端口)。 // Server 是搜索 HTTP 服务器,同时内嵌收获服务(统一在同一端口)。
type Server struct { type Server struct {
db *storage.DB db *storage.DB
@@ -908,7 +931,8 @@ func (s *Server) handleUrlKeywordsStats(w http.ResponseWriter, r *http.Request)
json.NewEncoder(w).Encode(resp) json.NewEncoder(w).Encode(resp)
} }
// handleUrlKeywordsList 返回所有缓存条目(按访问时间从旧到新 // handleUrlKeywordsList 返回缓存条目(支持分页
// Query params: page=1, page_size=50
func (s *Server) handleUrlKeywordsList(w http.ResponseWriter, r *http.Request) { func (s *Server) handleUrlKeywordsList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
@@ -922,7 +946,20 @@ func (s *Server) handleUrlKeywordsList(w http.ResponseWriter, r *http.Request) {
return return
} }
entries := s.urlKeywords.ListAll() page := 1
pageSize := 50
if p := r.URL.Query().Get("page"); p != "" {
if v, err := strconv.Atoi(p); err == nil && v > 0 {
page = v
}
}
if ps := r.URL.Query().Get("page_size"); ps != "" {
if v, err := strconv.Atoi(ps); err == nil && v > 0 && v <= 200 {
pageSize = v
}
}
entries := s.urlKeywords.ListPage(page, pageSize)
size, maxSize := s.urlKeywords.Stats() size, maxSize := s.urlKeywords.Stats()
// 转换为前端需要的格式 // 转换为前端需要的格式
@@ -937,13 +974,19 @@ func (s *Server) handleUrlKeywordsList(w http.ResponseWriter, r *http.Request) {
} }
resp := struct { resp := struct {
Items []map[string]any `json:"items"` // 缓存条目列表 Items []map[string]any `json:"items"` // 缓存条目列表
Size int `json:"size"` // 当前缓存的 URL 数量 Page int `json:"page"` // 当前
MaxSize int `json:"max_size"` // 缓存容量上限 PageSize int `json:"page_size"` // 每页数量
Total int `json:"total"` // 总数
Size int `json:"size"` // 当前缓存的 URL 数量(等于 Total)
MaxSize int `json:"max_size"` // 缓存容量上限
}{ }{
Items: items, Items: items,
Size: size, Page: page,
MaxSize: maxSize, PageSize: pageSize,
Total: size,
Size: size,
MaxSize: maxSize,
} }
json.NewEncoder(w).Encode(resp) json.NewEncoder(w).Encode(resp)