Glide:如何查找图像是否已经缓存并使用缓存的版本? [英] Glide : How to find if the image is already cached and use the cached version?

查看:1090
本文介绍了Glide:如何查找图像是否已经缓存并使用缓存的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我有一个很大的GIF图片,我想在用户首次使用 Glide 打开应用程序时对其进行缓存-图像加载和缓存库.之后,无论何时用户打开应用程序,我都想显示缓存的版本(如果存在).此GIF URL将在给定间隔后过期.过期后,我将获取新的GIF URL,并显示/缓存该URL以供将来使用.

I have a large GIF image which I want to cache the first time user opens the app using Glide - Image Loading and Caching library. After that whenever user opens the app, I want to show the cached version if present. This GIF URL will expire after a given interval. When it expires, I fetch the new GIF URL and display/cache that for future use.

我尝试过的事情:

我在Glide的github上经历了缓存和缓存无效页.我还通过了Google网上论坛线程确保加载的图像仅来自磁盘缓存,它显示了如何获取图像表单缓存.我还经历了如何使某些特定图像的Glide缓存无效问题.

I went through Caching and Cache Invalidation on Glide's github page. I also went though the Google Group thread Ensuring That Images Loaded Only Come From Disk Cache, which shows how to get the image form cache. I also went through How to invalidate Glide cache for some specific images question.

从上面的链接中,我看到以下代码片段,其中显示了如何从缓存中加载图像.但是,这仅尝试从缓存中获取图像.如果它不在缓存中,则它不会尝试从网络获取并失败:

From the links above I see the following code sniplet which shows how to load the image from cache. However this only tries to get the image from cache. If its not present in cache, it doesn't try to get from the network and fails:

Glide.with(TheActivity.this)
        .using(new StreamModelLoader<String>() {
            @Override
            public DataFetcher<InputStream> getResourceFetcher(final String model, int i, int i1) {
                return new DataFetcher<InputStream>() {
                    @Override
                    public InputStream loadData(Priority priority) throws Exception {
                        throw new IOException();
                    }

                    @Override
                    public void cleanup() {
                    }

                    @Override
                    public String getId() {
                        return model;
                    }

                    @Override
                    public void cancel() {
                    }
                };
            }
        })
       .load("http://sampleurl.com/sample.gif")
       .diskCacheStrategy(DiskCacheStrategy.SOURCE)
       .into(theImageView);

问题:

  1. 是否有一种更清洁的方法来实现以下目的:从缓存中显示GIF图像(如果存在),否则下载GIF,将其缓存以备后用,并在ImageView中显示.

上面的缓存文章提到了以下内容:

The caching article above mentions the following:

实际上,使缓存文件无效的最佳方法是更改 内容更改时的标识符(URL,URI,文件路径等)

In practice, the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc)

上一个过期时,服务器会向该应用程序发送一个不同的URL.在这种情况下,我相信旧图像最终将被垃圾收集吗?有没有办法强制从缓存中删除图像?

The server sends a different URL to the app when the previous one expires. In this case, I believe the old image will eventually be Garbage Collected? Is there a way to force remove the image from the cache?

类似地,有一种方法可以防止使用特定密钥对图像进行垃圾收集(以防止再次下载大文件),然后在URL更改时指示从缓存中删除旧图像. ?

On a similar note, is there a way to prevent the Garbage Collection of an image with specific key (to prevent downloading the large file again) and then later instruct to delete the old image from cache when the URL changes?

推荐答案

  1. 您不需要自定义ModelLoader即可从缓存中显示GIF(如果存在),否则无需获取它,这实际上是Glide的默认行为.仅使用标准负载线即可正常工作:

  1. You don't need a custom ModelLoader to show the GIF from cache if present and fetch it otherwise, that's actually Glide's default behavior. Just using a standard load line should work fine:

Glide.with(TheActivity.this)
   .load("http://sampleurl.com/sample.gif")
   .diskCacheStrategy(DiskCacheStrategy.SOURCE)
   .into(theImageView);

您的代码将阻止Glide下载GIF,并且仅在GIF已被缓存时才显示GIF,这听起来像您不想要的.

Your code will prevent Glide from downloading the GIF and will only show the GIF if it is already cached, which it sounds like you don't want.

  1. 是的,最终将删除旧图像.默认情况下,Glide使用LRU缓存,因此当缓存已满时,将删除最近最少使用的映像.您可以根据需要轻松自定义缓存的大小,以帮助实现这一目标.有关如何更改缓存大小,请参见配置 Wiki页面.

不幸的是,没有任何方法可以直接影响缓存的内容.您既不能明确地删除一项,也不能强行保留一项.在实践中,使用适当的磁盘缓存大小,通常无需担心二者之一.如果您经常显示图像,则不会将其逐出.如果您尝试缓存其他项目并且缓存中的空间不足,则较旧的项目将被自动逐出以腾出空间.

Unfortunately there isn't any way to influence the contents of the cache directly. You cannot either remove an item explicitly, or force one to be kept. In practice with an appropriate disk cache size you usually don't need to worry about doing either. If you display your image often enough, it won't be evicted. If you try to cache additional items and run out of space in the cache, older items will be evicted automatically to make space.

这篇关于Glide:如何查找图像是否已经缓存并使用缓存的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆