Local Cache Management¶
Tools and practices for managing the local build cache.
Cache Tools¶
View Statistics¶
Output:
Cache Statistics:
Enabled: true
Mode: local
Directory: /home/user/.cache/rninja
Local Cache:
Total entries: 1,234
Total size: 456.7 MB
Hit rate: 87.3%
Session:
Hits: 45
Misses: 12
Stores: 12
Run Garbage Collection¶
Removes:
- Entries older than
max_age - Oldest entries when exceeding
max_size - Orphaned blob files
- Corrupt entries
Check Health¶
Verifies:
- Database integrity
- Blob storage consistency
- Index correctness
Maintenance Tasks¶
Regular Cleanup¶
Schedule periodic garbage collection:
Or run manually after large builds:
Disk Space Recovery¶
When disk is full:
# Check current usage
rninja -t cache-stats
# Aggressive cleanup (reduce to 1GB)
RNINJA_CACHE_MAX_SIZE=1G rninja -t cache-gc
# Or clear entirely
rm -rf ~/.cache/rninja
Health Monitoring¶
Check periodically:
# Quick health check
rninja -t cache-health
# Full verification
rninja -t cache-health
rninja -t cache-gc
rninja -t cache-stats
Cache Reset¶
Soft Reset (GC)¶
Cleans up while preserving valid entries:
Hard Reset (Clear)¶
Removes entire cache:
Selective Reset¶
Remove specific entries (not directly supported, use time-based):
# Set aggressive max_age to remove old entries
RNINJA_CACHE_MAX_AGE=3600 rninja -t cache-gc # Keep only last hour
Monitoring¶
Size Tracking¶
Hit Rate Tracking¶
Automated Monitoring¶
#!/bin/bash
# cache_monitor.sh
while true; do
STATS=$(rninja -t cache-stats 2>/dev/null)
SIZE=$(echo "$STATS" | grep "Total size" | awk '{print $3, $4}')
RATE=$(echo "$STATS" | grep "Hit rate" | awk '{print $3}')
echo "$(date): Size=$SIZE, HitRate=$RATE"
sleep 300 # Every 5 minutes
done
Backup and Restore¶
Backup Cache¶
Restore Cache¶
Share Cache¶
For machines with identical toolchains:
# On source machine
tar -czf cache.tar.gz -C ~/.cache rninja
# Transfer and extract on target
scp cache.tar.gz target:/tmp/
ssh target 'tar -xzf /tmp/cache.tar.gz -C ~/.cache'
Toolchain Compatibility
Cache is only valid for identical toolchains. Different compiler versions will cause cache misses (not errors).
Troubleshooting¶
Cache Growing Too Fast¶
# Check for large artifacts
du -sh ~/.cache/rninja/blobs/* | sort -h | tail -20
# Set size limit
export RNINJA_CACHE_MAX_SIZE=5G
rninja -t cache-gc
Low Hit Rate¶
Database Errors¶
# Try health check first
rninja -t cache-health
# If errors persist, reset cache
rm -rf ~/.cache/rninja
Slow Cache Access¶
# Check disk performance
dd if=/dev/zero of=/tmp/test bs=1M count=100
rm /tmp/test
# Move cache to faster storage
export RNINJA_CACHE_DIR=/ssd/rninja-cache
Best Practices¶
Regular Maintenance¶
- Run
cache-gcweekly - Monitor hit rates
- Check health after system issues
Size Management¶
- Set appropriate
max_sizefor your disk - Use
max_agefor automatic cleanup - Monitor cache growth
Multiple Projects¶
Cache handles multiple projects automatically:
- Each project has unique cache keys
- No conflicts between projects
- Shared artifacts deduplicated
CI Integration¶
For CI, consider:
- Using remote cache instead
- Limiting local cache size
- Clearing cache between major builds
Automation Scripts¶
Weekly Maintenance¶
#!/bin/bash
# /etc/cron.weekly/rninja-maintenance
# Garbage collection
rninja -t cache-gc
# Health check
if ! rninja -t cache-health > /dev/null 2>&1; then
echo "Warning: rninja cache health check failed" | mail -s "rninja cache warning" [email protected]
fi
# Log stats
rninja -t cache-stats >> /var/log/rninja-cache.log