Skip to content

Commit

Permalink
feat: 扩展查询增加缓存机制
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Jun 20, 2024
1 parent d84f38e commit 8b23c55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Extend/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public function settings()
{
if ($this->settings === null) {
try {
$this->settings = ExtensionModel::all()->keyBy('name');
$this->settings = ExtensionModel::keyByNameList();
} catch (\Throwable $e) {
$this->settings = new Collection();
}
Expand Down
20 changes: 20 additions & 0 deletions src/Models/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@

class Extension extends BaseModel
{
const CACHE_KEY = 'admin_extensions';

protected $fillable = ['name', 'is_enabled', 'options'];

protected $casts = [
'options' => 'json',
];

protected $table = 'admin_extensions';

protected static function boot()
{
parent::boot();

$clearCache = function () {
cache()->forget(self::CACHE_KEY);
};

static::created($clearCache);
static::updated($clearCache);
static::deleted($clearCache);
}

public static function keyByNameList()
{
return cache()->rememberForever(self::CACHE_KEY, fn() => self::all()->keyBy('name'));
}
}

0 comments on commit 8b23c55

Please sign in to comment.