Cacheforpg (#1415)
* Added cache on PG query with results + Add a log info to say that ES is enabled * Added log when ES is disabled too * Added log info when cache is retrieved + moved notnull set variable with others * Fix travis test :')
Cette révision appartient à :
Parent
171683d314
révision
5bb45bbd75
4 fichiers modifiés avec 16 ajouts et 8 suppressions
3
main.go
3
main.go
|
@ -87,7 +87,10 @@ func main() {
|
|||
log.Fatal(err.Error())
|
||||
}
|
||||
if config.Get().Search.EnableElasticSearch {
|
||||
log.Info("ES Enabled in Config")
|
||||
models.ElasticSearchClient, _ = models.ElasticSearchInit()
|
||||
} else {
|
||||
log.Info("ES is disabled in Config")
|
||||
}
|
||||
err = publicSettings.InitI18n(config.Get().I18n, cookies.NewCurrentUserRetriever())
|
||||
if err != nil {
|
||||
|
|
|
@ -73,11 +73,12 @@ func ByQuery(c *gin.Context, pagenum int, withUser bool, deleted bool, hidden bo
|
|||
torrentParam.Hidden = hidden
|
||||
torrentParam.Full = withUser
|
||||
torrentParam.Deleted = deleted
|
||||
if found, ok := cache.C.Get(torrentParam.Identifier()); ok {
|
||||
log.Infof("Retrieve results from Cache in %s", torrentParam.Identifier())
|
||||
torrentCache := found.(*TorrentCache)
|
||||
return torrentParam, torrentCache.Torrents, torrentCache.Count, nil
|
||||
}
|
||||
if config.Get().Search.EnableElasticSearch && models.ElasticSearchClient != nil && !deleted {
|
||||
if found, ok := cache.C.Get(torrentParam.Identifier()); ok {
|
||||
torrentCache := found.(*TorrentCache)
|
||||
return torrentParam, torrentCache.Torrents, torrentCache.Count, nil
|
||||
}
|
||||
tor, totalHits, err := torrentParam.FindES(c, models.ElasticSearchClient)
|
||||
// If there are results no errors from ES search we use the ES client results
|
||||
if totalHits > 0 && err == nil {
|
||||
|
@ -93,6 +94,9 @@ func ByQuery(c *gin.Context, pagenum int, withUser bool, deleted bool, hidden bo
|
|||
// We fallback to PG, if ES gives error or no results or if ES is disabled in config or if deleted search is enabled
|
||||
log.Errorf("Falling back to postgresql query")
|
||||
tor, totalHits, err := torrentParam.FindDB(c)
|
||||
if totalHits > 0 && err == nil {
|
||||
cache.C.Set(torrentParam.Identifier(), &TorrentCache{tor, int(totalHits)}, 5*time.Minute)
|
||||
}
|
||||
return torrentParam, tor, int(totalHits), err
|
||||
}
|
||||
|
||||
|
|
|
@ -148,6 +148,10 @@ func (p *TorrentParam) FromRequest(c *gin.Context) {
|
|||
// Parse the sorting mode of the result from the "sort" argument in url
|
||||
p.Sort.Parse(c.Query("sort"))
|
||||
|
||||
// Set NoNull to improve pg query
|
||||
if p.Sort == Date {
|
||||
p.NotNull = p.Sort.ToDBField() + " IS NOT NULL"
|
||||
}
|
||||
// Category in which you have to search
|
||||
// Parse the categories from the "c" argument in url
|
||||
p.Category = ParseCategories(c.Query("c"))
|
||||
|
@ -437,9 +441,6 @@ func (p *TorrentParam) toDBQuery(c *gin.Context) *Query {
|
|||
*/
|
||||
func (p *TorrentParam) FindDB(c *gin.Context) ([]models.Torrent, int64, error) {
|
||||
orderBy := p.Sort.ToDBField()
|
||||
if p.Sort == Date {
|
||||
p.NotNull = p.Sort.ToDBField() + " IS NOT NULL"
|
||||
}
|
||||
query := p.toDBQuery(c)
|
||||
orderBy += " "
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ func TestTorrentParam_Identifier(t *testing.T) {
|
|||
func TestTorrentParam_FromRequest(t *testing.T) {
|
||||
torrentParam := &TorrentParam{}
|
||||
assert := assert.New(t)
|
||||
defTorrent := &TorrentParam{Sort: 2, Max: maxType(config.Get().Navigation.TorrentsPerPage)}
|
||||
defTorrent := &TorrentParam{Sort: 2, Max: maxType(config.Get().Navigation.TorrentsPerPage), NotNull: "date IS NOT NULL"}
|
||||
|
||||
c := mockRequest(t, "/?")
|
||||
torrentParam.FromRequest(c)
|
||||
|
|
Référencer dans un nouveau ticket