-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedModel.php
More file actions
46 lines (37 loc) · 1.1 KB
/
Copy pathCachedModel.php
File metadata and controls
46 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Waad\Truffle\Enums\DataType;
use Waad\Truffle\Truffle;
/**
* Per-model caching with TTL and custom driver.
*/
class Setting extends Model
{
use Truffle;
public $incrementing = false;
protected $keyType = 'string';
protected $primaryKey = 'key';
protected $truffleCache = true;
protected $truffleCacheTtl = 3600;
// protected $truffleCacheDriver = 'redis';
// protected $truffleCachePrefix = 'app_settings_';
protected $schema = [
'key' => DataType::String,
'value' => DataType::Text,
];
protected $records = [
['key' => 'app_name', 'value' => 'My Application'],
['key' => 'maintenance_mode', 'value' => 'false'],
['key' => 'items_per_page', 'value' => '25'],
];
public static function getValue(string $key, $default = null)
{
return static::find($key)?->value ?? $default;
}
}
// Usage:
// Setting::getValue('app_name');
// Setting::getValue('missing_key', 'fallback');
// Setting::clearTruffleCache();
// Setting::refreshTruffleCache();