1

What I need is : I am using glide to show images in my app. I need to set a expiry time for cached images in glide. I understand that we can use signature(Which is now changed as GlideImageVersion). I found that to set expiry for 600 seconds, I have used "System.currentTimeMillis()/(1000 * 60 *10)".

EDIT : Expiry technically means, I need to set a value for signature and that value should not change for 3,628,800 seconds (60,480 minutes).

For 600 seconds:

 currentTime = System.currentTimeMillis()
 ExpiryFor10mins = currentTime / (1000*60*10)  //For 10 minutes
Glide.with(getActivity())
    .load(mUser.getCoverPhoto())
    .error(R.drawable.bg_1)
    .signature(new StringSignature(ExpiryFor10mins.toInt())
    .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);

But I need to set this cache expiry for 3,628,800 seconds, which is for 6 weeks. Can anyone help to figure out the Math for this? Thanks in advance...

CavinAndro
  • 71
  • 5
  • did you want to clear all cache, or single image cache??\ – DeePanShu Sep 22 '21 at 05:48
  • I wanna to clear the single Image Cache... – CavinAndro Sep 22 '21 at 13:08
  • I think that is not possible in glide, you can override the same url using signature but not clean cache for single image – DeePanShu Sep 22 '21 at 14:30
  • I don't want clear the cache. Technically I need to override the same Url using signature. The signature now I am using is "System.currentTimeMillis()/(1000 * 60 *10)" . This gives an value which won't change for 10 minutes. So for that 10 minutes, I will get the image from cache. After 10 minutes , when the signature value changes, glide will override the Url, so it will download the image again. But I need a Math to make the signature not to change for 6 weeks(60,480 minutes) – CavinAndro Sep 22 '21 at 15:05
  • Is this signature working for 10 minutes (600000 miliseconds)?? – DeePanShu Sep 23 '21 at 05:38
  • Are you asking how many milliseconds are in 60,480 minutes? Your code already multiplies by 1000*60 to convert 10 minutes into milliseconds. – Joe Sep 25 '21 at 00:04

1 Answers1

2

You can use your Own SharedPreferences for this, I know its a long way but works perfect for your case, let's take an example:-

SharedPreferences signaturePref = getActivity().getSharedPreferences("signature_for_glide",MODE_PRIVATE);

// take default value 0 till its not saved
// get save signature for current image
long savedSignature = signaturePref.getLong(mUser.getCoverPhoto().trim(),0);
long currentTime = System.currentTimeMillis();

if((currentTime - savedSignature) > 6 weeks millseconds){
    savedSignature = currentTime;
    signaturePref.putLong(mUser.getCoverPhoto().trim(),savedSignature);
}

Glide.with(getActivity())
    .load(mUser.getCoverPhoto())
    .error(R.drawable.bg_1)
    .signature(new StringSignature(savedSignature))
    .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);

Try this solution and if any error pls. ask.

DeePanShu
  • 1,236
  • 10
  • 23