title: Image Cache Signature in Glide with Metadata
body: <p>Users can change a picture (replace it). Once the user changes their image, I want the new image to get cached in Glide and the old image to get thrown out of the cache. </p>
  
  <p>I've read everything online but I'm still at a loss on how to implement a good solution to this. </p>
  
  <p>I've tried skipping the local memory and disk caches like when setting the image:</p>
  
  <pre><code>GlideApp.with(fragment)
    .load(url)
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .skipMemoryCache(true)
    .into(view);
  </code></pre>
  
  <p>This solution is SLOW because it now calls the new image every single time - it never caches the new image.</p>
  
  <p>Glide documentation says:</p>
  
  <blockquote>
    <p>the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc) when possible. - <a href="https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation" rel="nofollow noreferrer">https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation</a></p>
  </blockquote>
  
  <p>But that is not possible for me, so Glide documentation then says:</p>
  
  <blockquote>
    <p>Since it’s often difficult or impossible to change identifiers, Glide
    also offers the signature() API to mix in additional data that you
    control into your cache key.</p>
  </blockquote>
  
  <p>And it gives this example:</p>
  
  <pre><code>Glide.with(yourFragment)
      .load(yourFileDataModel)
      .signature(new ObjectKey(yourVersionMetadata))
      .into(yourImageView);
  </code></pre>
  
  <p>But here comes the issue. What would be a good "yourVersionMetadata"? How do I create and maintain it? I've seen examples like so:</p>
  
  <pre><code>.signature(new ObjectKey(Long.toString(System.currentTimeMillis())))
  </code></pre>
  
  <p>This causes the disk cache key to change every time I load the image, so it is SLOW. I just need it to change when the user replaces the image. Not every time the image loads.</p>
  
  <p>Someone wrote:</p>
  
  <blockquote>
    <p>You can do something like generate a new UUID or increment an integer whenever the image changes. If you go that route, you'll have to keep track of the current signature for each image somewhere. - <a href="https://github.com/bumptech/glide/issues/2841" rel="nofollow noreferrer">https://github.com/bumptech/glide/issues/2841</a></p>
  </blockquote>
  
  <p>I don't understand how to do this.</p>
  
  <p>I also tried the Async task for completely deleting cache. It works, but again it's super slow (and Glide doesn't recommend using this approach).</p>
  
  <p>I don't know how I can just insert the current signature (which should be faster) rather than creating a new signature every time the image loads. Help? It seems to replace an image and recaching it shouldn't be so difficult!</p>