bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.65k stars 6.12k forks source link

Cache problem: does HTTP cache-control header work in Glide? #1768

Closed enginebai closed 7 years ago

enginebai commented 7 years ago

Hi, my app provides user to upload avatar, and server side use cache-control: max-age=600, public to control cache.

2017-03-03 3 23 58

My problem is that after uploading the image, the glide still load the cached image, and I use browser to open the image, it's updated. Does glide support cache-control header? Or how can I control cache in this way?

Here is my code to load image, I use signature() api but doesn't work.

Glide.with(getActivity())
        .load(mUser.getCoverPhoto())
        .error(R.drawable.bg_1)
        .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                mTextBioOther.setVisibility(!isMe ? View.VISIBLE : View.GONE);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                mTextBioOther.setVisibility(!isMe ? View.VISIBLE : View.GONE);
                return false;
            }
        })
        .into(mImageCover);
enginebai commented 7 years ago

I fixed the problem by providing signature(new StringSignature(String.valueOf(System.currentTimeMillis() / (1000 * 60 * 10)))).

TWiStErRob commented 7 years ago

Sorry for the silence, yes, Glide doesn't support it out of the box. You can get OkHttp to do the caching and invalidation based on headers for you, but then you have to disable Glide cache (.diskCacheStrategy(NONE)).

enginebai commented 7 years ago

OK, thanks for your kindly help.

platinum7919 commented 7 years ago

Will volley work also? I am currently using volley and I don't want to add okhttp.

TWiStErRob commented 7 years ago

@platinum7919 sure, if you know how to configure it to handle these headers. It doesn't matter what you use as long as you can configure it correctly.

frel commented 6 years ago

Is this still the case with Glide 4.7? Are there any disadvantages to disabling disk cache and using OkHttp cache instead? Our typical use case is avatar images being cached indefinitely. The way we've solved this so far is by invalidating using .signature(ObjectKey). But that invalidates mostly valid caches. Thank you for a wonderful library!

kavinmk6 commented 3 years ago

I fixed the problem by providing signature(new StringSignature(String.valueOf(System.currentTimeMillis() / (1000 * 60 * 10)))).

Hi, Thanks for the solution. In My case I need to set the cache for 3,628,800 seconds. May I know how can I set the calculation for this? Thanks

TWiStErRob commented 3 years ago

@kavinmk6 The example is 10 minutes (600 seconds).