protected static function set_backup_ids_cached($restoreid, $itemname, $itemid, $extrarecord) {
        global $DB;

        $key = "$itemid $itemname $restoreid";

        $record = array(
            'backupid' => $restoreid,
            'itemname' => $itemname,
            'itemid'   => $itemid,
        );

        // If record is not cached then add one.
        if (!isset(self::$backupidsexist[$key])) {
            // If we have this record in db, then just update this.
            if ($existingrecord = $DB->get_record('backup_ids_temp', $record)) {
                self::$backupidsexist[$key] = $existingrecord->id;
                self::$backupidsexistsize--;
                self::update_backup_cached_record($record, $extrarecord, $key, $existingrecord);
            } else {
                // Add new record to cache and db.
                $recorddefault = array (
                    'newitemid' => 0,
                    'parentitemid' => null,
                    'info' => null);
                $record = array_merge($record, $recorddefault, $extrarecord);
                $record['id'] = $DB->insert_record('backup_ids_temp', $record);
                self::$backupidsexist[$key] = $record['id'];
                self::$backupidsexistsize--;
                if (self::$backupidscachesize > 0) {
                    // Cache new records if we haven't got many yet.
                    self::$backupidscache[$key] = (object) $record;
                    self::$backupidscachesize--;
                }
            }
        } else {
            self::update_backup_cached_record($record, $extrarecord, $key);
        }
    }