Skip to content

Commit 09d003e

Browse files
committed
feat: add cache support
1 parent 1b0b651 commit 09d003e

File tree

5 files changed

+167
-38
lines changed

5 files changed

+167
-38
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ $isSubscribed = $user->getSetting('subscribe_newsletter');
179179
echo $isSubscribed // false
180180
```
181181

182+
## Cache
183+
184+
As you know, this package uses a database to store setting values. Every time you retrieve a setting value, it means you are making a query to the database. This is not a problem for small-scale applications, but it can have a significant impact when used in large-scale applications. To work around this, you can enable caching to store setting values in the cache, so the query is only performed once when you first attempt to retrieve a setting value.
185+
186+
To enable caching, you can go to the file `config/laravel-settings.php` and change the value of `with_cache` to true. You can also set the prefix and lifetime there.
187+
188+
```php
189+
<?php
190+
191+
return [
192+
// Change this to true if you want to use the cache feature.
193+
'with_cache' => true,
194+
195+
// The cache key prefix that will be used to store the settings in the cache.
196+
'cache_prefix' => 'laravel-settings',
197+
198+
// Cache lifetime in seconds.
199+
'cache_lifetime' => 60 * 60 * 24 * 7, // 7 days
200+
201+
// other config
202+
];
203+
```
204+
182205
## Customization
183206

184207
### Using Your Own Model

config/laravel-settings.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<?php
22

33
return [
4+
// Change this to true if you want to use the cache feature.
5+
'with_cache' => false,
6+
7+
// The cache key prefix that will be used to store the settings in the cache.
8+
'cache_prefix' => 'laravel-settings',
9+
10+
// Cache lifetime in seconds.
11+
'cache_lifetime' => 60 * 60 * 24 * 7, // 7 days
12+
413
// The model that will be used to retrieve and store settings.
514
// You can change this if you want to use a different model.
615
'model' => \RuangDeveloper\LaravelSettings\Models\Setting::class,

src/Services/SettingsService.php

Lines changed: 83 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace RuangDeveloper\LaravelSettings\Services;
44

5+
use Illuminate\Support\Facades\Cache;
6+
use RuangDeveloper\LaravelSettings\Supports\Support;
7+
58
class SettingsService
69
{
710
/**
@@ -31,10 +34,7 @@ public function __construct(string $model)
3134
*/
3235
public function set(string $key, mixed $value): void
3336
{
34-
$this->model::updateOrCreate(
35-
[config('laravel-settings.key_name') => $key],
36-
[config('laravel-settings.value_name') => $value]
37-
);
37+
$this->storeSetting($key, $value);
3838
}
3939

4040
/**
@@ -46,25 +46,7 @@ public function set(string $key, mixed $value): void
4646
*/
4747
public function get(string $key, mixed $default = null): mixed
4848
{
49-
$setting = $this->model::where([
50-
config('laravel-settings.key_name') => $key,
51-
config('laravel-settings.morph_type') => null,
52-
config('laravel-settings.morph_id') => null,
53-
])->first();
54-
55-
if ($setting) {
56-
return $setting->value;
57-
}
58-
59-
if (!is_null($default)) {
60-
return $default;
61-
}
62-
63-
if (config('laravel-settings.defaults') && array_key_exists($key, config('laravel-settings.defaults'))) {
64-
return config('laravel-settings.defaults')[$key];
65-
}
66-
67-
return $default;
49+
return $this->findSetting($key, null, null, $default);
6850
}
6951

7052
/**
@@ -75,11 +57,7 @@ public function get(string $key, mixed $default = null): mixed
7557
*/
7658
public function forget(string $key): void
7759
{
78-
$this->model::where([
79-
config('laravel-settings.key_name') => $key,
80-
config('laravel-settings.morph_type') => null,
81-
config('laravel-settings.morph_id') => null,
82-
])->delete();
60+
$this->deleteSetting($key);
8361
}
8462

8563
/**
@@ -93,37 +71,95 @@ public function forget(string $key): void
9371
*/
9472
public function setWithModel(string $key, mixed $value, string $modelType, mixed $modelId): void
9573
{
74+
$this->storeSetting($key, $value, $modelType, $modelId);
75+
}
76+
77+
/**
78+
* Get a setting value with model.
79+
*
80+
* @param string $key
81+
* @param string $modelType
82+
* @param mixed $modelId
83+
* @param mixed $default
84+
* @return mixed
85+
*/
86+
public function getWithModel(string $key, string $modelType, mixed $modelId, mixed $default = null): mixed
87+
{
88+
return $this->findSetting($key, $modelType, $modelId, $default);
89+
}
90+
91+
/**
92+
* Forget a setting value with model.
93+
*
94+
* @param string $key
95+
* @param string $modelType
96+
* @param mixed $modelId
97+
* @return void
98+
*/
99+
public function forgetWithModel(string $key, string $modelType, mixed $modelId): void
100+
{
101+
$this->deleteSetting($key, $modelType, $modelId);
102+
}
103+
104+
/**
105+
* Store a setting value.
106+
*
107+
* @param string $key
108+
* @param mixed $value
109+
* @param string $modelType
110+
* @param mixed $modelId
111+
* @return void
112+
*/
113+
private function storeSetting(string $key, mixed $value, string $modelType = null, mixed $modelId = null): void
114+
{
115+
96116
$this->model::updateOrCreate(
97117
[
98118
config('laravel-settings.key_name') => $key,
99119
config('laravel-settings.morph_type') => $modelType,
100120
config('laravel-settings.morph_id') => $modelId,
101121
],
102-
[
103-
'value' => $value,
104-
]
122+
[config('laravel-settings.value_name') => $value]
105123
);
124+
125+
if (config('laravel-settings.with_cache')) {
126+
Cache::forget(Support::getCacheKey($key, $modelType, $modelId));
127+
}
106128
}
107129

108130
/**
109-
* Get a setting value with model.
131+
* Find a setting value.
110132
*
111133
* @param string $key
112134
* @param string $modelType
113135
* @param mixed $modelId
114136
* @param mixed $default
115137
* @return mixed
116138
*/
117-
public function getWithModel(string $key, string $modelType, mixed $modelId, mixed $default = null): mixed
139+
private function findSetting(string $key, string $modelType = null, mixed $modelId = null, mixed $default = null): mixed
118140
{
141+
if (config('laravel-settings.with_cache')) {
142+
$cacheKey = Support::getCacheKey($key, $modelType, $modelId);
143+
144+
if (Cache::has($cacheKey)) {
145+
return Cache::get($cacheKey);
146+
}
147+
}
148+
119149
$setting = $this->model::where([
120150
config('laravel-settings.key_name') => $key,
121151
config('laravel-settings.morph_type') => $modelType,
122152
config('laravel-settings.morph_id') => $modelId,
123153
])->first();
124154

125155
if ($setting) {
126-
return $setting->value;
156+
$value = $setting->value;
157+
158+
if (config('laravel-settings.with_cache')) {
159+
Cache::put($cacheKey, $value, config('laravel-settings.cache_lifetime'));
160+
}
161+
162+
return $value;
127163
}
128164

129165
if (!is_null($default)) {
@@ -135,26 +171,36 @@ public function getWithModel(string $key, string $modelType, mixed $modelId, mix
135171
array_key_exists($modelType, config('laravel-settings.model_defaults')) &&
136172
array_key_exists($key, config('laravel-settings.model_defaults')[$modelType])
137173
) {
138-
return config('laravel-settings.model_defaults')[$modelType][$key];
174+
$value = config('laravel-settings.model_defaults')[$modelType][$key];
175+
return $value;
176+
}
177+
178+
if (config('laravel-settings.defaults') && array_key_exists($key, config('laravel-settings.defaults'))) {
179+
$value = config('laravel-settings.defaults')[$key];
180+
return $value;
139181
}
140182

141183
return $default;
142184
}
143185

144186
/**
145-
* Forget a setting value with model.
187+
* Delete a setting.
146188
*
147189
* @param string $key
148190
* @param string $modelType
149191
* @param mixed $modelId
150192
* @return void
151193
*/
152-
public function forgetWithModel(string $key, string $modelType, mixed $modelId): void
194+
private function deleteSetting(string $key, string $modelType = null, mixed $modelId = null): void
153195
{
154196
$this->model::where([
155197
config('laravel-settings.key_name') => $key,
156198
config('laravel-settings.morph_type') => $modelType,
157199
config('laravel-settings.morph_id') => $modelId,
158200
])->delete();
201+
202+
if (config('laravel-settings.with_cache')) {
203+
Cache::forget(Support::getCacheKey($key, $modelType, $modelId));
204+
}
159205
}
160206
}

src/Supports/Support.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace RuangDeveloper\LaravelSettings\Supports;
4+
5+
class Support
6+
{
7+
/**
8+
* Get the cache key.
9+
*
10+
* @param string $key
11+
* @param string|null $modelType
12+
* @param mixed|null $modelId
13+
* @return string
14+
*/
15+
public static function getCacheKey(string $key, string $modelType = null, mixed $modelId = null): string
16+
{
17+
$cacheKey = config('laravel-settings.cache_prefix') . '.' . $key;
18+
19+
if ($modelType) {
20+
$cacheKey .= '.' . $modelType;
21+
}
22+
23+
if ($modelId) {
24+
$cacheKey .= '.' . $modelId;
25+
}
26+
27+
return $cacheKey;
28+
}
29+
}

src/Traits/HasSettings.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace RuangDeveloper\LaravelSettings\Traits;
44

5+
use Illuminate\Support\Facades\Cache;
6+
use RuangDeveloper\LaravelSettings\Supports\Support;
7+
58
trait HasSettings
69
{
710
/**
@@ -39,6 +42,10 @@ public function setSetting(string $key, mixed $value): void
3942
config('laravel-settings.morph_id') => $this->getKey(),
4043
]
4144
);
45+
46+
if (config('laravel-settings.with_cache')) {
47+
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
48+
}
4249
}
4350

4451
/**
@@ -50,9 +57,20 @@ public function setSetting(string $key, mixed $value): void
5057
*/
5158
public function getSetting(string $key, mixed $default = null): mixed
5259
{
60+
if (config('laravel-settings.with_cache')) {
61+
$cacheKey = Support::getCacheKey($key, $this->getMorphClass(), $this->getKey());
62+
63+
if (Cache::has($cacheKey)) {
64+
return Cache::get($cacheKey);
65+
}
66+
}
67+
5368
$setting = $this->settings()->where(config('laravel-settings.key_name'), $key)->first();
5469

5570
if ($setting) {
71+
if (config('laravel-settings.with_cache')) {
72+
Cache::put($cacheKey, $setting->value, config('laravel-settings.cache_lifetime'));
73+
}
5674
return $setting->value;
5775
}
5876

@@ -76,7 +94,7 @@ public function getSetting(string $key, mixed $default = null): mixed
7694
*
7795
* @param string $key
7896
* @return void
79-
*/
97+
*/
8098
public function forgetSetting(string $key): void
8199
{
82100
$this->settings()->where(
@@ -86,5 +104,9 @@ public function forgetSetting(string $key): void
86104
config('laravel-settings.morph_id') => $this->getKey(),
87105
]
88106
)->delete();
107+
108+
if (config('laravel-settings.with_cache')) {
109+
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
110+
}
89111
}
90112
}

0 commit comments

Comments
 (0)