From 92ebc1f01bed96a028c4e1a6e33523c8a454163c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 30 Jul 2019 21:05:30 +0200 Subject: [PATCH 1/6] Add support for global cache (#78) --- composer.json | 3 ++- src/GlobalCacheFacade.php | 13 ++++++++++ src/TenancyServiceProvider.php | 5 ++++ tests/GlobalCacheTest.php | 43 ++++++++++++++++++++++++++++++++++ tests/TestCase.php | 3 ++- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/GlobalCacheFacade.php create mode 100644 tests/GlobalCacheTest.php diff --git a/composer.json b/composer.json index e6fc6818..6e55ab95 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "Stancl\\Tenancy\\TenancyServiceProvider" ], "aliases": { - "Tenancy": "Stancl\\Tenancy\\TenancyFacade" + "Tenancy": "Stancl\\Tenancy\\TenancyFacade", + "GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade" } } }, diff --git a/src/GlobalCacheFacade.php b/src/GlobalCacheFacade.php new file mode 100644 index 00000000..ba2f8fad --- /dev/null +++ b/src/GlobalCacheFacade.php @@ -0,0 +1,13 @@ +app->singleton(Seed::class, function ($app) { return new Seed($app['db'], $app[DatabaseManager::class]); }); + + $this->app->bind('globalCache', function ($app) { + return new CacheManager($app); + }); } } diff --git a/tests/GlobalCacheTest.php b/tests/GlobalCacheTest.php new file mode 100644 index 00000000..e67a0c73 --- /dev/null +++ b/tests/GlobalCacheTest.php @@ -0,0 +1,43 @@ +assertSame(null, cache('foo')); + GlobalCache::put(['foo' => 'bar'], 1); + $this->assertSame('bar', GlobalCache::get('foo')); + + tenant()->create('foo.localhost'); + tenancy()->init('foo.localhost'); + $this->assertSame('bar', GlobalCache::get('foo')); + + GlobalCache::put(['abc' => 'xyz'], 1); + cache(['def' => 'ghi'], 1); + $this->assertSame('ghi', cache('def')); + + tenancy()->end(); + $this->assertSame('xyz', GlobalCache::get('abc')); + $this->assertSame('bar', GlobalCache::get('foo')); + $this->assertSame(null, cache('def')); + + tenant()->create('bar.localhost'); + tenancy()->init('bar.localhost'); + $this->assertSame('xyz', GlobalCache::get('abc')); + $this->assertSame('bar', GlobalCache::get('foo')); + $this->assertSame(null, cache('def')); + cache(['def' => 'xxx'], 1); + $this->assertSame('xxx', cache('def')); + + tenancy()->init('foo.localhost'); + $this->assertSame('ghi', cache('def')); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index fc250f6d..b9b9c20b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -98,7 +98,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase protected function getPackageAliases($app) { return [ - 'Tenancy' => \Stancl\Tenancy\TenancyFacade::class + 'Tenancy' => \Stancl\Tenancy\TenancyFacade::class, + 'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class, ]; } From f34e1e97ac1f6e6781e91b8aa047ad886d16aab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 30 Jul 2019 21:27:01 +0200 Subject: [PATCH 2/6] [1.6.0] [WIP] Fix cache persistence (#79) * Add cache persistence test * Change cache driver to redis --- phpunit.xml | 2 +- tests/CacheManagerTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 28aa16ad..26f7e0b2 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -24,7 +24,7 @@ - + diff --git a/tests/CacheManagerTest.php b/tests/CacheManagerTest.php index 5d087ada..938699d2 100644 --- a/tests/CacheManagerTest.php +++ b/tests/CacheManagerTest.php @@ -73,4 +73,19 @@ class CacheManagerTest extends TestCase cache(['foo' => 'xyz'], 1); $this->assertSame('xyz', cache('foo')); } + + /** @test */ + public function cache_is_persisted() + { + tenant()->create('foo.localhost'); + tenancy()->init('foo.localhost'); + + cache(['foo' => 'bar'], 10); + $this->assertSame('bar', cache('foo')); + + tenancy()->end(); + + tenancy()->init('foo.localhost'); + $this->assertSame('bar', cache('foo')); + } } From 2757e8f76c562f305aed8e03ac68166745b566d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 30 Jul 2019 22:04:10 +0200 Subject: [PATCH 3/6] Fix tests --- tests/CacheManagerTest.php | 17 +++++++++++++++++ tests/GlobalCacheTest.php | 2 +- tests/TestCase.php | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/CacheManagerTest.php b/tests/CacheManagerTest.php index 938699d2..b8ce09f9 100644 --- a/tests/CacheManagerTest.php +++ b/tests/CacheManagerTest.php @@ -88,4 +88,21 @@ class CacheManagerTest extends TestCase tenancy()->init('foo.localhost'); $this->assertSame('bar', cache('foo')); } + + /** @test */ + public function cache_is_persisted_when_reidentification_is_used() + { + tenant()->create('foo.localhost'); + tenant()->create('bar.localhost'); + tenancy()->init('foo.localhost'); + + cache(['foo' => 'bar'], 10); + $this->assertSame('bar', cache('foo')); + + tenancy()->init('bar.localhost'); + tenancy()->end(); + + tenancy()->init('foo.localhost'); + $this->assertSame('bar', cache('foo')); + } } diff --git a/tests/GlobalCacheTest.php b/tests/GlobalCacheTest.php index e67a0c73..bdbcedfe 100644 --- a/tests/GlobalCacheTest.php +++ b/tests/GlobalCacheTest.php @@ -21,7 +21,7 @@ class GlobalCacheTest extends TestCase $this->assertSame('bar', GlobalCache::get('foo')); GlobalCache::put(['abc' => 'xyz'], 1); - cache(['def' => 'ghi'], 1); + cache(['def' => 'ghi'], 10); $this->assertSame('ghi', cache('def')); tenancy()->end(); diff --git a/tests/TestCase.php b/tests/TestCase.php index b9b9c20b..200c70b0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,6 +19,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase parent::setUp(); Redis::connection('tenancy')->flushdb(); + Redis::connection('cache')->flushdb(); if ($this->autoCreateTenant) { $this->createTenant(); @@ -77,6 +78,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase 's3', ], 'tenancy.redis.tenancy' => true, + 'tenancy.redis.prefixed_connections' => ['default'], 'tenancy.migrations_directory' => database_path('../migrations'), ]); } From 4d9c994c6caee5158a2318a2dbc349534c99461e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 30 Jul 2019 22:17:07 +0200 Subject: [PATCH 4/6] update changelog for v1.6.0 --- CHANGELOG-1.x.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG-1.x.md b/CHANGELOG-1.x.md index b3ced6e7..eefa74eb 100644 --- a/CHANGELOG-1.x.md +++ b/CHANGELOG-1.x.md @@ -1,5 +1,11 @@ # Release Notes for 1.x +## [v1.6.0 (2019-07-30)](https://github.com/stancl/tenancy/compare/v1.5.1...v1.6.0) + +### Added + +- `GlobalCache` facade [#78](https://github.com/stancl/tenancy/pull/78) + ## [v1.5.1 (2019-07-25)](https://github.com/stancl/tenancy/compare/v1.5.0...v1.5.1) ### Fixed From 3eb6c8cc29ed891def1fd6ab2f9a10990067b773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 30 Jul 2019 22:21:09 +0200 Subject: [PATCH 5/6] Mention global cache & storage in readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cf846078..15c7abe7 100644 --- a/README.md +++ b/README.md @@ -392,8 +392,9 @@ Connections listed in the `tenancy.redis.prefixed_connections` config array use ## Cache -Both `cache()` and `Cache` will use `Stancl\Tenancy\CacheManager`, which adds a tag (`prefix_base` + tenant UUID) to all methods called on it. +Both the `cache()` helper and the `Cache` facade will use `Stancl\Tenancy\CacheManager`, which adds a tag (`prefix_base` + tenant UUID) to all methods called on it. +If you need to store something in global, non-tenant cache, you can use the `GlobalCache` facade the same way you'd use the `Cache` facade. ## Filesystem/Storage @@ -475,6 +476,8 @@ Storage::disk('public')->put($filename, $data); Storage::disk('local')->put("public/$filename", $data); ``` +If you want to store something globally, simply create a new disk and *don't* add it to the `tenancy.filesystem.disks` config. + ## Artisan commands ``` From 2cf96423b068b228901446e4e1447a3d894ad513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 31 Jul 2019 23:07:39 +0200 Subject: [PATCH 6/6] Add link to website --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 15c7abe7..3f0fa9bb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Tenancy +# [stancl/tenancy](https://stancl.github.io/tenancy/) [![Laravel 5.7+](https://img.shields.io/badge/laravel-5.7+-red.svg)](https://laravel.com) [![Latest Stable Version](https://poser.pugx.org/stancl/tenancy/version)](https://packagist.org/packages/stancl/tenancy) @@ -20,7 +20,7 @@ You won't have to change a thing in your application's code.\*
Click to expand/collapse -- [Tenancy](#tenancy) +- [stancl/tenancy](#stancltenancy) + [*A Laravel multi-database tenancy package that respects your code.*](#-a-laravel-multi-database-tenancy-package-that-respects-your-code-) - [Installation](#installation) + [Requirements](#requirements)