Skip to content

Commit cd6a3db

Browse files
qschmickroshangautam
authored andcommitted
refactor : cache table prefix
- store table prefix in cache to avoid multiple and frequent database calls
1 parent 8906377 commit cd6a3db

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/Providers/TotemServiceProvider.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class TotemServiceProvider extends ServiceProvider
2323
*/
2424
public function boot()
2525
{
26+
try {
27+
if (Totem::baseTableExists()) {
28+
$this->app->register(ConsoleServiceProvider::class);
29+
}
30+
} catch (\PDOException $ex) {
31+
// This will trigger if DB cannot be connected to
32+
Log::error($ex->getMessage());
33+
}
2634
$this->registerResources();
2735
$this->defineAssetPublishing();
2836

@@ -69,15 +77,6 @@ public function register()
6977
$this->app->register(TotemRouteServiceProvider::class);
7078
$this->app->register(TotemEventServiceProvider::class);
7179
$this->app->register(TotemFormServiceProvider::class);
72-
73-
try {
74-
if (Totem::baseTableExists()) {
75-
$this->app->register(ConsoleServiceProvider::class);
76-
}
77-
} catch (\PDOException $ex) {
78-
// This will trigger if DB cannot be connected to
79-
Log::error($ex->getMessage());
80-
}
8180
}
8281

8382
/**

src/Totem.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Studio\Totem;
44

55
use Closure;
6+
use Illuminate\Support\Facades\Cache;
67
use Illuminate\Support\Facades\Schema;
78
use Illuminate\Support\Facades\Artisan;
89
use Symfony\Component\Console\Command\Command;
@@ -87,6 +88,16 @@ public static function getCommands()
8788
*/
8889
public static function baseTableExists() : bool
8990
{
90-
return Schema::connection(TOTEM_DATABASE_CONNECTION)->hasTable(TOTEM_TABLE_PREFIX.'tasks');
91+
if (Cache::get('totem.table.'.TOTEM_TABLE_PREFIX.'tasks')) {
92+
return true;
93+
}
94+
95+
if (Schema::hasTable(TOTEM_TABLE_PREFIX.'tasks')) {
96+
Cache::forever('totem.table.'.TOTEM_TABLE_PREFIX.'tasks', true);
97+
98+
return true;
99+
}
100+
101+
return false;
91102
}
92103
}

0 commit comments

Comments
 (0)