清除 SDWebImage 缓存
我正在开发一个带有新闻源的 iPhone 应用程序。此新闻源是从我编写的 JSON Web 服务中提取的(目前位于我笔记本电脑上的 MAMP 上)。
不管怎样,我使用 MySQL DB 来存储对图像的引用,这些图像存储在 apache 文件系统中。
我以一种非常特殊的方式存储它们,这就是我存储它们的方式:
完整图像:ng_(postid)_(seqid)
点赞:tng_(postid)_(seqid)
PostID 是分配给每个新闻帖子的唯一 ID。
SeqID 是仅对该新闻帖子的照片唯一的 ID。
我可能没有说得很清楚......示例:
第一篇文章中的图像文件可能看起来像这样
ng_1_1.jpg
ng_1_2.png
ng_1_3.jpg
第二篇文章的图像文件可能看起来像这样
ng_2_1.jpg
ng_2_2.png
ng_2_3.gif
到目前为止这一切都很好,但我试图看看是什么如果我删除一篇帖子并在其位置重新创建一个帖子,会发生什么情况?
假设我们有一篇名为“旧帖子”的帖子,其中有 2 个图像,帖子 ID 为 7。
它的图像可能如下所示:
ng_7_1.jpg
ng_7_2.jpg
假设我们删除了该帖子,然后创建了一个新帖子,其中包含 3 个图像和称为“新帖子”。
它的图像将如下所示:
ng_7_1.jpg
ng_7_2.jpg
ng_7_3.jpg
现在,问题来了...如果设备查看了已删除的旧帖子,然后查看此新帖子,他们将看到前两张图像是旧帖子中的图像。不是新的。
为什么? SDWebImage 认为由于 URL 相同,因此决定从磁盘中提取缓存的图像。它甚至不显示缓存的版本,然后检查图像是否已更新。
因此,我已经找到了两种可能的解决方案:
- 在显示缓存版本后,以某种方式让 SDWebImage 检查在线图像
- 在我的 JSON 中传递一个密钥,告诉我的应用程序擦除 SDWebImage 的缓存(必要时)
所以,我的问题是,您将如何删除 SDWebImage 的缓存,或使其在显示缓存版本后检查服务器?
I'm working on an iPhone app which has a news feed. This news feed is pulled from a JSON web service I've written (currently living on MAMP on my laptop).
Anyway, I use a MySQL DB to store references to my images, which are stored in the apache filesystem.
I store them in a very particular way, and this is how I store them:
Full Images: ng_(postid)_(seqid)
Thumbs: tng_(postid)_(seqid)
PostID is the unique ID that is assigned to every news post.
SeqID is an ID that is only unique for the photos for that news post.
I probably didn't make that very clear... example:
The images files in the first post might look like this
ng_1_1.jpg
ng_1_2.png
ng_1_3.jpg
The image files for the second post might look like this
ng_2_1.jpg
ng_2_2.png
ng_2_3.gif
This has worked great up till now, but I tried to see what would happen if I deleted a post, and recreated one in it's place?
Let's say we have a post called 'Old Post', which has 2 images, with a postid of 7.
It's images might look like this:
ng_7_1.jpg
ng_7_2.jpg
Let's say we deleted that post, and then created a new one afterwards, which has three images and is called 'New Post'.
It's images will look like this:
ng_7_1.jpg
ng_7_2.jpg
ng_7_3.jpg
Now, here comes the problem... If the device has viewed the old post, which was deleted, and then views this new post, they will see the first two images as the ones from OLD POST. Not the new ones.
Why? SDWebImage thinks because the URL is identical, and therefore decides to pull the cached image from disk. It doesn't even display the cached version, and then check if the image has been updated.
So, I've worked out there are two possible solutions to this:
- Somehow get SDWebImage to check the online image, after displaying the cached version
- Pass down a key in my JSON, to tell my app to wipe SDWebImage's cache (when necessary)
So, my question is, how would you go about deleting SDWebImage's cache, or making it check the server after displaying the cached version?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的 PostID 值在您的系统中不是唯一的,这会给您带来问题。如果您有唯一的 PostID 值,则无法删除具有给定 ID 的帖子并将该 ID 值分配给新帖子...
在我看来,PostID 不应该可重用 - 你能想象一个店员删除系统中的特定订单,用旧 ID 创建一个新订单,有一天接到客户的电话,提供他的订单 ID,该订单 ID 现在已在系统中被覆盖?
另一件事是,您永远不应该删除客户端上的缓存图像 - 对用户友好,节省带宽和用户的数据计划(检查此 链接 为什么这很重要)。不过,您可以为 SDWebImage 指定
cacheMaxCacheAge
来删除旧的、未使用的图像。例如,当用户决定删除其设备上的特定帖子时,您还可以使用removeImageForKey:
删除特定图像。最后,您描述的情况更多地与更新特定帖子相关,因此帖子可以获取不同的图像集。在这种情况下,您可以做的最简单的事情就是使用唯一的图像 ID,因此当下载帖子时,将下载新图像(当缓存达到最大年龄时,旧图像将被删除 - 查找
cacheMaxCacheAge
)。或者,您可以在 DB/JSON 中引入一种同步机制(例如,基于时间戳:如果下载了帖子并且其时间戳比存储在应用程序缓存中的帖子更新,则您可以删除旧资源并下载新图像、文本、等等...如果时间戳相等,则说明您可以使用已下载的数据)。高级解决方案将使用 RestKit 和 Core Data,这将使用户能够离线浏览您的帖子并更新内容(图像、文本) ) 当您的 Web 资源 (JSON) 更改时。
多么好的一封信啊……我只是希望我的评论对你有用:)
I think your PostID values are not unique in your system and that causes you problems. If you had unique PostID values it would be impossible to delete a post with given ID and assign that ID value to a new post...
PostID shouldn't be reusable in my opinion - can you imagine a clerk deleting a specific order in his system, creating a new one with old ID and one day getting a call from customer that provides his order ID which is now overwritten in the system?
Other thing is that you should never ever delete cached images on client's side - be user-friendly, save bandwidth and users' data plans (check this link why that matters). However you can specify
cacheMaxCacheAge
for SDWebImage to get rid of old, unused images. You can also remove specific images usingremoveImageForKey:
when for example user decides to delete a specific post on his device.Finally, the case you're describing relates more to updating a specific post, so posts can get different image set for example. In that scenario the simplest thing you can do is to use unique images IDs, so when a post is downloaded, new images will be downloaded (old ones will be deleted when cache reaches its max age - look for
cacheMaxCacheAge
). Alternatively, you could introduce a kind of synchronization mechanism in your DB/JSON (e.g. based on timestamps: if a post is downloaded and has a newer timestamp than the post stored in application cache, you remove old resources and download new images, text, etc... If timestamps are equal you're good with data you already downloaded).An advanced solution would use RestKit and Core Data which would enable users to browse your posts offline and update content (images, text) when your web resource (JSON) changes.
What an epistle... I just hope my comments are useful for you :)