From 5bb45bbd753c79aa50ef35c7795ad93ef47401ff Mon Sep 17 00:00:00 2001 From: akuma06 Date: Mon, 28 Aug 2017 01:15:57 +0200 Subject: [PATCH] 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 :') --- main.go | 3 +++ utils/search/search.go | 12 ++++++++---- utils/search/torrentParam.go | 7 ++++--- utils/search/torrentParam_test.go | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 46ea404f..a92e7cb1 100644 --- a/main.go +++ b/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 { diff --git a/utils/search/search.go b/utils/search/search.go index d7e221f3..12067c24 100644 --- a/utils/search/search.go +++ b/utils/search/search.go @@ -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 } diff --git a/utils/search/torrentParam.go b/utils/search/torrentParam.go index 019e8e31..2d20a169 100644 --- a/utils/search/torrentParam.go +++ b/utils/search/torrentParam.go @@ -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 += " " diff --git a/utils/search/torrentParam_test.go b/utils/search/torrentParam_test.go index c47d1ff6..d96e8727 100644 --- a/utils/search/torrentParam_test.go +++ b/utils/search/torrentParam_test.go @@ -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)