PHP code example of google / cloud-spanner

1. Go to this page and download the library: Download google/cloud-spanner 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/ */

    

google / cloud-spanner example snippets


use Google\Cloud\Spanner\SpannerClient;

// Create a client.
$spannerClient = new SpannerClient();

$db = $spanner->connect('my-instance', 'my-database');

$userQuery = $db->execute('SELECT * FROM Users WHERE id = @id', [
    'parameters' => [
        'id' => $userId
    ]
]);

$user = $userQuery->rows()->current();

echo 'Hello ' . $user['firstName'];

use Google\Cloud\Spanner\SpannerClient;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
// available by running `composer install symfony/cache`
$fileCacheItemPool = new FilesystemAdapter();
// configure through SpannerClient constructor
$spanner = new SpannerClient(['cacheItemPool' => $fileCacheItemPool]);
$database = $spanner->instance($instanceId)->database($databaseId);

$spanner = new SpannerClient();
// configure through instance method
$database = $spanner
    ->instance($instanceId, ['cacheItemPool' => $fileCacheItemPool])
    ->database($databaseId);
// configure through database method
$database = $spanner
    ->instance($instanceId)
    ->database($databaseId, ['cacheItemPool' => $fileCacheItemPool]);

// If you are using a custom PSR-6 cache via the "cacheItemPool" client option in your
// application, you will need to supply a cache with the same configuration here in
// order to properly refresh the session.
$spanner = new SpannerClient();

$sessionCache = $spanner
    ->instance($instanceId)
    ->database($databaseId)
    ->session();

// this will force-refresh the session
$sessionCache->refresh();

use Google\Cloud\Core\Lock\LockInterface;
use Google\Cloud\Spanner\SpannerClient;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\SharedLockInterface;
use Symfony\Component\Lock\Store\SemaphoreStore;

// Available by running `composer install symfony/lock`
$store = new SemaphoreStore();
$factory = new LockFactory($store);

// Create an adapter for Symfony's SharedLockInterface and Google's LockInterface
$lock = new class ($factory->createLock($databaseId)) implements LockInterface {
    public function __construct(private SharedLockInterface $lock) {
    }

    public function acquire(array $options = []) {
        return $this->lock->acquire()
    }

    public function release() {
        return $this->lock->acquire()
    }

    public function synchronize(callable $func, array $options = []) {
        if ($this->lock->acquire($options['blocking'] ?? true)) {
            return $func();
        }
    }
}

// Configure our custom lock on our database using the "lock" option
$spanner = new SpannerClient();
$database = $spanner
    ->instance($instanceId)
    ->database($databaseId, ['lock' => $lock]);

use Google\Cloud\Spanner\SpannerClient;

$spanner = new SpannerClient([
    'enableBuiltInMetrics' => true,
    // optional: timeout in milliseconds for metric export calls
    'metricsTimeoutMillis' => 100 
]);