Example #1
0
 public static function getLastTorrentsDataProvider($count = 5, $forceRefresh = false)
 {
     $cacheKey = 'browse_torrents';
     $torrentsByTags = Yii::$app->cache->get($cacheKey);
     if (!$torrentsByTags) {
         $torrentsByTags = [];
         $torrentsIds = [];
         $tags = Category::$categoriesTags;
         foreach ($tags as $tag) {
             $query = new Query();
             $rows = $query->from(static::indexName())->match(new Expression(':match', [':match' => '@tags ' . Yii::$app->sphinx->escapeMatchValue($tag)]))->where(['deleted' => 0])->orderBy(['weight()' => SORT_DESC, 'id' => SORT_DESC])->limit($count)->addOptions(['ranker' => 'SPH04', 'max_matches' => 5]);
             $torrents = array_map(function ($item) {
                 return $item['id'];
             }, $rows->all());
             $torrentsIds = array_merge($torrentsIds, $torrents);
         }
         if (empty($torrentsIds)) {
             Yii::log('Empty last torrents ids', CLogger::LEVEL_WARNING);
         } else {
             $query = (new Torrent())->find()->with('scrapes')->where(['id' => $torrentsIds]);
             foreach ($query->all() as $torrent) {
                 $torrentsByTags[Category::getTorrentCategoryTag($torrent)][] = $torrent;
             }
         }
         Yii::$app->cache->set($cacheKey, $torrentsByTags, 600);
     }
     $result = array_map(function ($data) {
         return new ArrayDataProvider(['key' => 'id', 'allModels' => $data]);
     }, $torrentsByTags);
     return $result;
 }