public static function fill_firstanalyses_cache(int $modelid, ?int $analysableid = null) {
        global $DB;

        // Using composed keys instead of cache $identifiers because of MDL-65358.
        $primarykey = $DB->sql_concat($modelid, "'_'", 'analysableid');
        $sql = "SELECT $primarykey AS id, MIN(firstanalysis) AS firstanalysis
                  FROM {analytics_used_analysables} aua
                 WHERE modelid = :modelid";
        $params = ['modelid' => $modelid];

        if ($analysableid) {
            $sql .= " AND analysableid = :analysableid";
            $params['analysableid'] = $analysableid;
        }

        $sql .= " GROUP BY modelid, analysableid ORDER BY analysableid";

        $firstanalyses = $DB->get_records_sql($sql, $params);
        if ($firstanalyses) {
            $cache = \cache::make('core', 'modelfirstanalyses');

            $firstanalyses = array_map(function($record) {
                return $record->firstanalysis;
            }, $firstanalyses);

            $cache->set_many($firstanalyses);
        }

        return $firstanalyses;
    }