/** * build_cache * * This attempts to reduce queries by asking for everything in the * browse all at once and storing it in the cache, this can help if the * db connection is the slow point. * @param int[] $song_ids * @return boolean */ public static function build_cache($song_ids, $limit_threshold = '') { if (!is_array($song_ids) || !count($song_ids)) { return false; } $idlist = '(' . implode(',', $song_ids) . ')'; // Callers might have passed array(false) because they are dumb if ($idlist == '()') { return false; } // Song data cache $sql = 'SELECT `song`.`id`, `file`, `catalog`, `album`, ' . '`year`, `artist`, `title`, `bitrate`, `rate`, ' . '`mode`, `size`, `time`, `track`, `played`, ' . '`song`.`enabled`, `update_time`, `tag_map`.`tag_id`, ' . '`mbid`, `addition_time`, `license`, `composer`, `user_upload` ' . 'FROM `song` LEFT JOIN `tag_map` ' . 'ON `tag_map`.`object_id`=`song`.`id` ' . "AND `tag_map`.`object_type`='song' "; if (AmpConfig::get('catalog_disable')) { $sql .= "LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` "; } $sql .= "WHERE `song`.`id` IN {$idlist} "; if (AmpConfig::get('catalog_disable')) { $sql .= "AND `catalog`.`enabled` = '1' "; } $db_results = Dba::read($sql); $artists = array(); $albums = array(); $tags = array(); while ($row = Dba::fetch_assoc($db_results)) { if (AmpConfig::get('show_played_times')) { $row['object_cnt'] = Stats::get_object_count('song', $row['id'], $limit_threshold); } parent::add_to_cache('song', $row['id'], $row); $artists[$row['artist']] = $row['artist']; $albums[$row['album']] = $row['album']; if ($row['tag_id']) { $tags[$row['tag_id']] = $row['tag_id']; } } Artist::build_cache($artists); Album::build_cache($albums); Tag::build_cache($tags); Tag::build_map_cache('song', $song_ids); Art::build_cache($albums); // If we're rating this then cache them as well if (AmpConfig::get('ratings')) { Rating::build_cache('song', $song_ids); } if (AmpConfig::get('userflags')) { Userflag::build_cache('song', $song_ids); } // Build a cache for the song's extended table $sql = "SELECT * FROM `song_data` WHERE `song_id` IN {$idlist}"; $db_results = Dba::read($sql); while ($row = Dba::fetch_assoc($db_results)) { parent::add_to_cache('song_data', $row['song_id'], $row); } return true; }