You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Locking occurs when a new session is created, and ensures no race conditions occur when a session expires.
122
+
Locking uses a [`Semaphore`][sem-lock] lock when `sysvmsg`, `sysvsem`, and `sysvshm` extensions are enabled, and a
123
+
[`Flock`][flock-lock] lock otherwise. To configure a custom lock, supply a class implementing
124
+
[`LockInterface`][lock-interface] when calling `Instance::database`. Here's an example which encorporates the
125
+
[Symfony Lock component][symfony-lock]:
126
+
127
+
```php
128
+
use Google\Cloud\Core\Lock\LockInterface;
129
+
use Google\Cloud\Spanner\SpannerClient;
130
+
use Symfony\Component\Lock\LockFactory;
131
+
use Symfony\Component\Lock\SharedLockInterface;
132
+
use Symfony\Component\Lock\Store\SemaphoreStore;
133
+
134
+
// Available by running `composer install symfony/lock`
135
+
$store = new SemaphoreStore();
136
+
$factory = new LockFactory($store);
137
+
138
+
// Create an adapter for Symfony's SharedLockInterface and Google's LockInterface
139
+
$lock = new class ($factory->createLock($databaseId)) implements LockInterface {
140
+
public function __construct(private SharedLockInterface $lock) {
141
+
}
142
+
143
+
public function acquire(array $options = []) {
144
+
return $this->lock->acquire()
145
+
}
146
+
147
+
public function release() {
148
+
return $this->lock->acquire()
149
+
}
150
+
151
+
public function synchronize(callable $func, array $options = []) {
152
+
if ($this->lock->acquire($options['blocking'] ?? true)) {
153
+
return $func();
154
+
}
155
+
}
56
156
}
157
+
158
+
// Configure our custom lock on our database using the "lock" option
159
+
$spanner = new SpannerClient();
160
+
$database = $spanner
161
+
->instance($instanceId)
162
+
->database($databaseId, ['lock' => $lock]);
57
163
```
58
164
59
-
By using a cache implementation like `SysVCacheItemPool`, you can share the cached sessions among multiple processes, so that for example, you can warmup the session upon the server startup, then all the other PHP processes will benefit from the warmed up sessions.
Copy file name to clipboardExpand all lines: Spanner/composer.json
+20-1Lines changed: 20 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,9 @@
18
18
"erusev/parsedown": "^1.6",
19
19
"google/cloud-pubsub": "^2.0",
20
20
"dg/bypass-finals": "^1.7",
21
-
"dms/phpunit-arraysubset-asserts": "^0.5.0"
21
+
"dms/phpunit-arraysubset-asserts": "^0.5.0",
22
+
"symfony/cache": "^6.4",
23
+
"symfony/process": "^6.4"
22
24
},
23
25
"suggest": {
24
26
"ext-protobuf": "Provides a significant increase in throughput over the pure PHP protobuf implementation. See https://cloud.google.com/php/grpc for installation instructions.",
0 commit comments