Cache loading templates

This commit is contained in:
Deon George 2025-06-11 21:43:22 +09:30
parent 31e3c75bc9
commit f2eaed247a
2 changed files with 24 additions and 11 deletions

View File

@ -10,7 +10,7 @@ class Template
private string $file; private string $file;
private array $template; private array $template;
private(set) bool $invalid = FALSE; private(set) bool $invalid = FALSE;
private string $reason = ''; private(set) string $reason = '';
public function __construct(string $file) public function __construct(string $file)
{ {

View File

@ -3,7 +3,10 @@
namespace App\Ldap; namespace App\Ldap;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use LdapRecord\Support\Arr; use LdapRecord\Support\Arr;
@ -41,20 +44,28 @@ class Entry extends Model
public function __construct(array $attributes = []) public function __construct(array $attributes = [])
{ {
$this->objects = collect();
parent::__construct($attributes); parent::__construct($attributes);
$this->objects = collect();
// Load any templates // Load any templates
$x = Storage::disk(config('pla.template.dir')); $this->templates = Cache::remember('templates'.Session::id(),config('ldap.cache.time'),function() {
$this->templates = collect(); $template_dir = Storage::disk(config('pla.template.dir'));
$templates = collect();
foreach (array_filter($x->files(),fn($item)=>Str::endsWith($item,'.json')) as $file) foreach (array_filter($template_dir->files(),fn($item)=>Str::endsWith($item,'.json')) as $file) {
$this->templates->put($file,new Template($file)); $to = new Template($file);
$this->templates = $this->templates if ($to->invalid) {
->filter(fn($item)=>(! $item->invalid) && $item->enabled) Log::debug(sprintf('Template [%s] is not valid (%s) - ignoring',$file,$to->reason));
->sortBy(fn($item)=>$item);
} else {
$templates->put($file,new Template($file));
}
}
return $templates;
});
} }
public function discardChanges(): static public function discardChanges(): static
@ -147,7 +158,9 @@ class Entry extends Model
// Filter out our templates specific for this entry // Filter out our templates specific for this entry
if ($this->dn && (! in_array(strtolower($this->dn),['cn=subschema']))) { if ($this->dn && (! in_array(strtolower($this->dn),['cn=subschema']))) {
$this->templates = $this->templates $this->templates = $this->templates
->filter(fn($item)=>! count($item->objectclasses->diff(array_map('strtolower',Arr::get($this->attributes,'objectclass'))))); ->filter(fn($item)=>$item->enabled
&& (! count($item->objectclasses->diff(array_map('strtolower',Arr::get($this->attributes,'objectclass'))))))
->sortBy(fn($item)=>$item);
} }
return $this; return $this;