Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store agent in cache to improve performance #90

Merged
merged 8 commits into from
Sep 12, 2024
5 changes: 4 additions & 1 deletion agentTemplate/forest_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\AgentPHP\DatasourceEloquent\EloquentDatasource;
use ForestAdmin\LaravelForestAdmin\Providers\AgentProvider;

return static function () {
$defaultDB = config('database.default');
$forestAgent = app()->make(AgentFactory::class);
$forestAgent = AgentProvider::getAgentInstance();

$forestAgent->addDatasource(
new EloquentDatasource(config('database.connections.' . $defaultDB)),
);

$forestAgent->build();
};
3 changes: 2 additions & 1 deletion config/forest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
'permissionExpiration' => env('FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS', 300),
'cacheDir' => storage_path('framework/cache/data/forest'),
'schemaPath' => base_path() . '/.forestadmin-schema.json',
'disabledApcuCache' => true,
'projectDir' => base_path(),
];
];
5 changes: 4 additions & 1 deletion src/Commands/SendApimap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ForestAdmin\LaravelForestAdmin\Commands;

use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\AgentPHP\Agent\Facades\Cache;
use ForestAdmin\LaravelForestAdmin\Providers\AgentProvider;
use Illuminate\Console\Command;

class SendApimap extends Command
Expand All @@ -13,7 +15,8 @@ class SendApimap extends Command

public function handle()
{
app()->make(AgentFactory::class)->sendSchema();
Cache::flush();
AgentProvider::getAgentInstance()->sendSchema();

$this->info('✅ Apimap sent');
}
Expand Down
45 changes: 40 additions & 5 deletions src/Providers/AgentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,53 @@
namespace ForestAdmin\LaravelForestAdmin\Providers;

use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\AgentPHP\Agent\Facades\Cache;
use ForestAdmin\AgentPHP\Agent\Http\Router as AgentRouter;
use ForestAdmin\AgentPHP\Agent\Utils\Filesystem;
use ForestAdmin\LaravelForestAdmin\Http\Controllers\ForestController;
use Illuminate\Routing\RouteCollection;
use Illuminate\Support\ServiceProvider;
use Laravel\SerializableClosure\SerializableClosure;

class AgentProvider extends ServiceProvider
{
protected RouteCollection $routes;

public function boot()
{
if ($this->forestIsPlanted()) {
$this->app->instance(AgentFactory::class, new AgentFactory(config('forest')));
if ($this->forestIsPlanted() && (request()->getMethod() !== 'OPTIONS')) {
// set cache file configuration
$filesystem = new Filesystem();
$directory = config('forest')['cacheDir'];
$disabledApcuCache = config('forest')['disabledApcuCache'];
AgentFactory::$fileCacheOptions = compact('filesystem', 'directory', 'disabledApcuCache');

$this->app->instance(AgentFactory::class, self::getAgentInstance());

$this->loadConfiguration();
}
}

public static function getAgentInstance(bool $forceReload = false)
{
if (! $forceReload &&
Cache::enabled() &&
Cache::has('forestAgent') &&
Cache::get('forestAgentExpireAt') > strtotime('+ 30 seconds')
) {
$forestAgentClosure = Cache::get('forestAgent');

return $forestAgentClosure();
}

$agent = new AgentFactory(config('forest'));
$expireAt = strtotime('+ '. AgentFactory::TTL .' seconds');
Cache::put('forestAgent', new SerializableClosure(fn () => $agent), AgentFactory::TTL);
Cache::put('forestAgentExpireAt', $expireAt, AgentFactory::TTL);

return $agent;
}

/**
* @return RouteCollection
*/
Expand Down Expand Up @@ -49,10 +79,15 @@ private function transformMethodsValuesToUpper(array|string $methods): array
private function loadConfiguration(): void
{
if (file_exists($this->appForestConfig())) {
$callback = require $this->appForestConfig();
$callback($this);
$hash = sha1(file_get_contents($this->appForestConfig()));
$agent = self::getAgentInstance();
if($hash !== Cache::get('forestConfigHash') || AgentFactory::get('datasource') === null) {
$this->app->instance(AgentFactory::class, self::getAgentInstance(true));
$callback = require $this->appForestConfig();
$callback();

$this->app->make(AgentFactory::class)->build();
Cache::put('forestConfigHash', $hash, 3600);
}
$this->loadRoutes();
}
}
Expand Down
Loading