带有元数据的 Glide 中的图像缓存签名
Image Cache Signature in Glide with Metadata
用户可以更改图片(替换它)。一旦用户更改了他们的图像,我希望新图像缓存在 Glide 中,而旧图像从缓存中删除。
我已经在线阅读了所有内容,但我仍然不知道如何对此实施良好的解决方案。
我试过在设置图像时跳过本地内存和磁盘缓存:
GlideApp.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(view);
这个解决方案很慢,因为它现在每次都调用新图像 - 它从不缓存新图像。
Glide 文档说:
the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc) when possible. - https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
但这对我来说是不可能的,所以 Glide 文档然后说:
Since it’s often difficult or impossible to change identifiers, Glide
also offers the signature() API to mix in additional data that you
control into your cache key.
它给出了这个例子:
Glide.with(yourFragment)
.load(yourFileDataModel)
.signature(new ObjectKey(yourVersionMetadata))
.into(yourImageView);
但是问题来了。什么是好的"yourVersionMetadata"?如何创建和维护它?我见过这样的例子:
.signature(new ObjectKey(Long.toString(System.currentTimeMillis())))
这会导致每次加载图像时磁盘缓存键都发生变化,所以速度很慢。我只需要在用户替换图像时更改它。并非每次加载图像时。
有人写道:
You can do something like generate a new UUID or increment an integer whenever the image changes. If you go that route, you'll have to keep track of the current signature for each image somewhere. - https://github.com/bumptech/glide/issues/2841
我不知道该怎么做。
我还尝试了完全删除缓存的异步任务。它有效,但同样非常慢(Glide 不推荐使用这种方法)。
我不知道如何只插入当前签名(应该更快)而不是每次加载图像时都创建一个新签名。帮助?看来替换图片和重新缓存应该不难!
两个选项
1) 要为文件生成唯一签名,您可以为其计算MD5 签名。在文件被修改之前,它始终是唯一的。查看如何生成 MD5 签名 here.
2) 设置唯一签名的另一种方法是使用文件的最后修改时间。如果您确定只有您的应用程序会修改图像而没有其他任何东西,那么您也可以依赖它。要获取最后修改时间,请使用:
File file = new File("path/to/image");
String signature = Long.toString(file.lastModified());
我为此工作了好几天。
我知道您可能以前读过这篇文章并忽略了它,因为您认为更改代码可能需要大量工作。但说真的,这是非常值得的。据我所知,其性能胜过所有其他提出的方法,这是 Glide 推荐的解决方案,并且您不需要跳过缓存或创建签名,因此它也可以使您的代码更简洁。
来自滑翔:
In practice, the best way to invalidate a cache file is to change your
identifier when the content changes (url, uri, file path etc) when
possible. - https://bumptech.github.io/glide/doc/caching.html
解决方案:
当用户上传新图像时更改图像的名称。获取文件名并使用它作为示例。一旦图像 URL 发生变化,Glide 就会知道您已经更改了图像并会相应地更新缓存。 这是迄今为止我的最佳表现。
使用时:
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
它从不缓存图像,这确实使图像加载缓慢。你会认为签名在性能方面更好,我也成功地实现了它们。但对我来说,它们似乎和完全跳过缓存一样慢。
用户可以更改图片(替换它)。一旦用户更改了他们的图像,我希望新图像缓存在 Glide 中,而旧图像从缓存中删除。
我已经在线阅读了所有内容,但我仍然不知道如何对此实施良好的解决方案。
我试过在设置图像时跳过本地内存和磁盘缓存:
GlideApp.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(view);
这个解决方案很慢,因为它现在每次都调用新图像 - 它从不缓存新图像。
Glide 文档说:
the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc) when possible. - https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
但这对我来说是不可能的,所以 Glide 文档然后说:
Since it’s often difficult or impossible to change identifiers, Glide also offers the signature() API to mix in additional data that you control into your cache key.
它给出了这个例子:
Glide.with(yourFragment)
.load(yourFileDataModel)
.signature(new ObjectKey(yourVersionMetadata))
.into(yourImageView);
但是问题来了。什么是好的"yourVersionMetadata"?如何创建和维护它?我见过这样的例子:
.signature(new ObjectKey(Long.toString(System.currentTimeMillis())))
这会导致每次加载图像时磁盘缓存键都发生变化,所以速度很慢。我只需要在用户替换图像时更改它。并非每次加载图像时。
有人写道:
You can do something like generate a new UUID or increment an integer whenever the image changes. If you go that route, you'll have to keep track of the current signature for each image somewhere. - https://github.com/bumptech/glide/issues/2841
我不知道该怎么做。
我还尝试了完全删除缓存的异步任务。它有效,但同样非常慢(Glide 不推荐使用这种方法)。
我不知道如何只插入当前签名(应该更快)而不是每次加载图像时都创建一个新签名。帮助?看来替换图片和重新缓存应该不难!
两个选项
1) 要为文件生成唯一签名,您可以为其计算MD5 签名。在文件被修改之前,它始终是唯一的。查看如何生成 MD5 签名 here.
2) 设置唯一签名的另一种方法是使用文件的最后修改时间。如果您确定只有您的应用程序会修改图像而没有其他任何东西,那么您也可以依赖它。要获取最后修改时间,请使用:
File file = new File("path/to/image");
String signature = Long.toString(file.lastModified());
我为此工作了好几天。
我知道您可能以前读过这篇文章并忽略了它,因为您认为更改代码可能需要大量工作。但说真的,这是非常值得的。据我所知,其性能胜过所有其他提出的方法,这是 Glide 推荐的解决方案,并且您不需要跳过缓存或创建签名,因此它也可以使您的代码更简洁。
来自滑翔:
In practice, the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc) when possible. - https://bumptech.github.io/glide/doc/caching.html
解决方案: 当用户上传新图像时更改图像的名称。获取文件名并使用它作为示例。一旦图像 URL 发生变化,Glide 就会知道您已经更改了图像并会相应地更新缓存。 这是迄今为止我的最佳表现。
使用时:
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
它从不缓存图像,这确实使图像加载缓慢。你会认为签名在性能方面更好,我也成功地实现了它们。但对我来说,它们似乎和完全跳过缓存一样慢。