Added cache for ES request
Cette révision appartient à :
Parent
26ad505a41
révision
a4ac7ede9a
1 fichiers modifiés avec 10 ajouts et 5 suppressions
|
@ -79,18 +79,23 @@ func ByQueryNoHidden(c *gin.Context, pagenum int) (search structs.TorrentParam,
|
||||||
// pagenum is extracted from request in .FromRequest()
|
// pagenum is extracted from request in .FromRequest()
|
||||||
// elasticsearch always provide a count to how many hits
|
// elasticsearch always provide a count to how many hits
|
||||||
// deleted is unused because es doesn't index deleted torrents
|
// deleted is unused because es doesn't index deleted torrents
|
||||||
func byQuery(c *gin.Context, pagenum int, countAll bool, withUser bool, deleted bool, hidden bool) (
|
func byQuery(c *gin.Context, pagenum int, countAll bool, withUser bool, deleted bool, hidden bool) (structs.TorrentParam, []models.Torrent, int, error) {
|
||||||
search structs.TorrentParam, tor []models.Torrent, count int, err error,
|
var err error
|
||||||
) {
|
tor := []models.Torrent{}
|
||||||
if models.ElasticSearchClient != nil {
|
if models.ElasticSearchClient != nil {
|
||||||
var torrentParam structs.TorrentParam
|
var torrentParam structs.TorrentParam
|
||||||
torrentParam.FromRequest(c)
|
torrentParam.FromRequest(c)
|
||||||
torrentParam.Offset = uint32(pagenum)
|
torrentParam.Offset = uint32(pagenum)
|
||||||
torrentParam.Hidden = hidden
|
torrentParam.Hidden = hidden
|
||||||
torrentParam.Full = withUser
|
torrentParam.Full = withUser
|
||||||
totalHits, torrents, err := torrentParam.Find(models.ElasticSearchClient)
|
if found, ok := cache.C.Get(torrentParam.Identifier()); ok {
|
||||||
|
tor = found.([]models.Torrent)
|
||||||
|
return torrentParam, tor, len(tor), nil
|
||||||
|
}
|
||||||
|
totalHits, tor, err := torrentParam.Find(models.ElasticSearchClient)
|
||||||
|
cache.C.Set(torrentParam.Identifier(), tor, 5*time.Minute)
|
||||||
// Convert back to non-json torrents
|
// Convert back to non-json torrents
|
||||||
return torrentParam, torrents, int(totalHits), err
|
return torrentParam, tor, int(totalHits), err
|
||||||
}
|
}
|
||||||
log.Errorf("Unable to create elasticsearch client: %s", err)
|
log.Errorf("Unable to create elasticsearch client: %s", err)
|
||||||
log.Errorf("Falling back to postgresql query")
|
log.Errorf("Falling back to postgresql query")
|
||||||
|
|
Référencer dans un nouveau ticket