Skip to content

Codedix/cdx_image_cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CdxImageCache

A flexible, lightweight and customizable in-memory image caching service for Flutter. Optimized for decoding and caching ui.Image instances in memory. Ideal for prefetching, smooth scroll performance, and low-latency rendering.

CdxImageCache

Supports LRU (Least Recently Used) and no-eviction strategies.


Features

  • In-memory caching of both raw image bytes (Uint8List) and decoded ui.Image.
  • Pluggable caching strategies: simple map or LRU (with byte size limit).
  • Supports prefetching with deduplication and awaitable in-flight fetches.
  • Customizable fetch and decode logic (e.g., via HTTP or local assets).
  • Built-in default decoder using Flutter's image codec.
  • Manual or automatic cache eviction.
  • Stateless widget for displaying cached images (CachedImageMemory) with:
    • Fade-in animation.
    • Custom loading and error widgets.
    • Ready/error callbacks.
  • No disk caching: built for fast runtime memory access.

Getting started

1. Add the dependency

dependencies:
  cdx_image_cache: ^0.0.3

2. Usage

Initialize the singleton

final cache = MemoryImageCacheService();
cache.init(
  strategy: ImageCacheStrategy.lru(maxSizeBytes: 100 * 1024 * 1024), // 100MB
  fetchFunction: (url) async {
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) return response.bodyBytes;
    throw Exception('Failed to fetch image');
  },
);

You can use any HTTP client or custom logic in the fetcher.

Prefetch on model creation

class Article {
  final String imageUrl;

  Article.fromJson(Map<String, dynamic> json)
      : imageUrl = json['image'] {
    imageCacheService.prefetch(imageUrl);
  }
}

Wait for all prefetches to complete

await imageCacheService.waitForAllPrefetches()

Perfect for splash screens or onboarding screens that load bulk content.

Show the image with CachedImageMemory

CachedImageMemory(
    url: article.imageUrl,
    cacheService: imageCacheService,
    fadeDuration: Duration(milliseconds: 400),
    builder: (context, data) => Image.memory(data, fit: BoxFit.cover),
    loadingBuilder: (context) => const CircularProgressIndicator(),
    errorBuilder: (context, error) => const Icon(Icons.broken_image),
    onReady: (data) => print('Image ready!'),
)

This widget is stateless and uses TweenAnimationBuilder for fade-in.

Cache Strategies

abstract class ImageCacheStrategy {
  factory ImageCacheStrategy.simple();
  factory ImageCacheStrategy.lru({required int maxSizeBytes});
}
LRU

Removes least recently used items when maxEntries is exceeded.

NoEviction

Grows unbounded. Use with caution.


API

ImageMemoryCacheService
ImageMemoryCacheService({
  required CacheStrategy strategy,
  required Future<Uint8List> Function(String url) fetcher,
  Duration timeout = const Duration(seconds: 15),
});

Methods:

  • Uint8List? getIfAvailable(String url)
  • ui.Image? getDecodedIfAvailable(String url)
  • Future<ui.Image?> getDecodedImage(String url)
  • void prefetchImages(Iterable<String> urls)
  • Future<void> waitForAllFetches()
  • void clearCache({String? url})

License

MIT

Author

Developed by Codedix

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •