Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ $isSubscribed = $user->getSetting('subscribe_newsletter');
echo $isSubscribed // false
```

## Cache

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.

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.

```php
<?php

return [
// Change this to true if you want to use the cache feature.
'with_cache' => true,

// The cache key prefix that will be used to store the settings in the cache.
'cache_prefix' => 'laravel-settings',

// Cache lifetime in seconds.
'cache_lifetime' => 60 * 60 * 24 * 7, // 7 days

// other config
];
```

## Customization

### Using Your Own Model
Expand Down
9 changes: 9 additions & 0 deletions config/laravel-settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?php

return [
// Change this to true if you want to use the cache feature.
'with_cache' => false,

// The cache key prefix that will be used to store the settings in the cache.
'cache_prefix' => 'laravel-settings',

// Cache lifetime in seconds.
'cache_lifetime' => 60 * 60 * 24 * 7, // 7 days

// The model that will be used to retrieve and store settings.
// You can change this if you want to use a different model.
'model' => \RuangDeveloper\LaravelSettings\Models\Setting::class,
Expand Down
120 changes: 83 additions & 37 deletions src/Services/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace RuangDeveloper\LaravelSettings\Services;

use Illuminate\Support\Facades\Cache;
use RuangDeveloper\LaravelSettings\Supports\Support;

class SettingsService
{
/**
Expand Down Expand Up @@ -31,10 +34,7 @@ public function __construct(string $model)
*/
public function set(string $key, mixed $value): void
{
$this->model::updateOrCreate(
[config('laravel-settings.key_name') => $key],
[config('laravel-settings.value_name') => $value]
);
$this->storeSetting($key, $value);
}

/**
Expand All @@ -46,25 +46,7 @@ public function set(string $key, mixed $value): void
*/
public function get(string $key, mixed $default = null): mixed
{
$setting = $this->model::where([
config('laravel-settings.key_name') => $key,
config('laravel-settings.morph_type') => null,
config('laravel-settings.morph_id') => null,
])->first();

if ($setting) {
return $setting->value;
}

if (!is_null($default)) {
return $default;
}

if (config('laravel-settings.defaults') && array_key_exists($key, config('laravel-settings.defaults'))) {
return config('laravel-settings.defaults')[$key];
}

return $default;
return $this->findSetting($key, null, null, $default);
}

/**
Expand All @@ -75,11 +57,7 @@ public function get(string $key, mixed $default = null): mixed
*/
public function forget(string $key): void
{
$this->model::where([
config('laravel-settings.key_name') => $key,
config('laravel-settings.morph_type') => null,
config('laravel-settings.morph_id') => null,
])->delete();
$this->deleteSetting($key);
}

/**
Expand All @@ -93,37 +71,95 @@ public function forget(string $key): void
*/
public function setWithModel(string $key, mixed $value, string $modelType, mixed $modelId): void
{
$this->storeSetting($key, $value, $modelType, $modelId);
}

/**
* Get a setting value with model.
*
* @param string $key
* @param string $modelType
* @param mixed $modelId
* @param mixed $default
* @return mixed
*/
public function getWithModel(string $key, string $modelType, mixed $modelId, mixed $default = null): mixed
{
return $this->findSetting($key, $modelType, $modelId, $default);
}

/**
* Forget a setting value with model.
*
* @param string $key
* @param string $modelType
* @param mixed $modelId
* @return void
*/
public function forgetWithModel(string $key, string $modelType, mixed $modelId): void
{
$this->deleteSetting($key, $modelType, $modelId);
}

/**
* Store a setting value.
*
* @param string $key
* @param mixed $value
* @param string $modelType
* @param mixed $modelId
* @return void
*/
private function storeSetting(string $key, mixed $value, string $modelType = null, mixed $modelId = null): void
{

$this->model::updateOrCreate(
[
config('laravel-settings.key_name') => $key,
config('laravel-settings.morph_type') => $modelType,
config('laravel-settings.morph_id') => $modelId,
],
[
'value' => $value,
]
[config('laravel-settings.value_name') => $value]
);

if (config('laravel-settings.with_cache')) {
Cache::forget(Support::getCacheKey($key, $modelType, $modelId));
}
}

/**
* Get a setting value with model.
* Find a setting value.
*
* @param string $key
* @param string $modelType
* @param mixed $modelId
* @param mixed $default
* @return mixed
*/
public function getWithModel(string $key, string $modelType, mixed $modelId, mixed $default = null): mixed
private function findSetting(string $key, string $modelType = null, mixed $modelId = null, mixed $default = null): mixed
{
if (config('laravel-settings.with_cache')) {
$cacheKey = Support::getCacheKey($key, $modelType, $modelId);

if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
}

$setting = $this->model::where([
config('laravel-settings.key_name') => $key,
config('laravel-settings.morph_type') => $modelType,
config('laravel-settings.morph_id') => $modelId,
])->first();

if ($setting) {
return $setting->value;
$value = $setting->value;

if (config('laravel-settings.with_cache')) {
Cache::put($cacheKey, $value, config('laravel-settings.cache_lifetime'));
}

return $value;
}

if (!is_null($default)) {
Expand All @@ -135,26 +171,36 @@ public function getWithModel(string $key, string $modelType, mixed $modelId, mix
array_key_exists($modelType, config('laravel-settings.model_defaults')) &&
array_key_exists($key, config('laravel-settings.model_defaults')[$modelType])
) {
return config('laravel-settings.model_defaults')[$modelType][$key];
$value = config('laravel-settings.model_defaults')[$modelType][$key];
return $value;
}

if (config('laravel-settings.defaults') && array_key_exists($key, config('laravel-settings.defaults'))) {
$value = config('laravel-settings.defaults')[$key];
return $value;
}

return $default;
}

/**
* Forget a setting value with model.
* Delete a setting.
*
* @param string $key
* @param string $modelType
* @param mixed $modelId
* @return void
*/
public function forgetWithModel(string $key, string $modelType, mixed $modelId): void
private function deleteSetting(string $key, string $modelType = null, mixed $modelId = null): void
{
$this->model::where([
config('laravel-settings.key_name') => $key,
config('laravel-settings.morph_type') => $modelType,
config('laravel-settings.morph_id') => $modelId,
])->delete();

if (config('laravel-settings.with_cache')) {
Cache::forget(Support::getCacheKey($key, $modelType, $modelId));
}
}
}
29 changes: 29 additions & 0 deletions src/Supports/Support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace RuangDeveloper\LaravelSettings\Supports;

class Support
{
/**
* Get the cache key.
*
* @param string $key
* @param string|null $modelType
* @param mixed|null $modelId
* @return string
*/
public static function getCacheKey(string $key, string $modelType = null, mixed $modelId = null): string
{
$cacheKey = config('laravel-settings.cache_prefix') . '.' . $key;

if ($modelType) {
$cacheKey .= '.' . $modelType;
}

if ($modelId) {
$cacheKey .= '.' . $modelId;
}

return $cacheKey;
}
}
24 changes: 23 additions & 1 deletion src/Traits/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace RuangDeveloper\LaravelSettings\Traits;

use Illuminate\Support\Facades\Cache;
use RuangDeveloper\LaravelSettings\Supports\Support;

trait HasSettings
{
/**
Expand Down Expand Up @@ -39,6 +42,10 @@ public function setSetting(string $key, mixed $value): void
config('laravel-settings.morph_id') => $this->getKey(),
]
);

if (config('laravel-settings.with_cache')) {
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
}
}

/**
Expand All @@ -50,9 +57,20 @@ public function setSetting(string $key, mixed $value): void
*/
public function getSetting(string $key, mixed $default = null): mixed
{
if (config('laravel-settings.with_cache')) {
$cacheKey = Support::getCacheKey($key, $this->getMorphClass(), $this->getKey());

if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
}

$setting = $this->settings()->where(config('laravel-settings.key_name'), $key)->first();

if ($setting) {
if (config('laravel-settings.with_cache')) {
Cache::put($cacheKey, $setting->value, config('laravel-settings.cache_lifetime'));
}
return $setting->value;
}

Expand All @@ -76,7 +94,7 @@ public function getSetting(string $key, mixed $default = null): mixed
*
* @param string $key
* @return void
*/
*/
public function forgetSetting(string $key): void
{
$this->settings()->where(
Expand All @@ -86,5 +104,9 @@ public function forgetSetting(string $key): void
config('laravel-settings.morph_id') => $this->getKey(),
]
)->delete();

if (config('laravel-settings.with_cache')) {
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
}
}
}