http_cached_client 1.0.3 copy "http_cached_client: ^1.0.3" to clipboard
http_cached_client: ^1.0.3 copied to clipboard

A lightweight Flutter package that wraps the standard `http` client with a caching mechanism

Simple HTTP Cache Client #

A lightweight Flutter package that wraps the standard http client with a caching mechanism. Improve your app's performance and reduce network overhead by caching API responses.

Features #

  • Simple, easy-to-use API similar to the standard http package
  • In-memory caching of GET and POST requests
  • Configurable cache timeout duration
  • Support for custom cache key generation
  • Methods to manually clear or invalidate specific cache entries
  • Support for common HTTP methods: GET, POST, PUT, DELETE
  • Seamless handling of redirects

Installation #

Add this to your pubspec.yaml file:

dependencies:
  http_cached_client: ^0.0.1

Then run:

flutter pub get

Usage #

Basic Setup #

import 'package:http_cached_client/http_cache_client.dart';

// Create a client instance
final client = HttpCacheClient(
  baseUrl: 'https://api.example.com',
  cacheTimeout: Duration(minutes: 10), // Default is 5 minutes
);

Making Requests #

// GET request (cached by default)
final response = await client.get('/users');

// GET request with query parameters
final userResponse = await client.get(
  '/users',
  queryParams: {'id': '123'},
);

// POST request with body
final createResponse = await client.post(
  '/users',
  body: {'name': 'John Doe', 'email': 'john@example.com'},
);

// Disable caching for a specific request
final uncachedResponse = await client.get(
  '/users/latest',
  cacheResult: false,
);

// PUT request (not cached by default)
final updateResponse = await client.put(
  '/users/123',
  body: {'name': 'Updated Name'},
);

// DELETE request (not cached by default)
final deleteResponse = await client.delete('/users/123');

Managing Cache #

// Clear all cached responses
client.clearCache();

// Invalidate a specific cached response
client.invalidateCache(
  uri: '/users',
  method: REQUEST_METHODS.GET,
);

// Invalidate a POST request with specific body
client.invalidateCache(
  uri: '/users',
  method: REQUEST_METHODS.POST,
  body: {'name': 'John Doe'},
);

Customization #

Custom Cache Key Generation #

You can customize how cache keys are generated by implementing the KeyGenerator interface:

import 'package:http_cached_client/key_generator.dart/key_generator.dart';
import 'package:http_cached_client/utils.dart';

class MyCustomKeyGenerator implements KeyGenerator {
  @override
  String generateKey({
    required REQUEST_METHODS method,
    required Uri url,
    Object? body,
  }) {
    // Your custom key generation logic
    return '$method-${url.path}-${body.hashCode}';
  }
}

// Use your custom key generator
final client = HttpCacheClient(
  baseUrl: 'https://api.example.com',
  keyGenerator: MyCustomKeyGenerator(),
);

Using a Custom HTTP Client #

You can provide your own implementation of http.Client:

import 'package:http/http.dart' as http;

final customHttpClient = http.Client();
// Configure your custom client if needed

final client = HttpCacheClient(
  baseUrl: 'https://api.example.com',
  httpClient: customHttpClient,
);

How It Works #

This library maintains an in-memory cache of responses for GET and POST requests. When making a cacheable request:

  1. A cache key is generated from the request method, URL, and body (if applicable)
  2. If a valid cached response exists (not expired), it's returned immediately
  3. Otherwise, the actual HTTP request is made, and the response is stored in the cache
  4. Subsequent identical requests will use the cached response until it expires

The default cache timeout is 5 minutes, but you can customize this when creating the client.

Benefits #

  • Reduced network usage
  • Faster response times for repeated requests
  • Less strain on your backend servers
  • Better user experience with quicker UI updates

License #

This project is licensed under the MIT License - see the LICENSE file for details.

1
likes
160
points
142
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight Flutter package that wraps the standard `http` client with a caching mechanism

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, http

More

Packages that depend on http_cached_client