Finch biedt route-niveau caching via de .cache()-uitbreidingsmethode. Gecachede antwoorden hoeven de controller niet opnieuw uit te voeren.
router
.get('/api/products', ProductController().index)
.cache(duration: Duration(minutes: 10));
| Parameter |
Type |
Beschrijving |
duration |
Duration |
Geldigheidsduur van de cache |
cacheSource |
CacheSource |
memory of file (standaard: memory) |
cacheType |
CacheParam |
Hoe de cachesleutel wordt bepaald |
| Waarde |
Beschrijving |
CacheParam.all |
Alle verzoeken als één behandelen (gedeelde cache) |
CacheParam.user |
Aparte cache per gebruiker |
CacheParam.params |
Aparte cache per parametercombinatie |
CacheParam.userAndParams |
Combinatie van gebruiker en parameters |
// In-memory cache (standaard) — sneller, verdwijnt bij herstart
.cache(
duration: Duration(hours: 1),
cacheSource: CacheSource.memory,
)
// Schijfcache — langzamer, blijft bij herstart behouden
.cache(
duration: Duration(hours: 24),
cacheSource: CacheSource.file,
)
// Startpagina: zelfde voor iedereen, 5 minuten
router
.get('/home', HomeController().index)
.cache(
duration: Duration(minutes: 5),
cacheType: CacheParam.all,
);
// Dashboard: aparte cache per gebruiker, 30 minuten
router
.get('/dashboard', DashboardController().index)
.cache(
duration: Duration(minutes: 30),
cacheType: CacheParam.user,
);
// Zoeken: op basis van queryparameters, 2 minuten
router
.get('/search', SearchController().results)
.cache(
duration: Duration(minutes: 2),
cacheType: CacheParam.params,
);
// Alle caches leegmaken
RouteCache.clearAllCache();
// Cache van een specifieke route leegmaken
RouteCache.clearCache('/api/products');