Caching configuration options
This commit is contained in:
parent
79b1be655c
commit
5cb91ea767
2 changed files with 18 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@
|
|||
/storage/*.key
|
||||
/vendor
|
||||
/storage/credit_deduction_log
|
||||
storage/debugbar
|
||||
.env
|
||||
.env.testing
|
||||
.env.backup
|
||||
|
|
|
@ -4,11 +4,14 @@ namespace App\Models;
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class Configuration extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const CACHE_TAG = 'configuration';
|
||||
|
||||
public $primaryKey = 'key';
|
||||
|
||||
public $incrementing = false;
|
||||
|
@ -21,6 +24,15 @@ class Configuration extends Model
|
|||
'type',
|
||||
];
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::updated(function (Configuration $configuration) {
|
||||
Cache::forget(self::CACHE_TAG .':'. $configuration->key);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param $default
|
||||
|
@ -28,7 +40,9 @@ class Configuration extends Model
|
|||
*/
|
||||
public static function getValueByKey(string $key, $default = null)
|
||||
{
|
||||
return Cache::rememberForever(self::CACHE_TAG .':'. $key, function () use ($default, $key) {
|
||||
$configuration = self::find($key);
|
||||
return $configuration ? $configuration->value : $default;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue