Caching in Magento 2
Caching is cruciaal voor Magento performance. Zonder caching is Magento traag door de complexe PHP verwerking.
Cache types overzicht
Navigatie
System > Cache Management
Beschikbare cache types
| Cache | Wat het doet |
|-------|--------------|
| Configuration | System configuratie |
| Layouts | Layout XML instructies |
| Blocks HTML | HTML blok output |
| Collections Data | Database query resultaten |
| Reflection Data | API interfaces |
| Database DDL | Database schema |
| Compiled Config | Gecompileerde configuratie |
| EAV types | Entity attribute values |
| Customer Notification | Klant notificaties |
| Integrations Config | Integratie configuratie |
| Integrations API | API configuratie |
| Full Page Cache | Complete pagina output |
| Translation | Vertalingen |
| Web Services Config | SOAP/REST configuratie |
Full-Page Cache (FPC)
Wat is FPC?
Full-Page Cache slaat complete HTML pagina's op. Bezoekers krijgen direct de cached HTML zonder PHP verwerking.
FPC Backends
Built-in (bestand):
- Eenvoudig, geen extra setup
- Geschikt voor kleine shops
- Langzamer dan Varnish
Varnish:
- Veel sneller
- Industry standard voor high-traffic
- Vereist server configuratie
FPC inschakelen
Stores > Configuration > Advanced > System > Full Page Cache
- Caching Application: Built-in of Varnish
- TTL for public content: 86400 (1 dag)
Varnish configureren
Varnish installeren
Varnish moet op server niveau worden geïnstalleerd.
VCL genereren
Stores > Configuration > Advanced > System > Full Page Cache
Health check URL
Configureer de health check URL voor Varnish status monitoring.
Redis configureren
Wat is Redis?
Redis is een in-memory data store. Veel sneller dan filesystem cache.
Redis voor cache backend
In app/etc/env.php:
``php
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '0'
]
]
]
]
`
Redis voor sessions
`php
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'database' => '2'
]
]
`
Aparte databases
Gebruik verschillende Redis databases voor:
- Cache: database 0
- FPC: database 1
- Sessions: database 2
Cache beheren
Via Admin
System > Cache Management
- Flush Magento Cache: Alleen Magento cache
- Flush Cache Storage: Alle cache (ook externe zoals Redis)
Selectief invalideren
Vink specifieke cache types aan en klik "Refresh".
Via CLI
`bash
# Status bekijken
bin/magento cache:status
# Specifieke cache flushen
bin/magento cache:flush config
bin/magento cache:flush full_page
# Alle cache flushen
bin/magento cache:flush
# Cache uitschakelen (development)
bin/magento cache:disable full_page
`
Cache invalidatie
Automatische invalidatie
Magento invalideert automatisch relevante cache bij:
- Product wijzigingen
- Categorie wijzigingen
- CMS wijzigingen
- Configuratie wijzigingen
"Invalidated" status
Als je "Invalidated" ziet in Cache Management:
De data is gewijzigd
Cache moet worden gerefreshed
Klik "Refresh" voor betrokken types
Cache tags
Magento gebruikt tags om gerelateerde cache entries te invalideren. Bijv.: wijzig product → alle gerelateerde cache entries geïnvalideerd.
Development mode caching
Development mode
`bash
bin/magento deploy:mode:set developer
`
In developer mode:
- Static files worden on-the-fly gegenereerd
- Sommige caches automatisch uitgeschakeld
Caching uitschakelen voor development
`bash
bin/magento cache:disable config layout block_html
`
Let op performance
Zonder caching is Magento zeer traag. Enable caching voor performance testing.
Hole punching
Wat is het?
Sommige pagina-onderdelen moeten dynamisch zijn (winkelwagen, login status). "Hole punching" laat specifieke blokken door cache heen.
Private content
Magento laadt private content via JavaScript na pagina load:
- Mini cart
- Welcome message
- Compare products
Configuratie
Blokken markeren als "cacheable=false" in layout XML zorgt voor hole punching.
Performance monitoring
Cache hit rate
Monitor hoeveel requests uit cache worden geserveerd.
Varnish:
`bash
varnishstat
``
Response times
Meet TTFB (Time To First Byte) voor cached vs uncached pages.
Tools
- New Relic
- Blackfire
- Tideways
Veelvoorkomende problemen
Cache wordt niet gelezen
Checklist:
Wijzigingen niet zichtbaar
Out of memory
Redis of filesystem vol:
- Verhoog Redis maxmemory
- Clean oude cache files
- Check cache configuration
Best practices
Staging/Production
Altijd alle caches enabled op productie.
Development
Disable selectief, maar test met caches enabled voor productie-representatieve tests.
Cache warming
Na deployment of cache flush:
- Crawl belangrijkste pagina's
- Of gebruik cache warming extensie
Monitoring
Monitor cache health:
- Hit rates
- Memory usage
- Response times