Android的凌空图像缓存问题 [英] Android volley image caching questions

查看:160
本文介绍了Android的凌空图像缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌自己的凌空抽射图片缓存教程

in the google's own volley image cache tutorial

// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
    final DisplayMetrics displayMetrics = ctx.getResources().
            getDisplayMetrics();
    final int screenWidth = displayMetrics.widthPixels;
    final int screenHeight = displayMetrics.heightPixels;
    // 4 bytes per pixel
    final int screenBytes = screenWidth * screenHeight * 4;

    return screenBytes * 3;
}

推荐的缓存是三个屏幕的价值相等于7MB图像。我有一个社交媒体应用程序,里面有它的新闻源。

the recommended cache is three screens worth of images which equals to 7mb. I have an social media app and there is a newsfeed inside it.

1)我的第一个问题是后高速缓存已满会发生什么?

1-) My first question is what will happen after the cache is full?

2 - )我想删除缓存每隔一小时,因此缓存将包含新的内容。那是合理的?是什么应用背后的图像缓存逻辑,其包括像新闻馈送(例如,的Instagram)?

2-) I am thinking about removing cache every one hour and thus the cache will include the newer content. Is that reasonable ? What is the image caching logic behind the apps which includes something like newsfeed(for example, instagram)?

3)我怎样才能删除特定项目的旧缓存,并迫使它重新下载?我想这个解决方案,但它没有工作:

3-) How can i remove the old cache of specific item and force it to download it again? I tried this solution but it did not work:

VolleySingleton.getInstance().getRequestQueue().getCache().remove(IMAGE_URL);

mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
mImageLoader = VolleySingleton.getInstance().getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);

有一个很多我的第三个问题的克隆问题,但没有人已经回答了。

There are a lots of clone question of my third question but none of them has been answered.

感谢您的帮助。 :)

推荐答案

1)缓存有2层的排球,一个是内存中缓存(在RAM),另一种是磁盘缓存。一旦缓存已满,当一个新的图像将要被缓存以腾出空间给新的项的最古老的图像(意味着还没有被访问时间最长的图象)的该高速缓存将被逐出。当事情从内存中缓存逐出,它仍然是present在磁盘缓存,可如果再次需要非常迅速地从磁盘加载。如果图像是从磁盘缓存驱逐,那就得,如果它再次需要被重新下载。

1.) There are 2 layers of cache in Volley, one is the in-memory cache (in RAM) and the other one is a disk cache. Once a cache is full, the oldest image (meaning the image that hasn't been accessed the longest) in that cache will be evicted when a new image is about to be cached to make room for the new items. When something is evicted from the in-memory cache, it is still present in the disk cache and can be loaded very quickly from disk if it is needed again. If an image is evicted from the disk cache, it would have to be redownloaded if it's needed again.

2)一旦你理解了问题答案1.缓存自动使房间较新的内容,也没有理由驱逐手动内容这听起来并不合理。手册驱逐实际上将降低缓存的效率。

2.) This doesn't sound reasonable once you understood the answer to question 1. The cache automatically makes room for newer content and there is no reason to evict content manually. Manual eviction will in fact lower your cache's efficiency.

3)。从广义上讲,这是不可能的(没有黑客),因为它不应该被需要。如果图像资源(几乎)总是在一定的时间之后到期,服务器应该发送资源到客户端使用HTTP标头宣布这一点。例如使用Cache-Control头的max-age的属性。有很多网站,详细解释这一点,例如: HTTP ://www.mobify.com/blog/beginners-guide-to-http-cache-headers/ 。如果图像资源几乎永远不会过期,可以考虑在变革和商店文件名作为一个属性改变其文件名。例如,一个用户可以具有包含URL到化身的化身属性。化身图像可以无限期缓存并更改了图片的URL,如果一个新的头像被上传。

3.) Broadly speaking, this is not possible (without hacks), because it should not be needed. If an image resource (almost) always expires after a certain time, the server should announce this using HTTP headers when sending the resource to the client. For example using the max-age property of the cache-control header. There are lots of websites explaining this in detail, for example: http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/. If an image resource almost never expires, you can consider changing its filename upon change and store that filename as a property. For example a user can have an avatar property containing the URL to the avatar. The avatar image can be cached indefinitely and you change the URL of the image if a new avatar gets uploaded.

这篇关于Android的凌空图像缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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