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 diff --git a/README.md b/README.md index cf846078..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) @@ -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 ``` diff --git a/composer.json b/composer.json index f9685541..a8d93299 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "Stancl\\Tenancy\\TenancyServiceProvider" ], "aliases": { - "Tenancy": "Stancl\\Tenancy\\TenancyFacade" + "Tenancy": "Stancl\\Tenancy\\TenancyFacade", + "GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade" } } }, 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/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/CacheManagerTest.php b/tests/CacheManagerTest.php index 5d087ada..b8ce09f9 100644 --- a/tests/CacheManagerTest.php +++ b/tests/CacheManagerTest.php @@ -73,4 +73,36 @@ 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')); + } + + /** @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 new file mode 100644 index 00000000..bdbcedfe --- /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'], 10); + $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..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'), ]); } @@ -98,7 +100,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, ]; }