runable
This commit is contained in:
181
lib/core/config/image_cache_config.dart
Normal file
181
lib/core/config/image_cache_config.dart
Normal file
@@ -0,0 +1,181 @@
|
||||
/// Performance-optimized image cache configuration
|
||||
///
|
||||
/// This configuration provides:
|
||||
/// - Memory cache limits to prevent OOM errors
|
||||
/// - Disk cache management for offline support
|
||||
/// - Optimized image sizing for different use cases
|
||||
/// - Efficient cache eviction policies
|
||||
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
|
||||
/// Custom cache manager for product images with performance optimizations
|
||||
class ProductImageCacheManager extends CacheManager {
|
||||
static const key = 'product_image_cache';
|
||||
|
||||
static ProductImageCacheManager? _instance;
|
||||
|
||||
factory ProductImageCacheManager() {
|
||||
_instance ??= ProductImageCacheManager._();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
ProductImageCacheManager._() : super(
|
||||
Config(
|
||||
key,
|
||||
// Cache for 30 days
|
||||
stalePeriod: const Duration(days: 30),
|
||||
// Max 200 cached images
|
||||
maxNrOfCacheObjects: 200,
|
||||
// Clean cache when app starts
|
||||
repo: JsonCacheInfoRepository(databaseName: key),
|
||||
fileService: HttpFileService(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Custom cache manager for category images (smaller cache)
|
||||
class CategoryImageCacheManager extends CacheManager {
|
||||
static const key = 'category_image_cache';
|
||||
|
||||
static CategoryImageCacheManager? _instance;
|
||||
|
||||
factory CategoryImageCacheManager() {
|
||||
_instance ??= CategoryImageCacheManager._();
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
CategoryImageCacheManager._() : super(
|
||||
Config(
|
||||
key,
|
||||
// Cache for 60 days (categories change less frequently)
|
||||
stalePeriod: const Duration(days: 60),
|
||||
// Max 50 cached category images
|
||||
maxNrOfCacheObjects: 50,
|
||||
repo: JsonCacheInfoRepository(databaseName: key),
|
||||
fileService: HttpFileService(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Image size configurations for different use cases
|
||||
class ImageSizeConfig {
|
||||
// Grid thumbnail sizes (small for memory efficiency)
|
||||
static const int gridThumbnailWidth = 300;
|
||||
static const int gridThumbnailHeight = 300;
|
||||
|
||||
// List item sizes (medium)
|
||||
static const int listItemWidth = 400;
|
||||
static const int listItemHeight = 400;
|
||||
|
||||
// Detail view sizes (larger but still optimized)
|
||||
static const int detailWidth = 800;
|
||||
static const int detailHeight = 800;
|
||||
|
||||
// Cart item thumbnail (very small)
|
||||
static const int cartThumbnailWidth = 200;
|
||||
static const int cartThumbnailHeight = 200;
|
||||
|
||||
// Category card sizes
|
||||
static const int categoryCardWidth = 250;
|
||||
static const int categoryCardHeight = 250;
|
||||
}
|
||||
|
||||
/// Memory cache configuration
|
||||
class MemoryCacheConfig {
|
||||
// Maximum memory cache size (in MB)
|
||||
static const int maxMemoryCacheMB = 50;
|
||||
|
||||
// Maximum number of cached images in memory
|
||||
static const int maxMemoryCacheCount = 100;
|
||||
|
||||
// Memory cache for grid items (smaller)
|
||||
static const int gridMemoryCacheMB = 30;
|
||||
|
||||
// Preload cache size
|
||||
static const int preloadCacheCount = 20;
|
||||
}
|
||||
|
||||
/// Disk cache configuration
|
||||
class DiskCacheConfig {
|
||||
// Maximum disk cache size (in MB)
|
||||
static const int maxDiskCacheMB = 200;
|
||||
|
||||
// Cache expiration durations
|
||||
static const Duration productImageCacheDuration = Duration(days: 30);
|
||||
static const Duration categoryImageCacheDuration = Duration(days: 60);
|
||||
|
||||
// Cache cleanup threshold (cleanup when this % is reached)
|
||||
static const double cleanupThreshold = 0.9; // 90%
|
||||
}
|
||||
|
||||
/// Image loading optimization helpers
|
||||
class ImageOptimization {
|
||||
/// Get optimal image dimensions based on screen size
|
||||
static ImageDimensions getOptimalDimensions({
|
||||
required double screenWidth,
|
||||
required ImageContext context,
|
||||
}) {
|
||||
switch (context) {
|
||||
case ImageContext.gridThumbnail:
|
||||
return ImageDimensions(
|
||||
width: ImageSizeConfig.gridThumbnailWidth,
|
||||
height: ImageSizeConfig.gridThumbnailHeight,
|
||||
);
|
||||
case ImageContext.listItem:
|
||||
return ImageDimensions(
|
||||
width: ImageSizeConfig.listItemWidth,
|
||||
height: ImageSizeConfig.listItemHeight,
|
||||
);
|
||||
case ImageContext.detail:
|
||||
return ImageDimensions(
|
||||
width: ImageSizeConfig.detailWidth,
|
||||
height: ImageSizeConfig.detailHeight,
|
||||
);
|
||||
case ImageContext.cartThumbnail:
|
||||
return ImageDimensions(
|
||||
width: ImageSizeConfig.cartThumbnailWidth,
|
||||
height: ImageSizeConfig.cartThumbnailHeight,
|
||||
);
|
||||
case ImageContext.categoryCard:
|
||||
return ImageDimensions(
|
||||
width: ImageSizeConfig.categoryCardWidth,
|
||||
height: ImageSizeConfig.categoryCardHeight,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all image caches
|
||||
static Future<void> clearAllCaches() async {
|
||||
await ProductImageCacheManager().emptyCache();
|
||||
await CategoryImageCacheManager().emptyCache();
|
||||
}
|
||||
|
||||
/// Clear expired cache entries
|
||||
static Future<void> clearExpiredCache() async {
|
||||
// Cache managers automatically handle this
|
||||
}
|
||||
|
||||
/// Get total cache size
|
||||
static Future<int> getTotalCacheSize() async {
|
||||
// This would require implementing cache size calculation
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
enum ImageContext {
|
||||
gridThumbnail,
|
||||
listItem,
|
||||
detail,
|
||||
cartThumbnail,
|
||||
categoryCard,
|
||||
}
|
||||
|
||||
class ImageDimensions {
|
||||
final int width;
|
||||
final int height;
|
||||
|
||||
const ImageDimensions({
|
||||
required this.width,
|
||||
required this.height,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user