google / cloud-spanner
Cloud Spanner Client for PHP
Installs: 507 938
Dependents: 5
Suggesters: 0
Security: 0
Stars: 20
Watchers: 51
Forks: 10
pkg:composer/google/cloud-spanner
Requires
- php: ^8.1
- ext-grpc: *
- google/cloud-core: ^1.68
- google/gax: ^1.38.0
Requires (Dev)
- dg/bypass-finals: ^1.7
- dms/phpunit-arraysubset-asserts: ^0.5.0
- erusev/parsedown: ^1.6
- google/cloud-pubsub: ^2.0
- phpdocumentor/reflection: ^5.3.3||^6.0
- phpdocumentor/reflection-docblock: ^5.3
- phpspec/prophecy-phpunit: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: 3.*
- symfony/cache: ^6.4
- symfony/process: ^6.4
Suggests
- ext-protobuf: Provides a significant increase in throughput over the pure PHP protobuf implementation. See https://cloud.google.com/php/grpc for installation instructions.
- brick/math: Perform arithmetic on NUMERIC values.
- dev-main
- v2.0.0-RC1
- v1.106.0
- v1.105.1
- v1.105.0
- v1.104.1
- v1.104.0
- v1.103.0
- v1.102.0
- v1.101.0
- v1.100.1
- v1.100.0
- v1.99.0
- v1.98.0
- v1.97.0
- v1.96.0
- v1.95.0
- v1.94.0
- v1.93.1
- v1.93.0
- v1.92.1
- v1.92.0
- v1.91.0
- v1.90.0
- v1.89.0
- v1.88.0
- v1.87.0
- v1.86.0
- v1.85.0
- v1.84.0
- v1.83.0
- v1.82.0
- v1.81.0
- v1.80.0
- v1.79.0
- v1.78.0
- v1.77.0
- v1.76.1
- v1.76.0
- v1.75.2
- v1.75.1
- v1.75.0
- v1.74.0
- v1.73.0
- v1.72.0
- v1.71.0
- v1.70.0
- v1.69.0
- v1.68.0
- v1.67.0
- v1.65.0
- v1.64.0
- v1.63.2
- v1.63.1
- v1.63.0
- v1.62.1
- v1.62.0
- v1.61.0
- v1.60.0
- v1.59.0
- v1.58.4
- v1.58.3
- v1.58.2
- v1.58.1
- v1.58.0
- v1.57.0
- v1.56.0
- v1.55.0
- v1.54.2
- v1.54.1
- v1.54.0
- v1.53.0
- v1.52.0
- v1.51.2
- v1.51.1
- v1.51.0
- v1.50.0
- v1.49.2
- v1.49.1
- v1.49.0
- v1.48.2
- v1.48.1
- v1.48.0
- v1.47.0
- v1.46.3
- v1.46.2
- v1.46.1
- v1.46.0
- v1.45.0
- v1.44.0
- v1.43.0
- v1.42.0
- v1.41.0
- v1.40.0
- v1.39.0
- v1.38.0
- v1.37.0
- v1.36.0
- v1.35.0
- v1.34.0
- v1.33.0
- v1.32.3
- v1.32.2
- v1.32.1
- v1.32.0
- v1.31.0
- v1.30.1
- v1.30.0
- v1.29.2
- v1.29.1
- v1.29.0
- v1.28.0
- v1.27.1
- v1.27.0
- v1.26.0
- v1.25.0
- v1.24.1
- v1.24.0
- v1.23.0
- v1.22.0
- v1.21.0
- v1.20.0
- v1.19.0
- v1.18.0
- v1.17.0
- v1.16.1
- v1.16.0
- v1.15.6
- v1.15.5
- v1.15.4
- v1.15.3
- v1.15.2
- v1.15.1
- v1.15.0
- v1.14.0
- v1.13.0
- v1.12.0
- v1.11.7
- v1.11.6
- v1.11.5
- v1.11.4
- v1.11.3
- v1.11.2
- v1.11.1
- v1.11.0
- v1.10.1
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- v0.11.0
- v0.10.0
- v0.9.1
- v0.9.0
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- dev-uuid-spanner
This package is auto-updated.
Last update: 2025-10-20 23:06:02 UTC
README
Idiomatic PHP client for Cloud Spanner.
NOTE: This repository is part of Google Cloud PHP. Any support requests, bug reports, or development contributions should be directed to that project.
A fully managed, mission-critical, relational database service that offers transactional consistency at global scale, schemas, SQL (ANSI 2011 with extensions), and automatic, synchronous replication for high availability.
Installation
To begin, install the preferred dependency manager for PHP, Composer.
Now install this component:
$ composer require google/cloud-spanner
This component requires the gRPC extension. Please see our gRPC installation guide for more information on how to configure the extension.
Authentication
Please see our Authentication guide for more information on authenticating your client. Once authenticated, you'll be ready to start making requests.
Sample
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'];
Multiplexed Sessions
The V2 version of the Spanner Client Library for PHP uses Multiplexed Sessions. Multiplexed Sessions allow your application to create a large number of concurrent requests on a single session. Some advantages include reduced backend resource consumption due to a more straightforward session management protocol, and less management as sessions no longer require cleanup after use or keep-alive requests when idle.
Session Caching
The session cache is configured with a default cache which uses the PSR-6 compatible SysvCacheItemPool
when the sysvshm extension is enabled, and FileSystemCacheItemPool when sysvshm is not
available. This ensures that your processes share a single multiplex session for each database and creator role.
To change the default cache pool, use the option cacheItemPool when instantiating your Spanner client:
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);
This can also be passed in as an option to the instance or database methods:
$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]);
Refreshing Sessions
Sessions will refresh synchronously every 7 days. You can use this script to refresh the session asynchronously, in to avoid latency in your application (recommended every ~24 hours):
// 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();
Session Locking
Locking occurs when a new session is created, and ensures no race conditions occur when a session expires.
Locking uses a Semaphore lock when sysvmsg, sysvsem, and sysvshm extensions are enabled, and a
Flock lock otherwise. To configure a custom lock, supply a class implementing
LockInterface when calling Instance::database. Here's an example which encorporates the
Symfony Lock component:
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]);
Debugging
Please see our Debugging guide for more information about the debugging tools.
Version
This component is considered GA (generally available). As such, it will not introduce backwards-incompatible changes in any minor or patch releases. We will address issues and requests with the highest priority.
Next Steps
- Understand the official documentation.
- Take a look at in-depth usage samples.