protected function get_index_from_key($key) {
        if (isset(self::$lockidcache[$key])) {
            return self::$lockidcache[$key];
        }

        $index = 0;
        $record = $this->db->get_record('lock_db', array('resourcekey' => $key));
        if ($record) {
            $index = $record->id;
        }

        if (!$index) {
            $record = new \stdClass();
            $record->resourcekey = $key;
            try {
                $index = $this->db->insert_record('lock_db', $record);
            } catch (\dml_exception $de) {
                // Race condition - never mind - now the value is guaranteed to exist.
                $record = $this->db->get_record('lock_db', array('resourcekey' => $key));
                if ($record) {
                    $index = $record->id;
                }
            }
        }

        if (!$index) {
            throw new \moodle_exception('Could not generate unique index for key');
        }

        self::$lockidcache[$key] = $index;
        return $index;
    }