浏览代码

Fix cache-issues due to hash collisions

Vishnu Mohandas 5 年之前
父节点
当前提交
36467b6673
共有 5 个文件被更改,包括 13 次插入11 次删除
  1. 6 6
      lib/core/cache/image_cache.dart
  2. 4 2
      lib/models/file.dart
  3. 1 1
      lib/ui/gallery.dart
  4. 1 1
      lib/ui/video_widget.dart
  5. 1 1
      lib/ui/zoomable_image.dart

+ 6 - 6
lib/core/cache/image_cache.dart

@@ -5,25 +5,25 @@ import 'package:photos/core/cache/lru_map.dart';
 import 'package:photos/models/file.dart';
 
 class FileLruCache {
-  static LRUMap<int, dart.File> _map = LRUMap(25);
+  static LRUMap<String, dart.File> _map = LRUMap(25);
 
   static dart.File get(File file) {
-    return _map.get(file.hashCode);
+    return _map.get(file.tag());
   }
 
   static void put(File file, dart.File imageData) {
-    _map.put(file.hashCode, imageData);
+    _map.put(file.tag(), imageData);
   }
 }
 
 class BytesLruCache {
-  static LRUMap<int, Uint8List> _map = LRUMap(25);
+  static LRUMap<String, Uint8List> _map = LRUMap(25);
 
   static Uint8List get(File file) {
-    return _map.get(file.hashCode);
+    return _map.get(file.tag());
   }
 
   static void put(File file, Uint8List imageData) {
-    _map.put(file.hashCode, imageData);
+    _map.put(file.tag(), imageData);
   }
 }

+ 4 - 2
lib/models/file.dart

@@ -129,10 +129,12 @@ class File {
     return generatedId.hashCode ^ uploadedFileId.hashCode ^ localId.hashCode;
   }
 
-  String heroTag() {
+  String tag() {
     return "local_" +
         localId.toString() +
         ":remote_" +
-        uploadedFileId.toString();
+        uploadedFileId.toString() +
+        ":generated_" +
+        generatedId.toString();
   }
 }

+ 1 - 1
lib/ui/gallery.dart

@@ -176,7 +176,7 @@ class _GalleryState extends State<Gallery> {
               : null,
         ),
         child: Hero(
-          tag: file.heroTag(),
+          tag: file.tag(),
           child: ThumbnailWidget(file),
         ),
       ),

+ 1 - 1
lib/ui/video_widget.dart

@@ -58,7 +58,7 @@ class _VideoWidgetState extends State<VideoWidget> {
         ? _getVideoPlayer()
         : _getLoadingWidget();
     return Hero(
-      tag: widget.file.heroTag(),
+      tag: widget.file.tag(),
       child: content,
     );
   }

+ 1 - 1
lib/ui/zoomable_image.dart

@@ -59,7 +59,7 @@ class _ZoomableImageState extends State<ZoomableImage>
         minScale: PhotoViewComputedScale.contained,
         gaplessPlayback: true,
         heroAttributes: PhotoViewHeroAttributes(
-          tag: widget.photo.heroTag(),
+          tag: widget.photo.tag(),
         ),
       );
     } else {