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;
    }
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;
    }
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 ; }
This function generates the unique index for a specific lock key.
Once an index is assigned to a key, it never changes - so this is
statically cached.

@param string $key
@return int
@throws \moodle_exception
This function generates the unique index for a specific lock key . Once an index is assigned to a key it never changes - so this is statically cached .