Skip to content

Commit

Permalink
Added documentation for SysVCacheItemPool (#995)
Browse files Browse the repository at this point in the history
* Added some tips for lower latency to docs

* Fixed typo

* Document update for the code review
  • Loading branch information
Takashi Matsuo authored Apr 6, 2018
1 parent 2d92190 commit 9e76550
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,21 @@ $storage = new StorageClient([
]);
```

This library provides a PSR-6 implementation with the SystemV shared memory at `Google\Auth\Cache\SysVCacheItemPool`. This implementation is only available on *nix machines, but it's the one of the fastest implementations and you can share the cache among multiple processes. The following example shows how to use it.

```php
require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Spanner\SpannerClient;
use Google\Auth\Cache\SysVCacheItemPool;

$cache = new SysVCacheItemPool();

$spanner = new SpannerClient([
'authCache' => $cache
]);
```

## Versioning

This library follows [Semantic Versioning](http://semver.org/).
Expand Down
47 changes: 47 additions & 0 deletions Spanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,50 @@ NOTE: In addition to the gRPC extension, we recommend installing the protobuf ex
```
$ composer require google/cloud-spanner
```

## Session warmup

To issue a query against the Spanner service, the client library needs to request a session id from the server under the cover. This API call will add significant latency to your program. The Spanner client library provides a handy way to alleviate this problem by having a cached session pool.

For more details, see: https://github.com/GoogleCloudPlatform/google-cloud-php/blob/master/Spanner/src/Session/CacheSessionPool.php#L30

The following example shows how to use the `CacheSessionPool` with `SysVCacheItemPool` as well as how to configure a proper cache for authentication:

```php
require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Session\CacheSessionPool;
use Google\Auth\Cache\SysVCacheItemPool;

$authCache = new SysVCacheItemPool();
$sessionCache = new SysVCacheItemPool([
// Use a different project identifier for ftok than the default
'proj' => 'B'
]);

$spanner = new SpannerClient([
'authCache' => $authCache
]);

$sessionPool = new CacheSessionPool(
$sessionCache,
[
'minSession' => 10,
'maxSession' => 10 // Here it will create 10 sessions under the cover.
]
);

$database = $client->connect(
'my-instance',
'my-db',
[
'sessionPool' => $sessionPool
]
);
// `warmup` will actually create the sessions for the first time.
$sessionPool->warmup();

```

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.
16 changes: 11 additions & 5 deletions Spanner/src/Session/CacheSessionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
* This session pool implementation accepts a PSR-6 compatible cache
* implementation and utilizes it to store sessions between requests.
*
* We provide `Google\Auth\Cache\SysVCacheItemPool`, which is a fast PSR-6
* implementation in `google/auth` library. If your PHP has `sysvshm`
* extension enabled (most binary distributions have it compiled in), consider
* using it.
*
* Please note that when
* {@see Google\Cloud\Spanner\Session\CacheSessionPool::acquire()} is called at
* most only a single session is created. Due to this, it is possible to sit
Expand All @@ -56,12 +61,13 @@
* {@see Google\Cloud\Spanner\Session\CacheSessionPool::clear()} in order to
* delete any active sessions.
*
* Please note: While required for the session pool, a PSR-6 implementation is
* not included in this library. It will be neccesary to include a separate
* dependency to fulfill this requirement. The below example makes use of
* If you're on Windows, or your PHP doesn't have `sysvshm` extension,
* unfortunately you can not use `Google\Auth\Cache\SysVCacheItemPool`. In such
* cases, it will be neccesary to include a separate dependency to fulfill
* this requirement. The below example makes use of
* [Symfony's Cache Component](https://github.com/symfony/cache). For more
* implementations please see the
* [Packagist PHP Package Repository](https://packagist.org/providers/psr/cache-implementation).
* implementations please see the [Packagist PHP Package
* Repository](https://packagist.org/providers/psr/cache-implementation).
*
* Example:
* ```
Expand Down

0 comments on commit 9e76550

Please sign in to comment.