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
Open `config/cache.php` and add a new item to the `stores` array.
39
+
Open `config/cache.php` and add a new store.
42
40
43
41
```php
44
42
'stores' => [
45
43
// ... other stores
46
44
'playback' => [
47
45
'driver' => 'redis',
48
-
// We strongly recommend using a different
49
-
// connection (another redis DB) in production.
46
+
// 👇🏻 Caution!
47
+
// You probably don't want to use the cache connection in production.
48
+
// Playback cache can grow to a huge size for busy applications.
49
+
// Make sure your redis instance is ready.
50
50
'connection' => 'cache',
51
51
],
52
52
]
53
53
```
54
54
55
55
💡 **Apply the middleware**
56
56
57
-
Just apply the `Swiftmade\Swiftmade\Playback` middleware to your endpoints. You can see how here:
57
+
Just apply the `Swiftmade\Playback\Playback` middleware to your endpoints. There are many ways of doing it, so here's a link to the docs:
58
58
59
59
-https://laravel.com/docs/8.x/middleware
60
60
61
61
## Use
62
62
63
-
+ The client must supply an idempotency key. Otherwise, the middleware won't execute.
63
+
Even when middleware is active on a route, it's business as usual unless the client sends an `Idempotency-Key` in their request header.
64
64
65
65
```
66
66
Idempotency-Key: preferrably uuid4, but anything flies
67
67
```
68
68
69
-
+ The server will look the key up. If there's a match, exactly that response will be returned.
69
+
Once Playback detects a key, it'll look it up in redis. If found, it will serve the same response **without hitting your controller action again**. You can know that happened by looking at the response headers. If it contains `Is-Playback`, you know it's just a repetition.
70
70
71
-
You can know that the response is a playback from the response headers:
71
+
If the key is not found during the lookup, a race begins. The first request to acquire the redis lock gets to process the request and cache the response. Any other unlucky requests that land during that time window will return `425` status code.
72
72
73
-
```
74
-
Is-Playback: your idempotency key
75
-
```
73
+
#### Errors:
74
+
75
+
+**400 Bad Request**
76
+
If you get back status `400`, it means your request was not identical to the cached one. It's the client's responsibility to repeat the exact same request. This is also why another user can't steal a response just by stealing/guessing the idempotency key. The cookies/authentication token would be different, which fails the signature check.
76
77
77
-
+ If you get back status `400`, it means the following request was not identical with the cached one. Just use another idempotency key, if you mean to execute a fresh request.
78
+
+**425 Too Early**
79
+
If you get this error, it means you retried too fast after your initial attempt. Don't panic and try again a second later or so. It's perfectly safe to do so!
78
80
79
-
+If you get back status `425`, it means you retried too fast. It's perfectly safe to try again later.
81
+
🚨 Pro tip: If your controller action returns 4xx or 3xx status code, Playback won't cache the response. It's your responsibility to ensure no side effects take place (or they are rolled back) if a validation fails, a related db record was not found, etc and therefore the response status is 4xx or 3xx.
0 commit comments