PHP code example of hokoo / wp-lock

1. Go to this page and download the library: Download hokoo/wp-lock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hokoo / wp-lock example snippets


use iTRON\WP_Lock\WP_Lock;


use iTRON\WP_Lock\WP_Lock;

$lock = new WP_Lock( 'user:' . $user_id . ':balance' );

if ( ! $lock->acquire( WP_Lock::WRITE ) ) {
	throw new RuntimeException( 'Could not acquire the balance lock.' );
}

try {
	$balance = (float) get_user_meta( $user_id, 'balance', true );
	update_user_meta( $user_id, 'balance', $balance + $topup );
} finally {
	$lock->release();
}

public function acquire( $level = self::WRITE, $blocking = true, $expiration = 30 ): bool

use iTRON\WP_Lock\WP_Lock;
use iTRON\WP_Lock\WP_Lock_Backend_DB;

// Wait for at most 10 seconds and retry DB errors twice after the first attempt.
$backend = new WP_Lock_Backend_DB( 10.0, 2 );
$lock    = new WP_Lock( 'scheduled-import', $backend );

if ( $lock->lock_exists( WP_Lock::WRITE ) ) {
	// An exclusive lock currently exists.
}

interface WP_Lock_Backend {
	public function acquire( $id, $level, $blocking, $expiration ): bool;
	public function release( $id ): bool;
	public function exists( $id, $level ): bool;
}