Module 2
Topic 8

Caching

Storing data locally for faster access, reduced network usage, and offline support.

cache_manager package
What Is Caching?

Caching is the process of storing copies of data in a local storage layer so that future requests for that data can be served faster and with less network usage . It's essential for:

  • Performance – Faster data access (reading from disk or memory is faster than network)
  • Bandwidth – Reduces data usage and API calls
  • Offline support – Users can access data without an internet connection
  • User experience – No loading spinners for cached content

🎯 The Caching Hierarchy

  • Memory Cache – Fastest, but cleared when app restarts
  • Disk Cache – Slower than memory, but persists across app restarts
  • Network – Slowest, but always fresh
Caching Strategies

There are three main caching strategies used in Flutter apps:

📦
Memory Cache
Store data in RAM for fast access
💾
Disk Cache
Persist data to storage (SharedPreferences, Hive, SQLite)
🔄
Network Cache
Use HTTP caching headers (ETag, Cache-Control)
Simple Memory Cache

A memory cache stores data in a Map or Cache object. It's fast but cleared when the app restarts.

cache_manager Package

The cache_manager package provides a comprehensive caching solution with disk persistence and HTTP caching support.

📦 cache_manager Features

  • Disk caching – Persist data to local storage
  • HTTP caching – Respects Cache-Control headers
  • Customizable – Configure cache duration and size
  • Stream support – Works with images and files
  • Offline-first – Serve cached data when offline
Offline-First Strategy

An offline-first strategy ensures your app works even without an internet connection by serving cached data when the network is unavailable.

Complete Example: Cached Posts App

Here's a complete app that uses caching to display posts with offline support.

Step-by-Step Explanation
1.
Choose a caching strategy – Decide between memory cache, disk cache, or both.
2.
Implement the cache layer – Use SharedPreferences , Hive , or cache_manager for disk caching.
3.
Check cache before making network requests – If data is cached and valid, return it immediately.
4.
Save responses to cache – After a successful network request, save the data to cache.
5.
Handle offline scenarios – Fall back to cache when the network is unavailable.
6.
Provide cache management – Allow users to refresh or clear the cache.
Caching Best Practices

✅ Do's

  • Cache data that is frequently accessed and doesn't change often
  • Set appropriate cache expiration times (TTL)
  • Handle offline scenarios gracefully
  • Provide a way to refresh or clear the cache
  • Use cache_manager for HTTP caching
  • Store cache keys consistently
  • Show cache status to users (e.g., "Cached" badge)

❌ Don'ts

  • Don't cache sensitive data without encryption
  • Don't cache indefinitely (always set a TTL)
  • Don't ignore cache expiration
  • Don't cache large files without size limits
  • Don't forget to clear cache when logging out
Common Mistakes
❌ Mistake 1: Not setting cache expiration

Without a TTL (Time To Live), cached data can become stale and show outdated information.

✅ Correct: Set a cache duration

Always set a reasonable expiration time based on how often the data changes.

❌ Mistake 2: Caching everything

Not all data should be cached. Only cache frequently accessed, static, or semi-static data.

✅ Correct: Cache selectively

Cache data that provides value when offline. Avoid caching user-specific or sensitive data.

❌ Mistake 3: Not handling cache read errors

If cached data is corrupted or malformed, the app should fall back gracefully.

✅ Correct: Handle cache errors

Use try-catch when reading from cache and fall back to network requests if it fails.

🎯 Key Takeaway

Caching is essential for performance , offline support , and reduced network usage . Use cache_manager for comprehensive caching solutions or SharedPreferences for simple key-value storage. Always set expiration times and handle cache read errors gracefully.