forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCache.php
More file actions
145 lines (123 loc) · 4.75 KB
/
Cache.php
File metadata and controls
145 lines (123 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Router\Attributes;
use Attribute;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\I18n\Time;
/**
* Cache Attribute
*
* Caches the response of a controller method at the server level for a specified duration.
* This is server-side caching to avoid expensive operations, not browser-level caching.
*
* Usage:
* ```php
* #[Cache(for: 3600)] // Cache for 1 hour
* #[Cache(for: 300, key: 'custom_key')] // Cache with custom key
* ```
*
* Limitations:
* - Only caches GET requests; POST, PUT, DELETE, and other methods are ignored
* - Streaming responses or file downloads may not cache properly
* - Cache key includes HTTP method, path, query string, and possibly user_id(), but not request headers
* - Does not automatically invalidate related cache entries
* - Cookies set in the response are cached and reused for all subsequent requests
* - Large responses may impact cache storage performance
* - Browser Cache-Control headers do not affect server-side caching behavior
*
* Security Considerations:
* - Ensure cache backend is properly secured and not accessible publicly
* - Be aware that authorization checks happen before cache lookup
*/
#[Attribute(Attribute::TARGET_METHOD)]
class Cache implements RouteAttributeInterface
{
public function __construct(
public int $for = 3600,
public ?string $key = null,
) {
}
public function before(RequestInterface $request): RequestInterface|ResponseInterface|null
{
// Only cache GET requests
if ($request->getMethod() !== 'GET') {
return null;
}
// Check cache before controller execution
$cacheKey = $this->key ?? $this->generateCacheKey($request);
$cached = cache($cacheKey);
// Validate cached data structure
if ($cached !== null && (is_array($cached) && isset($cached['body'], $cached['headers'], $cached['status']))) {
$response = service('response');
$response->setBody($cached['body']);
$response->setStatusCode($cached['status']);
// Mark response as served from cache to prevent re-caching
$response->setHeader('X-Cached-Response', 'true');
// Restore headers from cached array of header name => value strings
foreach ($cached['headers'] as $name => $value) {
$response->setHeader($name, $value);
}
$time = Time::now()->getTimestamp();
$response->setHeader('Age', (string) ($time - ($cached['timestamp'] ?? $time)));
return $response;
}
return null; // Continue to controller
}
public function after(RequestInterface $request, ResponseInterface $response): ?ResponseInterface
{
// Don't re-cache if response was already served from cache
if ($response->hasHeader('X-Cached-Response')) {
// Remove the marker header before sending response
$response->removeHeader('X-Cached-Response');
return null;
}
// Only cache GET requests
if ($request->getMethod() !== 'GET') {
return null;
}
$cacheKey = $this->key ?? $this->generateCacheKey($request);
// Convert Header objects to strings for caching
$headers = [];
foreach ($response->headers() as $name => $header) {
// Handle both single Header and array of Headers
if (is_array($header)) {
// Multiple headers with same name
$values = [];
foreach ($header as $h) {
$values[] = $h->getValueLine();
}
$headers[$name] = implode(', ', $values);
} else {
// Single header
$headers[$name] = $header->getValueLine();
}
}
$data = [
'body' => $response->getBody(),
'headers' => $headers,
'status' => $response->getStatusCode(),
'timestamp' => Time::now()->getTimestamp(),
];
cache()->save($cacheKey, $data, $this->for);
return $response;
}
protected function generateCacheKey(RequestInterface $request): string
{
return 'route_cache_' . hash(
'xxh128',
$request->getMethod() .
$request->getUri()->getPath() .
$request->getUri()->getQuery() .
(function_exists('user_id') ? user_id() : ''),
);
}
}