Skip to content

Commit

Permalink
Add support for global cache (archtechx#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl authored Jul 30, 2019
1 parent d0d1f69 commit 92ebc1f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"Stancl\\Tenancy\\TenancyServiceProvider"
],
"aliases": {
"Tenancy": "Stancl\\Tenancy\\TenancyFacade"
"Tenancy": "Stancl\\Tenancy\\TenancyFacade",
"GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade"
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions src/GlobalCacheFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Stancl\Tenancy;

use Illuminate\Support\Facades\Facade;

class GlobalCacheFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'globalCache';
}
}
5 changes: 5 additions & 0 deletions src/TenancyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Stancl\Tenancy\Commands\TenantList;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Interfaces\ServerConfigManager;
use Illuminate\Cache\CacheManager;

class TenancyServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -69,5 +70,9 @@ public function register()
$this->app->singleton(Seed::class, function ($app) {
return new Seed($app['db'], $app[DatabaseManager::class]);
});

$this->app->bind('globalCache', function ($app) {
return new CacheManager($app);
});
}
}
43 changes: 43 additions & 0 deletions tests/GlobalCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Stancl\Tenancy\Tests;

use GlobalCache;

class GlobalCacheTest extends TestCase
{
public $autoCreateTenant = false;
public $autoInitTenancy = false;

/** @test */
public function global_cache_manager_stores_data_in_global_cache()
{
$this->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'));
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ protected function getPackageProviders($app)
protected function getPackageAliases($app)
{
return [
'Tenancy' => \Stancl\Tenancy\TenancyFacade::class
'Tenancy' => \Stancl\Tenancy\TenancyFacade::class,
'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class,
];
}

Expand Down

0 comments on commit 92ebc1f

Please sign in to comment.