From 74b06196e4625f7d20594057132358ae50ae484a Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Thu, 26 Mar 2026 10:47:16 +0100 Subject: [PATCH] refactor(cache): move CacheEntry class outside CacheService and enhance its functionality --- lib/core/services/cache_service.dart | 75 ++++++++++++++-------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/lib/core/services/cache_service.dart b/lib/core/services/cache_service.dart index e8f3854..b5db591 100644 --- a/lib/core/services/cache_service.dart +++ b/lib/core/services/cache_service.dart @@ -1,5 +1,30 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; +/// Cache entry with metadata +class CacheEntry { + final T data; + final DateTime timestamp; + final int sizeEstimate; + int accessCount; + DateTime lastAccess; + + CacheEntry({ + required this.data, + required this.sizeEstimate, + }) : timestamp = DateTime.now(), + lastAccess = DateTime.now(), + accessCount = 0; + + bool isExpired({Duration ttl = const Duration(hours: 24)}) { + return DateTime.now().difference(timestamp) > ttl; + } + + void markAccess() { + accessCount++; + lastAccess = DateTime.now(); + } +} + /// Optimized cache service for memory management class CacheService { static const _maxMemoryCacheSizeMb = 200; @@ -8,31 +33,6 @@ class CacheService { final Map _cache = {}; int _totalSizeBytes = 0; - /// Cache entry with metadata - class CacheEntry { - final T data; - final DateTime timestamp; - final int sizeEstimate; - int accessCount; - DateTime lastAccess; - - CacheEntry({ - required this.data, - required this.sizeEstimate, - }) : timestamp = DateTime.now(), - lastAccess = DateTime.now(), - accessCount = 0; - - bool isExpired({Duration ttl = const Duration(hours: 24)}) { - return DateTime.now().difference(timestamp) > ttl; - } - - void markAccess() { - accessCount++; - lastAccess = DateTime.now(); - } - } - /// Cache data with automatic size management void cache(String key, T value, {int estimatedSize = 1000}) { // Remove old entry if exists @@ -111,6 +111,19 @@ class CacheService { } } +/// Cached image metadata +class CachedImage { + final String url; + final int sizeBytes; + final DateTime cachedAt; + int accessCount = 0; + + CachedImage({ + required this.url, + required this.sizeBytes, + }) : cachedAt = DateTime.now(); +} + /// Image cache with size limiting class ImageCacheManager { static const _maxImageMemoryMb = 100; @@ -119,18 +132,6 @@ class ImageCacheManager { final Map _imageCache = {}; int _totalMemoryBytes = 0; - class CachedImage { - final String url; - final int sizeBytes; - final DateTime cachedAt; - int accessCount = 0; - - CachedImage({ - required this.url, - required this.sizeBytes, - }) : cachedAt = DateTime.now(); - } - /// Add image to cache void cacheImage(String url, int sizeEstimateBytes) { if (_imageCache.length >= _maxImages) {