Merge pull request #214 from majestrate/make-rss-fast-again
make rss fast again
Cette révision appartient à :
révision
bbffb24ae8
4 fichiers modifiés avec 32 ajouts et 6 suppressions
1
main.go
1
main.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
|
||||
"github.com/nicksnyder/go-i18n/i18n"
|
||||
|
||||
"github.com/ewhal/nyaa/config"
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
func RssHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
_, torrents, _, err := search.SearchByQuery(r, 1)
|
||||
_, torrents, err := search.SearchByQueryNoCount(r, 1)
|
||||
if err != nil {
|
||||
util.SendError(w, err, 400)
|
||||
return
|
||||
|
|
|
@ -67,7 +67,17 @@ func GetTorrentById(id string) (model.Torrents, error) {
|
|||
return torrent, nil
|
||||
}
|
||||
|
||||
func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, err error) {
|
||||
torrents, _, err = getTorrentsOrderBy(parameters, orderBy, limit, offset, false)
|
||||
return
|
||||
}
|
||||
|
||||
func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, count int, err error) {
|
||||
torrents, count, err = getTorrentsOrderBy(parameters, orderBy, limit, offset, true)
|
||||
return
|
||||
}
|
||||
|
||||
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (torrents []model.Torrents, count int, err error) {
|
||||
var conditionArray []string
|
||||
if strings.HasPrefix(orderBy, "filesize") {
|
||||
// torrents w/ NULL filesize fuck up the sorting on postgres
|
||||
|
@ -81,11 +91,12 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
|
|||
params = parameters.Params
|
||||
}
|
||||
conditions := strings.Join(conditionArray, " AND ")
|
||||
err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error
|
||||
if err != nil {
|
||||
return
|
||||
if countAll {
|
||||
err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Vulnerable to injections. Use query builder.
|
||||
|
||||
// build custom db query for performance reasons
|
||||
|
|
|
@ -57,6 +57,16 @@ type SearchParam struct {
|
|||
}
|
||||
|
||||
func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, count int, err error) {
|
||||
search, tor, count, err = searchByQuery(r, pagenum, true)
|
||||
return
|
||||
}
|
||||
|
||||
func SearchByQueryNoCount(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, err error) {
|
||||
search, tor, _, err = searchByQuery(r, pagenum, false)
|
||||
return
|
||||
}
|
||||
|
||||
func searchByQuery(r *http.Request, pagenum int, countAll bool) (search SearchParam, tor []model.Torrents, count int, err error) {
|
||||
max, err := strconv.ParseUint(r.URL.Query().Get("max"), 10, 32)
|
||||
if err != nil {
|
||||
err = nil
|
||||
|
@ -180,6 +190,10 @@ func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []mode
|
|||
|
||||
parameters.Conditions = strings.Join(conditions[:], " AND ")
|
||||
log.Infof("SQL query is :: %s\n", parameters.Conditions)
|
||||
tor, count, err = torrentService.GetTorrentsOrderBy(¶meters, order_by, int(search.Max), int(search.Max)*(pagenum-1))
|
||||
if countAll {
|
||||
tor, count, err = torrentService.GetTorrentsOrderBy(¶meters, order_by, int(search.Max), int(search.Max)*(pagenum-1))
|
||||
} else {
|
||||
tor, err = torrentService.GetTorrentsOrderByNoCount(¶meters, order_by, int(search.Max), int(search.Max)*(pagenum-1))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket