Skip to content

Commit

Permalink
feat: use same cache adapter for auth and session pool (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Nov 17, 2023
1 parent b13c57f commit face8e7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 145 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Added
Changed
- [Breaking] Match `Query\Builder::forceIndex()` behavior with laravel's (`forceIndex` property no longer exists). (#114)
- [Breaking] SessionNotFoundErrorMode was removed and will always run clear session pool. (#132) (#130)
- [Breaking] Auth cache and Session pool now share the same file cache adapter (#139)

# v5.3.0 (2023-11-17)

Expand Down
112 changes: 0 additions & 112 deletions src/FileCacheAdapter.php

This file was deleted.

30 changes: 10 additions & 20 deletions src/SpannerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use Colopl\Spanner\Console\SessionsCommand;
use Colopl\Spanner\Console\WarmupCommand;
use Google\Cloud\Spanner\Session\CacheSessionPool;
use Google\Cloud\Spanner\Session\SessionPoolInterface;
use Illuminate\Database\DatabaseManager;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class SpannerServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -61,13 +61,15 @@ public function boot(): void
*/
protected function createSpannerConnection(array $config): Connection
{
$cache = $this->getCacheAdapter($config['name']);

return new Connection(
$config['instance'],
$config['database'],
$config['prefix'],
$config,
$this->createAuthCache($config['name']),
$this->createSessionPool($config['name'], $config['session_pool'] ?? [])
$cache,
new CacheSessionPool($cache, $config['session_pool'] ?? [])
);
}

Expand All @@ -86,24 +88,12 @@ protected function parseConfig(array $config, string $name): array
}

/**
* @param string $name
* @param array<string, mixed> $sessionPoolConfig
* @return SessionPoolInterface
*/
protected function createSessionPool(string $name, array $sessionPoolConfig): SessionPoolInterface
{
$cachePath = $this->app->storagePath(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner']));
return new CacheSessionPool(new FileCacheAdapter("{$name}-session", $cachePath), $sessionPoolConfig);
}

/**
* @param string $name
* @return CacheItemPoolInterface
* @return AdapterInterface
*/
protected function createAuthCache(string $name): CacheItemPoolInterface
protected function getCacheAdapter(string $namespace): AdapterInterface
{
$cachePath = $this->app->storagePath(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner']));
return new FileCacheAdapter("{$name}-auth", $cachePath);
$path = $this->app->storagePath('framework/spanner');
return new FilesystemAdapter($namespace, 0, $path);
}

protected function closeSessionAfterEachQueueJob(): void
Expand Down
12 changes: 6 additions & 6 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@ public function test_AuthCache_with_FileSystemAdapter(): void
$conn = $this->getDefaultConnection();
$conn->select('SELECT 1');

$outputPath = $this->app->storagePath("framework/spanner/{$conn->getName()}-auth");
$outputPath = $this->app->storagePath("framework/spanner");
self::assertFileExists($outputPath);
self::assertSame('0777', substr(sprintf('%o', fileperms(dirname($outputPath))), -4));
self::assertSame('0644', substr(sprintf('%o', fileperms($outputPath)), -4));
self::assertSame('0755', substr(sprintf('%o', fileperms(dirname($outputPath))), -4));
self::assertSame('0755', substr(sprintf('%o', fileperms($outputPath)), -4));
}

public function test_session_pool(): void
Expand All @@ -438,10 +438,10 @@ public function test_session_pool_with_FileSystemAdapter(): void
$conn = $this->getDefaultConnection();
$conn->select('SELECT 1');

$outputPath = $this->app->storagePath("framework/spanner/{$conn->getName()}-session");
$outputPath = $this->app->storagePath("framework/spanner");
self::assertFileExists($outputPath);
self::assertSame('0777', substr(sprintf('%o', fileperms(dirname($outputPath))), -4));
self::assertSame('0644', substr(sprintf('%o', fileperms($outputPath)), -4));
self::assertSame('0755', substr(sprintf('%o', fileperms(dirname($outputPath))), -4));
self::assertSame('0755', substr(sprintf('%o', fileperms($outputPath)), -4));
}

public function test_clearSessionPool(): void
Expand Down
11 changes: 5 additions & 6 deletions tests/Console/SessionsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function test_no_args(): void
->run();

$this->artisan('spanner:sessions')
->expectsOutputToContain('main contains 2 session(s).')
->expectsOutputToContain('alternative contains 2 session(s).')
->expectsOutputToContain('main contains 1 session(s).')
->expectsOutputToContain('alternative contains 1 session(s).')
->assertSuccessful()
->run();
}
Expand All @@ -81,7 +81,9 @@ public function test_with_args(): void

public function test_no_sessions_shows_no_table(): void
{
$this->artisan('spanner:sessions', ['connections' => 'main'])
$conn = $this->getDefaultConnection();

$this->artisan('spanner:sessions', ['connections' => $conn->getName()])
->expectsOutputToContain('main contains 0 session(s).')
->doesntExpectOutputToContain('Name')
->assertSuccessful()
Expand All @@ -91,7 +93,6 @@ public function test_no_sessions_shows_no_table(): void
public function test_sort(): void
{
$conn = $this->getDefaultConnection();
$this->setUpDatabaseOnce($conn);
$this->createSessions($conn, 2);

$list = $conn->listSessions()
Expand All @@ -112,7 +113,6 @@ public function test_sort(): void
public function test_sort_order(): void
{
$conn = $this->getDefaultConnection();
$this->setUpDatabaseOnce($conn);
$this->createSessions($conn, 2);

$list = $conn->listSessions()
Expand All @@ -133,7 +133,6 @@ public function test_sort_order(): void
public function test_sort_patterns(): void
{
$conn = $this->getDefaultConnection();
$this->setUpDatabaseOnce($conn);
$this->createSessions($conn, 1);

foreach (['Name', 'Created', 'LastUsed', 'Labels'] as $column) {
Expand Down
2 changes: 1 addition & 1 deletion tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
return [
'connections' => [
'main' => $conn,
'alternative' => $conn,
'alternative' => ['database' => $conn['database'] . '-alt'] + $conn,
],
'default' => 'main',
];

0 comments on commit face8e7

Please sign in to comment.