You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,38 +4,45 @@ The logic of Async Cache PHP is built on a **Middleware Pipeline**. Every reques
4
4
5
5
## Default Pipeline
6
6
7
-
When you create a manager using `AsyncCacheBuilder`, the following middleware stack is assembled (in order):
7
+
When you create a manager using `AsyncCacheManager::configure()`, the following middleware stack is assembled (in order):
8
8
9
9
1.**`CoalesceMiddleware`**: Prevents duplicate requests for the same key happening at the same time. If 10 requests come in for `key_A`, only one factory is executed, and the result is shared.
10
10
2.**`StaleOnErrorMiddleware`**: If the factory fails (throws an exception), this middleware tries to return stale data from the cache instead of propagating the error.
11
11
3.**`CacheLookupMiddleware`**: Checks if the item is in the cache. Handles `CacheStrategy` logic (Strict vs Background) and X-Fetch calculations.
12
-
4.**`AsyncLockMiddleware`**: Acquires a lock before calling the factory to ensure only one process generates the data (Cache Stampede protection via locking).
13
-
5.**`SourceFetchMiddleware`**: The final handler. It executes the user's factory function and saves the result to the storage.
12
+
4.**`TagValidationMiddleware`**: Validates cache tags and handles tag-based invalidation.
13
+
5.**`AsyncLockMiddleware`**: Acquires a lock before calling the factory to ensure only one process generates the data (Cache Stampede protection via locking).
14
+
6.**`SourceFetchMiddleware`**: The final handler. It executes the user's factory function and saves the result to the storage.
15
+
16
+
## Additional Middleware
17
+
18
+
The library also provides optional middleware that can be added to the pipeline:
19
+
20
+
-**`CircuitBreakerMiddleware`**: Implements the circuit breaker pattern to prevent cascading failures when external services are down.
21
+
-**`RetryMiddleware`**: Automatically retries failed operations with configurable backoff strategies.
14
22
15
23
## Custom Middleware
16
24
17
-
You can inject your own middleware into the pipeline using the `AsyncCacheBuilder`.
25
+
You can inject your own middleware into the pipeline using the configuration builder.
18
26
19
27
### 1. Implement `MiddlewareInterface`
20
28
21
29
```php
22
30
use Fyennyi\AsyncCache\Middleware\MiddlewareInterface;
23
31
use Fyennyi\AsyncCache\Core\CacheContext;
24
-
use Fyennyi\AsyncCache\Core\Future;
32
+
use React\Promise\PromiseInterface;
25
33
26
34
class MyCustomMiddleware implements MiddlewareInterface
27
35
{
28
-
public function handle(CacheContext $context, callable $next): Future
36
+
public function handle(CacheContext $context, callable $next): PromiseInterface
29
37
{
30
38
// Pre-processing
31
39
echo "Processing key: " . $context->key . "\n";
32
40
33
41
// Call next middleware
34
-
$future = $next($context);
35
-
36
-
// Post-processing (using Future callbacks)
37
-
return $future->onResolve(function ($value) {
42
+
return $next($context)->then(function ($value) {
43
+
// Post-processing
38
44
echo "Finished!\n";
45
+
return $value;
39
46
});
40
47
}
41
48
}
@@ -44,11 +51,110 @@ class MyCustomMiddleware implements MiddlewareInterface
Custom middleware is appended to the **end** of the stack by default (before `SourceFetchMiddleware` but after others). If you need precise control over the order, you might need to construct the `AsyncCacheManager` manually, passing the full array of middleware.
|`compression_threshold`|`int`|`1024`| Minimum data size in bytes to trigger compression. |
144
+
|`fail_safe`|`bool`|`true`| Catch cache adapter exceptions and treat as misses. |
145
+
146
+
## `CacheOptionsBuilder`
147
+
148
+
Fluent builder for `CacheOptions`.
149
+
150
+
```php
151
+
$options = CacheOptionsBuilder::create()
152
+
->withTtl(300)
153
+
->withStrategy(CacheStrategy::Background)
154
+
->withStaleGracePeriod(3600)
155
+
->withCompression(true, 2048)
156
+
->withTags(['users', 'api'])
157
+
->build();
158
+
```
159
+
EOF! warning
160
+
Custom middleware is prepended to the stack (runs before default middleware). If you need precise control over the order, you might need to construct the `AsyncCacheManager` manually, passing the full array of middleware.
0 commit comments