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
|
/storage/*.key
|
||||||
/vendor
|
/vendor
|
||||||
/storage/credit_deduction_log
|
/storage/credit_deduction_log
|
||||||
|
storage/debugbar
|
||||||
.env
|
.env
|
||||||
.env.testing
|
.env.testing
|
||||||
.env.backup
|
.env.backup
|
||||||
|
|
|
@ -4,11 +4,14 @@ namespace App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class Configuration extends Model
|
class Configuration extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
public const CACHE_TAG = 'configuration';
|
||||||
|
|
||||||
public $primaryKey = 'key';
|
public $primaryKey = 'key';
|
||||||
|
|
||||||
public $incrementing = false;
|
public $incrementing = false;
|
||||||
|
@ -21,14 +24,25 @@ class Configuration extends Model
|
||||||
'type',
|
'type',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public static function boot()
|
||||||
|
{
|
||||||
|
parent::boot();
|
||||||
|
|
||||||
|
static::updated(function (Configuration $configuration) {
|
||||||
|
Cache::forget(self::CACHE_TAG .':'. $configuration->key);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param $default
|
* @param $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function getValueByKey(string $key , $default = null)
|
public static function getValueByKey(string $key, $default = null)
|
||||||
{
|
{
|
||||||
$configuration = self::find($key);
|
return Cache::rememberForever(self::CACHE_TAG .':'. $key, function () use ($default, $key) {
|
||||||
return $configuration ? $configuration->value : $default;
|
$configuration = self::find($key);
|
||||||
|
return $configuration ? $configuration->value : $default;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue