Skip to content

Commit

Permalink
Remove Cache Controller Package
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Jan 30, 2024
1 parent 4fba4f8 commit ea952a5
Show file tree
Hide file tree
Showing 195 changed files with 1,144 additions and 1,815 deletions.
42 changes: 42 additions & 0 deletions app/Helpers/CacheControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Facades\Cache;

class CacheControl
{
/**
* Cache Clear by key
*
* @param string $key
* @return void
*/
public static function clearCache(string $key)
{
$keys = (array) Cache::get($key, []);

foreach ($keys as $key) {
Cache::forget($key);
}

Cache::forget($key);
}

/**
* Put Cache key to another key as a array
*
* @param string $baseCacheKey
* @param string $newCacheKey
* @return void
*/
public static function updateCacheKeys(string $baseCacheKey, string $newCacheKey)
{
$cachedKeys = Cache::get($baseCacheKey, []);

if (!in_array($newCacheKey, $cachedKeys)) {
$cachedKeys[] = $newCacheKey;
Cache::put($baseCacheKey, $cachedKeys);
}
}
}
40 changes: 17 additions & 23 deletions app/Http/Controllers/Admin/Role/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers\Admin\Role;

use AnisAronno\LaravelCacheMaster\CacheControl;
use App\Helpers\CacheControl;
use App\Helpers\CacheKey;
use App\Http\Controllers\InertiaApplicationController;
use App\Http\Requests\Role\RoleStoreRequest;
Expand All @@ -11,6 +11,7 @@
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
Expand All @@ -32,39 +33,36 @@ public function __construct()
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @param Request $request
*/
public function index(Request $request)
{
$currentPage = isset($request->page) ? (int) [$request->page] : 1;
$queryParams = $request->query();
ksort($queryParams);
$queryString = http_build_query($queryParams);
$roleCacheKey = CacheKey::getRoleCacheKey();
$key = $roleCacheKey . md5(serialize([$queryString]));

if (! empty($request->search)) {
$q = $request->search;
$roles = Role::with(['permissions' => function ($query) {
$query->select('id', 'name');
}])->where('name', 'LIKE', '%'.$q.'%')->orderBy('id', 'desc')->paginate(10);

return Inertia::render('Dashboard/Role/Index', ['roles' => $roles]);
}

$key = CacheKey::getRoleCacheKey();
$cacheKey = $key.md5(serialize([$currentPage]));

$roles = CacheControl::init($key)->remember($cacheKey, 10, function () {
$roles = Cache::remember($key, now()->addDay(), function () use ($request) {
return Role::with(['permissions' => function ($query) {
$query->select('id', 'name');
}])->orderBy('id', 'desc')->paginate(10);
}])
->when($request->has('search'), function ($query) use ($request) {
$query->where('name', 'LIKE', '%' . $request->input('search') . '%');
})
->orderBy($request->input('orderBy', 'id'), $request->input('order', 'desc'))
->paginate(10)
->withQueryString();
});

Session::put('last_visited_url', $request->fullUrl());
CacheControl::updateCacheKeys($roleCacheKey, $key);

return Inertia::render('Dashboard/Role/Index')->with(['roles' => RoleResource::collection($roles)]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
Expand All @@ -89,7 +87,6 @@ public function create()
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(RoleStoreRequest $request)
{
Expand Down Expand Up @@ -120,7 +117,6 @@ public function show(Role $role)
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Role $role)
{
Expand Down Expand Up @@ -152,7 +148,6 @@ public function edit(Role $role)
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(RoleUpdateRequest $request, Role $role)
{
Expand All @@ -177,7 +172,6 @@ public function update(RoleUpdateRequest $request, Role $role)
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Role $role)
{
Expand Down
53 changes: 29 additions & 24 deletions app/Http/Controllers/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Http\Controllers\User;

use AnisAronno\LaravelCacheMaster\CacheControl;
use AnisAronno\MediaHelper\Facades\Media;
use App\Enums\UserStatus;
use App\Helpers\CacheControl;
use App\Helpers\CacheKey;
use App\Http\Controllers\InertiaApplicationController;
use App\Http\Requests\User\UserStoreRequest;
Expand All @@ -13,6 +13,7 @@
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
Expand All @@ -33,34 +34,44 @@ public function __construct()
/**
* Display a listing of the resource.
*
*/
* @param Request $request
*/
public function index(Request $request)
{
$currentPage = isset($request->page) ? (int) [$request->page] : 1;


if (! empty($request->search)) {
$q = $request->search;
$users = User::with(['roles'])->where('name', 'LIKE', '%'.$q.'%')->orWhere('email', 'LIKE', '%'.$q.'%')->orderBy('id', 'desc')->paginate(10);

return Inertia::render('Dashboard/User/Index', ['users' => $users]);
}
$key = CacheKey::getUserCacheKey();
$cacheKey = $key.md5(serialize([$currentPage]));

$users = CacheControl::init($key)->remember($cacheKey, 10, function () {
return User::with(['roles'])->orderBy('id', 'desc')->paginate(10);
$queryParams = $request->query();
ksort($queryParams);
$queryString = http_build_query($queryParams);
$userCacheKey = CacheKey::getUserCacheKey();
$key = $userCacheKey . md5(serialize([$queryString]));

$users = Cache::remember($key, now()->addDay(), function () use ($request) {
return User::with(['roles'])
->when($request->has('search'), function ($query) use ($request) {
$query->where(function ($subQuery) use ($request) {
$subQuery->where('name', 'LIKE', '%' . $request->input('search') . '%')
->orWhere('email', 'LIKE', '%' . $request->input('search') . '%');
});
})
->when($request->has('startDate') && $request->has('endDate'), function ($query) use ($request) {
$query->whereBetween('created_at', [
new \DateTime($request->input('startDate')),
new \DateTime($request->input('endDate'))
]);
})
->orderBy($request->input('orderBy', 'id'), $request->input('order', 'desc'))
->paginate(10)
->withQueryString();
});

Session::put('last_visited_url', $request->fullUrl());
CacheControl::updateCacheKeys($userCacheKey, $key);

return Inertia::render('Dashboard/User/Index')->with(['users' => UserResources::collection($users)]);
}


/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
Expand All @@ -74,7 +85,6 @@ public function create()
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(UserStoreRequest $request)
{
Expand Down Expand Up @@ -110,7 +120,6 @@ public function show(User $user)
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
Expand All @@ -132,7 +141,6 @@ public function edit(User $user)
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UserUpdateRequest $request, User $user)
{
Expand Down Expand Up @@ -164,7 +172,6 @@ public function update(UserUpdateRequest $request, User $user)
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(User $user)
{
Expand All @@ -185,7 +192,6 @@ public function destroy(User $user)
* Summary of avatarUpdate
*
* @param User $user
* @return \Illuminate\Http\RedirectResponse
*/
public function avatarUpdate(Request $request, User $user)
{
Expand All @@ -206,7 +212,6 @@ public function avatarUpdate(Request $request, User $user)
* Remove the specified user avatar.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function avatarDelete(User $user)
{
Expand Down
12 changes: 6 additions & 6 deletions app/Observers/OptionObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Observers;

use AnisAronno\LaravelCacheMaster\CacheControl;
use App\Helpers\CacheControl;
use App\Helpers\CacheKey;
use App\Models\Option;

Expand All @@ -23,7 +23,7 @@ public function __construct()
*/
public function created(Option $option)
{
CacheControl::forgetCache($this->optionsCacheKey);
CacheControl::clearCache($this->optionsCacheKey);
}

/**
Expand All @@ -34,7 +34,7 @@ public function created(Option $option)
*/
public function updated(Option $option)
{
CacheControl::forgetCache($this->optionsCacheKey);
CacheControl::clearCache($this->optionsCacheKey);
}

/**
Expand All @@ -45,7 +45,7 @@ public function updated(Option $option)
*/
public function deleted(Option $option)
{
CacheControl::forgetCache($this->optionsCacheKey);
CacheControl::clearCache($this->optionsCacheKey);
}

/**
Expand All @@ -56,7 +56,7 @@ public function deleted(Option $option)
*/
public function restored(Option $option)
{
CacheControl::forgetCache($this->optionsCacheKey);
CacheControl::clearCache($this->optionsCacheKey);
}

/**
Expand All @@ -67,7 +67,7 @@ public function restored(Option $option)
*/
public function forceDeleted(Option $option)
{
CacheControl::forgetCache($this->optionsCacheKey);
CacheControl::clearCache($this->optionsCacheKey);

}
}
22 changes: 11 additions & 11 deletions app/Observers/RoleObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Observers;

use AnisAronno\LaravelCacheMaster\CacheControl;
use App\Helpers\CacheControl;
use App\Helpers\CacheKey;
use Spatie\Permission\Contracts\Role;

Expand All @@ -26,8 +26,8 @@ public function __construct()
*/
public function created(Role $role)
{
CacheControl::forgetCache($this->roleCacheKey);
CacheControl::forgetCache($this->userCacheKey);
CacheControl::clearCache($this->roleCacheKey);
CacheControl::clearCache($this->userCacheKey);
}

/**
Expand All @@ -38,8 +38,8 @@ public function created(Role $role)
*/
public function updated(Role $role)
{
CacheControl::forgetCache($this->roleCacheKey);
CacheControl::forgetCache($this->userCacheKey);
CacheControl::clearCache($this->roleCacheKey);
CacheControl::clearCache($this->userCacheKey);
}

/**
Expand All @@ -50,8 +50,8 @@ public function updated(Role $role)
*/
public function deleted(Role $role)
{
CacheControl::forgetCache($this->roleCacheKey);
CacheControl::forgetCache($this->userCacheKey);
CacheControl::clearCache($this->roleCacheKey);
CacheControl::clearCache($this->userCacheKey);
}

/**
Expand All @@ -62,8 +62,8 @@ public function deleted(Role $role)
*/
public function restored(Role $role)
{
CacheControl::forgetCache($this->roleCacheKey);
CacheControl::forgetCache($this->userCacheKey);
CacheControl::clearCache($this->roleCacheKey);
CacheControl::clearCache($this->userCacheKey);
}

/**
Expand All @@ -74,7 +74,7 @@ public function restored(Role $role)
*/
public function forceDeleted(Role $role)
{
CacheControl::forgetCache($this->roleCacheKey);
CacheControl::forgetCache($this->userCacheKey);
CacheControl::clearCache($this->roleCacheKey);
CacheControl::clearCache($this->userCacheKey);
}
}
Loading

0 comments on commit ea952a5

Please sign in to comment.