public function reset_caches($tablenames = null) {
        if (!empty($tablenames)) {
            $dbmetapurged = false;
            foreach ($tablenames as $tablename) {
                if ($this->temptables->is_temptable($tablename)) {
                    $this->get_temp_tables_cache()->delete($tablename);
                } else if ($dbmetapurged === false) {
                    $this->tables = null;
                    $this->get_metacache()->purge();
                    $this->metacache = null;
                    $dbmetapurged = true;
                }
            }
        } else {
            $this->get_temp_tables_cache()->purge();
            $this->tables = null;
            // Purge MUC as well.
            $this->get_metacache()->purge();
            $this->metacache = null;
        }
    }