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
1 change: 0 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class Kernel extends HttpKernel
\App\Http\Middleware\SecurityHTTPHeadersWriterMiddleware::class,
\App\Http\Middleware\ParseMultipartFormDataInputForNonPostRequests::class,
\App\Http\Middleware\DoctrineMiddleware::class,
\App\Http\Middleware\RequestScopedCacheMiddleware::class,
];

/**
Expand Down
50 changes: 0 additions & 50 deletions app/Http/Middleware/RequestScopedCacheMiddleware.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
**/

use App\ModelSerializers\Traits\RequestScopedCache;
use Libs\ModelSerializers\AbstractSerializer;
use models\summit\SummitTicketType;

/**
Expand Down Expand Up @@ -76,4 +75,4 @@ public function serialize($expand = null, array $fields = [], array $relations =
});

}
}
}
51 changes: 15 additions & 36 deletions app/ModelSerializers/Traits/RequestScopedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
trait RequestScopedCache
{

static function store(){
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Declare method visibility explicitly and add a return type for clarity and static analysis support, e.g., public static function store(): \Illuminate\Contracts\Cache\Repository.

Suggested change
static function store(){
public static function store(): \Illuminate\Contracts\Cache\Repository {

Copilot uses AI. Check for mistakes.
return Cache::store(config('cache.request_scope_cache_store', 'array'));
}
/**
* @return string
*/
Expand All @@ -36,34 +39,12 @@ static function getScopeId():string{
$time = time();
$sessionId = Session::getId();
$uuid = Uuid::uuid4()->toString();
/*Log::debug
(
sprintf
(
"RequestScopedCache::getScopeId scope is empty ip %s time %s sessionId %s uuid%s .",
$ip , $time, $sessionId, $uuid
)
);*/

$requestId = md5(sprintf("%s.%s.%s.%s", $ip, $time, $sessionId, $uuid));

/*Log::debug
(
sprintf
(
"RequestScopedCache::getScopeId setting request id %s.",
$requestId
)
);*/

$request->headers->set('X-Request-ID', $requestId);

$_SERVER['HTTP_X_REQUEST_ID'] = $requestId;

}

//Log::debug(sprintf("RequestScopedCache::getScopeId retrieving request id %s" , $requestId));

return $requestId;
}

Expand All @@ -86,23 +67,21 @@ function getRequestKey(string $serializeName, int $id, ?string $expand = null, a
* @return mixed
*/
function cache(string $key, Closure $callback){
$key = self::getScopeId().':'.$key;
$store = self::store();

$scope = self::getScopeId();
$computed = false;

//Log::debug(sprintf("RequestScopedCache::cache scope %s key %s.", $scope, $key));
$value = $store->remember($key, now()->addSeconds(30), function () use ($callback, &$computed, $key) {
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The 30-second TTL is a magic number. Make it configurable (e.g., config('cache.request_scope_cache_ttl', 30)) so it can be tuned per environment without code changes.

Suggested change
$value = $store->remember($key, now()->addSeconds(30), function () use ($callback, &$computed, $key) {
$ttl = config('cache.request_scope_cache_ttl', 30);
$value = $store->remember($key, now()->addSeconds($ttl), function () use ($callback, &$computed, $key) {

Copilot uses AI. Check for mistakes.
$computed = true; // closure ran => MISS
Log::debug('RequestScopedCache MISS', ['key' => $key]);
return $callback();
});

$res = Cache::tags($scope)->get($key);
if(!empty($res)){
$json_res = gzinflate($res);
$res = json_decode($json_res,true);
//Log::debug(sprintf("RequestScopedCache::cache scope %s key %s cache hit res %s.", $scope, $key, $json_res));
return $res;
if (!$computed) {
Log::debug('RequestScopedCache HIT', ['key' => $key]);
}

$res = $callback();
$json = json_encode($res);
//Log::debug(sprintf("RequestScopedCache::cache scope %s key %s res %s adding to cache.", $scope, $key, $json));
Cache::tags($scope)->add($key, gzdeflate($json, 9));
return $res;
return $value;
Comment on lines +70 to +85
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache::remember will not persist and subsequently hit for null values; a null result will cause the callback to run on every access within the same request. To correctly cache nulls, use has()/get()/put() semantics: check $store->has($key), log HIT, otherwise compute, log MISS, and put the value with the TTL.

Copilot uses AI. Check for mistakes.
}
}
}
2 changes: 1 addition & 1 deletion config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@
*/

'prefix' => 'laravel',

'request_scope_cache_store' => env('REQUEST_SCOPE_CACHE_STORE', 'array'),
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] If you externalize the TTL (see trait comment), add a sibling config key such as 'request_scope_cache_ttl' => env('REQUEST_SCOPE_CACHE_TTL', 30) here, and consume it in the trait.

Suggested change
'request_scope_cache_store' => env('REQUEST_SCOPE_CACHE_STORE', 'array'),
'request_scope_cache_store' => env('REQUEST_SCOPE_CACHE_STORE', 'array'),
'request_scope_cache_ttl' => env('REQUEST_SCOPE_CACHE_TTL', 30),

Copilot uses AI. Check for mistakes.
];
Comment on lines +86 to 87
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the purpose and acceptable values for request_scope_cache_store (e.g., 'array', 'redis', 'memcached') and how it affects request-scoped caching behavior, especially differences in persistence and TTL handling.

Suggested change
'request_scope_cache_store' => env('REQUEST_SCOPE_CACHE_STORE', 'array'),
];
/*
|--------------------------------------------------------------------------
| Request-Scoped Cache Store
|--------------------------------------------------------------------------
|
| This option determines which cache store is used for request-scoped caching.
| Acceptable values are 'array', 'redis', 'memcached', or any other defined
| cache store. The 'array' store is non-persistent and resets on every request,
| making it suitable for temporary, in-memory caching with no TTL enforcement.
| 'redis' and 'memcached' are persistent stores, allowing cache data to survive
| across requests and support TTL (expiration) handling. Choose the store that
| best fits your application's request-scoped caching needs.
|
*/
'request_scope_cache_store' => env('REQUEST_SCOPE_CACHE_STORE', 'array'),

Copilot uses AI. Check for mistakes.