Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MiguelNavas19/miapibcv/llms.txt
Use this file to discover all available pages before exploring further.
Why Caching?
Mi API BCV implements an intelligent caching strategy to:- Reduce database queries: Avoid hitting the database on every API request
- Improve response times: Serve cached data in milliseconds instead of querying
- Minimize resource usage: Lower CPU and database load on high-traffic scenarios
- Provide consistency: Same data for all requests within the cache window
Cache Implementation
Cache Configuration
By default, Mi API BCV uses database caching as configured in.env:
.env
file- File-based cachingredis- Redis (requires Redis server)memcached- Memcached (requires Memcached server)database- Database table (default, no additional setup needed)
Cache Duration
All cached data has a 1-hour TTL (Time To Live):app/Http/Controllers/Api/ScraperController.php
- Exchange rates don’t change frequently during the day
- Balances freshness with performance
- Scheduled updates run multiple times, ensuring fresh data
Cache Keys
Cache keys are date-based to ensure proper isolation:- Each date has its own cache entry
- Historical queries get their own cache
- Today’s data is cached separately from yesterday’s
Example Cache Keys
- Today’s rates:
tasas_bancos_2026-03-04 - Yesterday’s rates:
tasas_bancos_2026-03-03 - Specific date:
tasas_bancos_2026-03-01
Cache Flow
Cache Hit: Return Immediately
If cached data exists and hasn’t expired, return it instantly without touching the database.
Automatic Cache Invalidation
When new exchange rates are scraped and saved, the cache is automatically invalidated using a model observer:app/Observers/ReferenceObserver.php
Observer Registration
The observer is registered using PHP attributes:app/Models/ReferenceRecord.php
This approach ensures cache consistency without manual cache management in controllers or commands.
Cache Warming
The system implements cache warming through the scheduledrates:update command:
routes/console.php
- Command fetches fresh rates from banks
- New records are saved to database
- Observer automatically invalidates old cache
- Next API request rebuilds cache with fresh data
This “warm cache” strategy ensures the first request after an update gets fresh data and caches it for subsequent requests.
Cache Performance
Without Cache
With Cache (Hit)
Cache Operations
Viewing Cache Contents
For debugging, you can inspect cache contents using tinker:Manually Clearing Cache
Checking Cache Driver
Advanced Caching Strategies
Using Redis for Better Performance
For high-traffic applications, switch to Redis:Cache Tags (Redis/Memcached only)
If using Redis, you can implement cache tags for better organization:Cache Best Practices
1. Use Date-Based Keys
Always include the date in cache keys to prevent data from different days mixing:2. Set Appropriate TTL
1 hour is ideal for exchange rates, but adjust based on your needs:3. Handle Cache Failures Gracefully
4. Monitor Cache Hit Ratio
Track cache effectiveness:Troubleshooting
Cache not working
Cache not working
Check your cache configuration:Verify
CACHE_STORE in .env matches your setup.Stale data being served
Stale data being served
Cache may not be invalidating properly. Check:
- Is the observer registered?
- Are database writes actually saving?
- Try manual cache clear:
Cache table doesn't exist
Cache table doesn't exist
Run migrations to create the cache table:The
0001_01_01_000001_create_cache_table migration creates it.Redis connection errors
Redis connection errors
Ensure Redis is running:Check connection settings in
.env.Cache Monitoring
For production environments, monitor cache performance:Summary
Mi API BCV’s caching strategy:- 1-hour TTL balances freshness and performance
- Date-based keys ensure data isolation
- Automatic invalidation via model observers
- Database driver for simple setup (switchable to Redis)
- Cache warming through scheduled commands
- ~10x performance improvement on cache hits