dio_smart_cache 1.0.0 copy "dio_smart_cache: ^1.0.0" to clipboard
dio_smart_cache: ^1.0.0 copied to clipboard

A smart caching interceptor for Dio with configurable endpoint-based cache rules and duration types.

example/example.dart

import 'package:dio/dio.dart';
import 'package:dio_smart_cache/dio_smart_cache.dart';

void main() async {
  // Create Dio instance
  final dio = Dio();
  dio.options.baseUrl = 'https://api.example.com';

  // Configure smart cache with endpoint rules
  final smartCache = SmartCacheInterceptor(
    options: const SmartCacheOptions(
      // Optional: customize default durations
      cacheDurations: {
        CacheDurationType.veryLong: Duration(days: 2),
        CacheDurationType.long: Duration(hours: 24)
      },

      // Define cache rules per endpoint
      cacheRules: [
        // Static data - cache for very long
        CacheRule(
            pattern: '/locations', durationType: CacheDurationType.veryLong),
        CacheRule(
            pattern: '/countries', durationType: CacheDurationType.veryLong),

        // Semi-static data - cache for long
        CacheRule(
            pattern: '/trip_availabilities',
            durationType: CacheDurationType.long),
        CacheRule(
            pattern: '/payment_methods', durationType: CacheDurationType.long),

        // Dynamic content - cache for medium
        CacheRule(
            pattern: '/slide_show_ads', durationType: CacheDurationType.medium),

        // Frequently changing - cache for short
        CacheRule(
            pattern: '/promotions', durationType: CacheDurationType.short),

        // Custom duration example
        CacheRule.custom(
            pattern: '/special_offers', duration: Duration(minutes: 45)),

        // Regex pattern matching
        CacheRule(
            pattern: r'/users/\d+/profile',
            durationType: CacheDurationType.medium,
            regexMatch: true),
      ],
    ),
  );

  // Add the interceptor to Dio
  dio.addSmartCache(smartCache);

  // Example 1: Normal request (uses cache if available)
  try {
    final response = await dio.get('/locations',
        options: smartCache.cacheOptionsFor('/locations'));
    print('Locations: ${response.data}');
  } catch (e) {
    print('Error: $e');
  }

  // Example 2: Force refresh (bypass cache)
  try {
    final freshResponse = await dio.get(
      '/locations',
      options: smartCache.cacheOptionsFor('/locations', forceRefresh: true),
    );
    print('Fresh locations: ${freshResponse.data}');
  } catch (e) {
    print('Error: $e');
  }

  // Example 3: Request to non-cached endpoint (no caching)
  try {
    final uncachedResponse = await dio.get(
      '/some-other-endpoint',
      options: smartCache.cacheOptionsFor('/some-other-endpoint'),
    );
    print('Response: ${uncachedResponse.data}');
  } catch (e) {
    print('Error: $e');
  }

  // Cache management
  // Clear all cache
  await smartCache.clearCache();

  // Delete specific cache entry
  await smartCache.deleteCache('https://api.example.com/locations');
}
2
likes
150
points
105
downloads

Publisher

unverified uploader

Weekly Downloads

A smart caching interceptor for Dio with configurable endpoint-based cache rules and duration types.

Homepage

Topics

#dio #cache #networking #http

Documentation

API reference

License

MIT (license)

Dependencies

dio, dio_cache_interceptor, shared_preferences

More

Packages that depend on dio_smart_cache