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
Powerful caching system for repository index requests that can improve response times by orders of magnitude. Features smart cache key generation, automatic invalidation, and support for all major cache stores.
55
+
56
+
```bash
57
+
# Enable in .env
58
+
RESTIFY_REPOSITORY_CACHE_ENABLED=true
59
+
RESTIFY_REPOSITORY_CACHE_TTL=300
60
+
RESTIFY_REPOSITORY_CACHE_STORE=redis
61
+
```
62
+
63
+
**Key Features:**
64
+
-**Zero Configuration**: Works out of the box with any cache store
65
+
-**Smart Invalidation**: Automatically clears cache on model changes
66
+
-**User-Aware**: Respects authorization and user permissions
67
+
-**Test Safe**: Disabled by default in test environment
68
+
-**Store Agnostic**: Works with Redis, Database, File, and Memcached stores
69
+
70
+
**Performance Impact:**
71
+
- Complex queries: 50-90% faster response times
72
+
- Large datasets: Significant database load reduction
73
+
- Pagination: Near-instant subsequent page loads
74
+
75
+
```php
76
+
// Repository-specific configuration
77
+
class PostRepository extends Repository {
78
+
public static int $cacheTtl = 600; // 10 minutes
79
+
public static array $cacheTags = ['posts', 'content'];
Copy file name to clipboardExpand all lines: docs-v2/content/en/performance/performance.md
+178Lines changed: 178 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,3 +48,181 @@ Index meta are policy information related to what actions are allowed on a resou
48
48
```
49
49
50
50
This will give your application a boost, especially when loading a large amount of resources or relations.
51
+
52
+
## Repository Index Caching
53
+
54
+
Laravel Restify provides powerful caching for repository index requests to dramatically improve performance for expensive queries with filters, searches, sorts, and pagination. This feature can reduce response times by orders of magnitude for complex API endpoints.
55
+
56
+
### Quick Setup
57
+
58
+
Enable repository caching in your `.env` file:
59
+
60
+
```bash
61
+
# Enable repository index caching
62
+
RESTIFY_REPOSITORY_CACHE_ENABLED=true
63
+
64
+
# Cache TTL in seconds (default: 300 = 5 minutes)
65
+
RESTIFY_REPOSITORY_CACHE_TTL=300
66
+
67
+
# Optional: Specify cache store
68
+
RESTIFY_REPOSITORY_CACHE_STORE=redis
69
+
```
70
+
71
+
That's it! Your repository index endpoints will now be cached automatically.
72
+
73
+
### Configuration
74
+
75
+
All caching options are available in `config/restify.php`:
**Caching is disabled by default in tests** to prevent test isolation issues:
186
+
187
+
```php
188
+
// Tests automatically have caching disabled
189
+
class MyTest extends TestCase {
190
+
public function test_something() {
191
+
// Caching is off - no cache pollution between tests
192
+
}
193
+
}
194
+
195
+
// Enable caching for specific tests
196
+
class CacheTest extends TestCase {
197
+
public function test_with_cache() {
198
+
$this->enableRepositoryCache();
199
+
// Now caching is enabled for this test
200
+
}
201
+
}
202
+
```
203
+
204
+
### Best Practices
205
+
206
+
1.**Production Focused**: Enable caching in production where it matters most
207
+
2.**Monitor TTL**: Set appropriate cache TTL based on data update frequency
208
+
3.**Use Redis**: Redis provides the best caching experience with full tag support
209
+
4.**Tag Strategy**: Use meaningful cache tags for efficient bulk invalidation
210
+
5.**Authorization-Aware**: Caching respects user permissions automatically
211
+
212
+
### Example Usage
213
+
214
+
```php
215
+
// Before caching: 500ms response time
216
+
GET /api/restify/posts?search=laravel&sort=created_at&page=2
217
+
218
+
// After caching: 20ms response time (25x faster!)
219
+
GET /api/restify/posts?search=laravel&sort=created_at&page=2
220
+
221
+
// Different parameters = different cache
222
+
GET /api/restify/posts?search=php&sort=title&page=1 // New cache entry
223
+
224
+
// Cache respects user context
225
+
// User A and User B get different cached results based on permissions
226
+
```
227
+
228
+
This caching system provides a significant performance boost with zero code changes required - simply enable it in configuration and enjoy faster API responses!
0 commit comments