22
33namespace Spatie \LaravelSettings ;
44
5+ use DateInterval ;
6+ use DateTimeInterface ;
57use Illuminate \Support \Collection ;
68use Illuminate \Support \Facades \Cache ;
79use Spatie \LaravelSettings \Exceptions \CouldNotUnserializeSettings ;
810use Spatie \LaravelSettings \Exceptions \SettingsCacheDisabled ;
911
1012class SettingsCache
1113{
12- private bool $ enabled ;
13-
14- private ?string $ store ;
15-
16- private ?string $ prefix ;
17-
18- /** @var \DateTimeInterface|\DateInterval|int|null */
19- private $ ttl ;
20-
2114 public function __construct (
22- bool $ enabled ,
23- ?string $ store ,
24- ?string $ prefix ,
25- $ ttl = null
15+ private bool $ enabled ,
16+ private ?string $ store ,
17+ private ?string $ prefix ,
18+ private DateTimeInterface | DateInterval | int | null $ ttl = null
2619 ) {
27- $ this ->enabled = $ enabled ;
28- $ this ->store = $ store ;
29- $ this ->prefix = $ prefix ;
30- $ this ->ttl = $ ttl ;
3120 }
3221
3322 public function isEnabled (): bool
3423 {
3524 return $ this ->enabled ;
3625 }
3726
38- public function has (string $ settingsClass ): bool
39- {
40- if ($ this ->enabled === false ) {
41- return false ;
42- }
43-
44- return Cache::store ($ this ->store )->has ($ this ->resolveCacheKey ($ settingsClass ));
45- }
46-
47- public function get (string $ settingsClass ): Settings
27+ public function get (string $ settingsClass ): ?Settings
4828 {
4929 if ($ this ->enabled === false ) {
5030 throw SettingsCacheDisabled::create ();
5131 }
5232
5333 $ serialized = Cache::store ($ this ->store )->get ($ this ->resolveCacheKey ($ settingsClass ));
5434
35+ if ($ serialized === null ){
36+ return null ;
37+ }
38+
5539 $ settings = unserialize ($ serialized );
5640
5741 if (! $ settings instanceof Settings) {
5842 throw new CouldNotUnserializeSettings ();
5943 }
6044
45+ $ settings ->settingsConfig ()->markLoadedFromCache (true );
46+
6147 return $ settings ;
6248 }
6349
@@ -67,6 +53,8 @@ public function put(Settings $settings): void
6753 return ;
6854 }
6955
56+ $ settings ->settingsConfig ()->markLoadedFromCache (true );
57+
7058 $ serialized = serialize ($ settings );
7159
7260 Cache::store ($ this ->store )->put (
0 commit comments