-
Notifications
You must be signed in to change notification settings - Fork 2
fix: refactor Request scope cache feature #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,9 @@ | |||||||
| trait RequestScopedCache | ||||||||
| { | ||||||||
|
|
||||||||
| static function store(){ | ||||||||
| return Cache::store(config('cache.request_scope_cache_store', 'array')); | ||||||||
| } | ||||||||
| /** | ||||||||
| * @return string | ||||||||
| */ | ||||||||
|
|
@@ -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; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -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) { | ||||||||
|
||||||||
| $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
AI
Oct 15, 2025
There was a problem hiding this comment.
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.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -83,5 +83,5 @@ | |||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 'prefix' => 'laravel', | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 'request_scope_cache_store' => env('REQUEST_SCOPE_CACHE_STORE', 'array'), | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| '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
AI
Oct 15, 2025
There was a problem hiding this comment.
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.
| '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'), |
There was a problem hiding this comment.
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.