In Android How can I Set Glide Signature (Cache expiry) for the cached Images

901 Views Asked by At

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...

1

There are 1 best solutions below

0
DeePanShu On

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.