Skip to content

Commit

Permalink
Separate Cache Helper File to Package
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Oct 16, 2023
1 parent efa0869 commit 21a67ba
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 217 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "anisaronno/laravel-settings",
"description": "Laravel Settings Table",
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"keywords": [
"laravel-settings",
Expand All @@ -20,6 +20,7 @@
"homepage": "https://github.com/anisAronno/laravel-settings",
"require": {
"php": "^7.4|^8.0",
"anisaronno/laravel-cache-control": "^0.0.1",
"laravel/framework": "^8.0|^9.0|^10.0"
},
"require-dev": {
Expand Down
69 changes: 66 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 0 additions & 199 deletions src/Helpers/CacheHelper.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Helpers/CacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AnisAronno\LaravelSettings\Helpers;

class CacheKey
{
/**
* Cache Key
* @return string
*/
public static function getLaravelSettingsCacheKey(): string
{
return "_LaravelSettings_";
}
}
9 changes: 5 additions & 4 deletions src/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AnisAronno\LaravelSettings\Helpers;

use AnisAronno\LaravelCacheMaster\CacheControl;
use AnisAronno\LaravelSettings\Models\SettingsProperty;
use Exception;
use Illuminate\Support\Collection;
Expand All @@ -18,11 +19,11 @@ class SettingsHelper
*/
public static function getSettings(string $settingsKey): string
{
$key = CacheHelper::getLaravelSettingsCacheKey();
$key = CacheKey::getLaravelSettingsCacheKey();


try {
$settings = CacheHelper::init($key)->remember($settingsKey, now()->addDay(), function () use ($settingsKey) {
$settings = CacheControl::init($key)->remember($settingsKey, now()->addDay(), function () use ($settingsKey) {
return SettingsProperty::select('settings_value')->find($settingsKey);
});

Expand Down Expand Up @@ -113,11 +114,11 @@ public static function upsertSettings(string $key, string $value): SettingsPrope
*/
public static function getAllSettings(): Collection
{
$key = CacheHelper::getLaravelSettingsCacheKey();
$key = CacheKey::getLaravelSettingsCacheKey();
$cacheKey = $key.md5(serialize(['getAllSettings']));

try {
return CacheHelper::init($key)->remember($cacheKey, 10, function () {
return CacheControl::init($key)->remember($cacheKey, 10, function () {
return SettingsProperty::select('settings_value', 'settings_key')->orderBy('settings_key', 'asc')->get()->flatMap(function ($name) {
return [$name->settings_key => $name->settings_value];
});
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Controllers/LaravelSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace AnisAronno\LaravelSettings\Http\Controllers;

use AnisAronno\LaravelSettings\Helpers\CacheHelper;
use AnisAronno\LaravelCacheMaster\CacheControl;
use AnisAronno\LaravelSettings\Helpers\CacheKey;
use AnisAronno\LaravelSettings\Http\Requests\StoreLaravelSettingsRequest;
use AnisAronno\LaravelSettings\Http\Requests\UpdateLaravelSettingsRequest;
use AnisAronno\LaravelSettings\Http\Resources\SettingsResources;
Expand All @@ -29,11 +30,11 @@ public function index(Request $request): JsonResponse
$endDate = $request->query('endDate', '');
$page = $request->query('page', 1);

$imageCacheKey = CacheHelper::getLaravelSettingsCacheKey();
$imageCacheKey = CacheKey::getLaravelSettingsCacheKey();

$key = $imageCacheKey.md5(serialize([$orderBy, $order, $page, $search, $startDate, $endDate, ]));

$images = CacheHelper::init($imageCacheKey)->remember(
$images = CacheControl::init($imageCacheKey)->remember(
$key,
now()->addDay(),
function () use ($request) {
Expand Down
Loading

0 comments on commit 21a67ba

Please sign in to comment.