@@ -2,7 +2,7 @@ import 'package:lru_cache/lru_cache.dart';
22
33/// Example of a custom cache implementation with size calculation
44class ImageCache extends LruCache <String , List <int >> {
5- ImageCache (int maxSizeInBytes) : super ( maxSizeInBytes);
5+ ImageCache (super . maxSizeInBytes);
66
77 @override
88 int sizeOf (String key, List <int > value) {
@@ -13,35 +13,35 @@ class ImageCache extends LruCache<String, List<int>> {
1313 @override
1414 void entryRemoved (bool evicted, String key, List <int > oldValue, List <int >? newValue) {
1515 if (evicted) {
16- print ('Image evicted from cache: $key (${oldValue .length } bytes)' );
16+ print ('Image evicted from cache: $key (${oldValue .length } bytes)' ); // ignore: avoid_print
1717 } else {
18- print ('Image replaced in cache: $key ' );
18+ print ('Image replaced in cache: $key ' ); // ignore: avoid_print
1919 }
2020 }
2121
2222 @override
2323 List <int >? create (String key) {
2424 // Simulate loading image from network
25- print ('Loading image from network: $key ' );
25+ print ('Loading image from network: $key ' ); // ignore: avoid_print
2626 return _loadImageFromNetwork (key);
2727 }
2828
2929 List <int > _loadImageFromNetwork (String key) {
3030 // Simulate network delay
31- Future .delayed (Duration (milliseconds: 100 ));
31+ Future .delayed (const Duration (milliseconds: 100 ));
3232 // Return dummy image data
3333 return List .generate (1000 , (index) => index % 256 );
3434 }
3535}
3636
3737/// Example of a cache with custom value creation
3838class UserProfileCache extends LruCache <int , Map <String , dynamic >> {
39- UserProfileCache (int maxSize) : super ( maxSize);
39+ UserProfileCache (super . maxSize);
4040
4141 @override
4242 Map <String , dynamic >? create (int userId) {
4343 // Simulate fetching user profile from database
44- print ('Fetching user profile for ID: $userId ' );
44+ print ('Fetching user profile for ID: $userId ' ); // ignore: avoid_print
4545 return {
4646 'id' : userId,
4747 'name' : 'User $userId ' ,
@@ -53,13 +53,13 @@ class UserProfileCache extends LruCache<int, Map<String, dynamic>> {
5353 @override
5454 void entryRemoved (bool evicted, int key, Map <String , dynamic > oldValue, Map <String , dynamic >? newValue) {
5555 if (evicted) {
56- print ('User profile evicted: ${oldValue ['name' ]}' );
56+ print ('User profile evicted: ${oldValue ['name' ]}' ); // ignore: avoid_print
5757 }
5858 }
5959}
6060
6161Future <void > main () async {
62- print ('=== Basic LRU Cache Example ===\n ' );
62+ print ('=== Basic LRU Cache Example ===\n ' ); // ignore: avoid_print
6363
6464 // Create a basic cache with max size of 3
6565 final basicCache = LruCache <String , String >(3 );
@@ -69,28 +69,28 @@ Future<void> main() async {
6969 await basicCache.put ('key2' , 'value2' );
7070 await basicCache.put ('key3' , 'value3' );
7171
72- print ('Cache after adding 3 items:' );
73- print ('Size: ${await basicCache .size ()}' );
74- print ('Keys: ${await basicCache .keys ()}' );
75- print ('Values: ${await basicCache .values ()}' );
72+ print ('Cache after adding 3 items:' ); // ignore: avoid_print
73+ print ('Size: ${await basicCache .size ()}' ); // ignore: avoid_print
74+ print ('Keys: ${await basicCache .keys ()}' ); // ignore: avoid_print
75+ print ('Values: ${await basicCache .values ()}' ); // ignore: avoid_print
7676
7777 // Access an item to make it most recently used
7878 await basicCache.get ('key1' );
7979
8080 // Add a fourth item - this will evict the least recently used item
8181 await basicCache.put ('key4' , 'value4' );
8282
83- print ('\n Cache after adding 4th item:' );
84- print ('Size: ${await basicCache .size ()}' );
85- print ('Keys: ${await basicCache .keys ()}' );
86- print ('Hit rate: ${basicCache .hitRate ().toStringAsFixed (1 )}%' );
83+ print ('\n Cache after adding 4th item:' ); // ignore: avoid_print
84+ print ('Size: ${await basicCache .size ()}' ); // ignore: avoid_print
85+ print ('Keys: ${await basicCache .keys ()}' ); // ignore: avoid_print
86+ print ('Hit rate: ${basicCache .hitRate ().toStringAsFixed (1 )}%' ); // ignore: avoid_print
8787
8888 // Check if items exist
89- print ('\n Checking if items exist:' );
90- print ('key1 exists: ${await basicCache .containsKey ('key1' )}' );
91- print ('key2 exists: ${await basicCache .containsKey ('key2' )}' ); // Should be false (evicted)
89+ print ('\n Checking if items exist:' ); // ignore: avoid_print
90+ print ('key1 exists: ${await basicCache .containsKey ('key1' )}' ); // ignore: avoid_print
91+ print ('key2 exists: ${await basicCache .containsKey ('key2' )}' ); // ignore: avoid_print // Should be false (evicted)
9292
93- print ('\n === Image Cache Example ===\n ' );
93+ print ('\n === Image Cache Example ===\n ' ); // ignore: avoid_print
9494
9595 // Create an image cache with max size of 5000 bytes
9696 final imageCache = ImageCache (5000 );
@@ -100,24 +100,24 @@ Future<void> main() async {
100100 final image2 = await imageCache.get ('image2.jpg' );
101101 final image3 = await imageCache.get ('image3.jpg' );
102102
103- print ('Loaded ${image1 ?.length ?? 0 } bytes for image1.jpg' );
104- print ('Loaded ${image2 ?.length ?? 0 } bytes for image2.jpg' );
105- print ('Loaded ${image3 ?.length ?? 0 } bytes for image3.jpg' );
103+ print ('Loaded ${image1 ?.length ?? 0 } bytes for image1.jpg' ); // ignore: avoid_print
104+ print ('Loaded ${image2 ?.length ?? 0 } bytes for image2.jpg' ); // ignore: avoid_print
105+ print ('Loaded ${image3 ?.length ?? 0 } bytes for image3.jpg' ); // ignore: avoid_print
106106
107107 // Access image1 again to make it most recently used
108108 await imageCache.get ('image1.jpg' );
109109
110110 // Add a large image that will cause eviction
111111 await imageCache.put ('large_image.jpg' , List .generate (3000 , (i) => i % 256 ));
112112
113- print ('\n Cache statistics:' );
114- print ('Size: ${await imageCache .size ()} bytes' );
115- print ('Max size: ${imageCache .maxSize ()} bytes' );
116- print ('Hit count: ${imageCache .hitCount ()}' );
117- print ('Miss count: ${imageCache .missCount ()}' );
118- print ('Eviction count: ${imageCache .evictionCount ()}' );
113+ print ('\n Cache statistics:' ); // ignore: avoid_print
114+ print ('Size: ${await imageCache .size ()} bytes' ); // ignore: avoid_print
115+ print ('Max size: ${imageCache .maxSize ()} bytes' ); // ignore: avoid_print
116+ print ('Hit count: ${imageCache .hitCount ()}' ); // ignore: avoid_print
117+ print ('Miss count: ${imageCache .missCount ()}' ); // ignore: avoid_print
118+ print ('Eviction count: ${imageCache .evictionCount ()}' ); // ignore: avoid_print
119119
120- print ('\n === User Profile Cache Example ===\n ' );
120+ print ('\n === User Profile Cache Example ===\n ' ); // ignore: avoid_print
121121
122122 // Create a user profile cache
123123 final userCache = UserProfileCache (5 );
@@ -127,55 +127,55 @@ Future<void> main() async {
127127 final user2 = await userCache.get (2 );
128128 final user3 = await userCache.get (3 );
129129
130- print ('User 1: ${user1 ?['name' ]}' );
131- print ('User 2: ${user2 ?['name' ]}' );
132- print ('User 3: ${user3 ?['name' ]}' );
130+ print ('User 1: ${user1 ?['name' ]}' ); // ignore: avoid_print
131+ print ('User 2: ${user2 ?['name' ]}' ); // ignore: avoid_print
132+ print ('User 3: ${user3 ?['name' ]}' ); // ignore: avoid_print
133133
134134 // Access user1 again (cache hit)
135135 final user1Again = await userCache.get (1 );
136- print ('User 1 again: ${user1Again ?['name' ]}' );
136+ print ('User 1 again: ${user1Again ?['name' ]}' ); // ignore: avoid_print
137137
138138 // Add more users to trigger eviction
139139 for (int i = 4 ; i <= 8 ; i++ ) {
140140 await userCache.get (i);
141141 }
142142
143- print ('\n User cache statistics:' );
144- print ('Size: ${await userCache .size ()}' );
145- print ('Hit rate: ${userCache .hitRate ().toStringAsFixed (1 )}%' );
146- print ('Create count: ${userCache .createCount ()}' );
143+ print ('\n User cache statistics:' ); // ignore: avoid_print
144+ print ('Size: ${await userCache .size ()}' ); // ignore: avoid_print
145+ print ('Hit rate: ${userCache .hitRate ().toStringAsFixed (1 )}%' ); // ignore: avoid_print
146+ print ('Create count: ${userCache .createCount ()}' ); // ignore: avoid_print
147147
148- print ('\n === Cache Resize Example ===\n ' );
148+ print ('\n === Cache Resize Example ===\n ' ); // ignore: avoid_print
149149
150150 // Create a cache and demonstrate resizing
151151 final resizeCache = LruCache <String , String >(2 );
152152 await resizeCache.put ('item1' , 'value1' );
153153 await resizeCache.put ('item2' , 'value2' );
154154
155- print ('Before resize:' );
156- print ('Max size: ${resizeCache .maxSize ()}' );
157- print ('Current size: ${await resizeCache .size ()}' );
158- print ('Items: ${await resizeCache .keys ()}' );
155+ print ('Before resize:' ); // ignore: avoid_print
156+ print ('Max size: ${resizeCache .maxSize ()}' ); // ignore: avoid_print
157+ print ('Current size: ${await resizeCache .size ()}' ); // ignore: avoid_print
158+ print ('Items: ${await resizeCache .keys ()}' ); // ignore: avoid_print
159159
160160 // Resize to larger size
161161 await resizeCache.resize (5 );
162162 await resizeCache.put ('item3' , 'value3' );
163163 await resizeCache.put ('item4' , 'value4' );
164164
165- print ('\n After resize to larger size:' );
166- print ('Max size: ${resizeCache .maxSize ()}' );
167- print ('Current size: ${await resizeCache .size ()}' );
168- print ('Items: ${await resizeCache .keys ()}' );
165+ print ('\n After resize to larger size:' ); // ignore: avoid_print
166+ print ('Max size: ${resizeCache .maxSize ()}' ); // ignore: avoid_print
167+ print ('Current size: ${await resizeCache .size ()}' ); // ignore: avoid_print
168+ print ('Items: ${await resizeCache .keys ()}' ); // ignore: avoid_print
169169
170170 // Resize to smaller size (will cause eviction)
171171 await resizeCache.resize (1 );
172172
173- print ('\n After resize to smaller size:' );
174- print ('Max size: ${resizeCache .maxSize ()}' );
175- print ('Current size: ${await resizeCache .size ()}' );
176- print ('Items: ${await resizeCache .keys ()}' );
173+ print ('\n After resize to smaller size:' ); // ignore: avoid_print
174+ print ('Max size: ${resizeCache .maxSize ()}' ); // ignore: avoid_print
175+ print ('Current size: ${await resizeCache .size ()}' ); // ignore: avoid_print
176+ print ('Items: ${await resizeCache .keys ()}' ); // ignore: avoid_print
177177
178- print ('\n === Performance Example ===\n ' );
178+ print ('\n === Performance Example ===\n ' ); // ignore: avoid_print
179179
180180 // Demonstrate cache performance
181181 final perfCache = LruCache <int , String >(100 );
@@ -189,18 +189,18 @@ Future<void> main() async {
189189
190190 stopwatch.stop ();
191191
192- print ('Performance test completed in ${stopwatch .elapsedMilliseconds }ms' );
193- print ('Operations: ${perfCache .putCount () + perfCache .hitCount () + perfCache .missCount ()}' );
194- print ('Hit rate: ${perfCache .hitRate ().toStringAsFixed (1 )}%' );
192+ print ('Performance test completed in ${stopwatch .elapsedMilliseconds }ms' ); // ignore: avoid_print
193+ print ('Operations: ${perfCache .putCount () + perfCache .hitCount () + perfCache .missCount ()}' ); // ignore: avoid_print
194+ print ('Hit rate: ${perfCache .hitRate ().toStringAsFixed (1 )}%' ); // ignore: avoid_print
195195
196- print ('\n === Cache Statistics ===\n ' );
196+ print ('\n === Cache Statistics ===\n ' ); // ignore: avoid_print
197197
198198 // Clear statistics and show final state
199199 perfCache.clearStats ();
200- print ('After clearing statistics:' );
201- print ('Hit count: ${perfCache .hitCount ()}' );
202- print ('Miss count: ${perfCache .missCount ()}' );
203- print ('Put count: ${perfCache .putCount ()}' );
204- print ('Create count: ${perfCache .createCount ()}' );
205- print ('Eviction count: ${perfCache .evictionCount ()}' );
206- }
200+ print ('After clearing statistics:' ); // ignore: avoid_print
201+ print ('Hit count: ${perfCache .hitCount ()}' ); // ignore: avoid_print
202+ print ('Miss count: ${perfCache .missCount ()}' ); // ignore: avoid_print
203+ print ('Put count: ${perfCache .putCount ()}' ); // ignore: avoid_print
204+ print ('Create count: ${perfCache .createCount ()}' ); // ignore: avoid_print
205+ print ('Eviction count: ${perfCache .evictionCount ()}' ); // ignore: avoid_print
206+ }
0 commit comments