Skip to content

Commit

Permalink
feat: allow custom cache path (#142)
Browse files Browse the repository at this point in the history
* feat: allow custom cache path

* add changelog
  • Loading branch information
taka-oyama committed Nov 17, 2023
1 parent face8e7 commit c0860e2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Added
- Deprecation warnings to `Connection`'s methods `cursorWithTimestampBound` `selectWithTimestampBound` `selectOneWithTimestampBound`. Use `cursorWithOptions` `selectWithOptions` instead. (#122)
- `Connection` has new methods `selectWithOptions` `cursorWithOptions` which allows spanner specific options to be set for each query. (#122)
- `session:list` command can now show and filter by labels. (#134)
- Allow custom cache path (#142)

Changed
- [Breaking] Match `Query\Builder::forceIndex()` behavior with laravel's (`forceIndex` property no longer exists). (#114)
Expand Down
11 changes: 8 additions & 3 deletions src/SpannerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public function boot(): void
*/
protected function createSpannerConnection(array $config): Connection
{
$cache = $this->getCacheAdapter($config['name']);
$cache = $this->getCacheAdapter(
$config['name'],
$config['cache_path'] ?? null,
);

return new Connection(
$config['instance'],
Expand All @@ -88,11 +91,13 @@ protected function parseConfig(array $config, string $name): array
}

/**
* @param string $namespace
* @param string|null $path
* @return AdapterInterface
*/
protected function getCacheAdapter(string $namespace): AdapterInterface
protected function getCacheAdapter(string $namespace, ?string $path): AdapterInterface
{
$path = $this->app->storagePath('framework/spanner');
$path ??= $this->app->storagePath('framework/spanner');
return new FilesystemAdapter($namespace, 0, $path);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/SpannerServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright 2019 Colopl Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Colopl\Spanner\Tests;

use Illuminate\Database\DatabaseManager;

class SpannerServiceProviderTest extends TestCase
{
public function test_change_cache_path(): void
{
$newPath = '/tmp/spanner';
config()->set('database.connections.main.cache_path', $newPath);
$this->beforeApplicationDestroyed(static fn () => shell_exec('rm -rf {$newPath}'));

/** @var DatabaseManager $db */
$db = $this->app->make('db');

$db->connection('main')->query()->select('SELECT 1');

$this->assertDirectoryExists($newPath);
}
}

0 comments on commit c0860e2

Please sign in to comment.