Skip to content

Declarative Cache Control API #438

Closed

Description

@JakeWharton

I've been sitting on this for a while as a future feature but hadn't documented it.

The API would allow specifying a cache interaction policy both "downward" on the load and "upward" back through the processing. Through this you can facilitate complex cache interactions for both valid and seemingly-questionable use cases.

Random subset of interactions from other issues:

  • Skip memory cache "downward" only.
  • Skip memory cache "upward" only.
  • Load from local cache (memory or disk) only.
  • Force re-load from network.

I'm not going to do a full-on API strawman, but I know we'll probably want some EnumSet action with

public enum BitmapSource { MEMORY, DISK, NETWORK }
public enum BitmapSink { MEMORY, DISK }

This API will trump the existing weak skipCache() method on RequestCreator.

Activity

  1. added this to the milestone on Mar 12, 2014
  2. TequilaZhang commented on Mar 31, 2014

    @TequilaZhang

    Hi:
    Support for force reloading,now? Thank you..
    I think your code is very amazing,but sometiomes I want to clear the cache (memory and disk), reload from cloud.Could you tell me a way to do that?

  3. JakeWharton commented on Mar 31, 2014

    @JakeWharton
    CollaboratorAuthor

    Not yet. No.

    On Sun, Mar 30, 2014 at 10:26 PM, TequilaZhang notifications@github.comwrote:

    Hi:
    Support for force relosding,now?

    Reply to this email directly or view it on GitHubhttps://github.com//issues/438#issuecomment-39054917
    .

  4. TequilaZhang commented on Mar 31, 2014

    @TequilaZhang

    Hi:
    will you update it or not later?

  5. ovy9086 commented on Mar 31, 2014

    @ovy9086

    is work on this started on a branch? or after 2.3 release?

  6. JakeWharton commented on Mar 31, 2014

    @JakeWharton
    CollaboratorAuthor

    Not yet started.

    On Mon, Mar 31, 2014 at 3:28 PM, Ovidiu Latcu notifications@github.comwrote:

    is work on this started on a branch? or after 2.3 release?

    Reply to this email directly or view it on GitHubhttps://github.com//issues/438#issuecomment-39150220
    .

  7. dnkoutso commented on Mar 31, 2014

    @dnkoutso
    Collaborator

    After 2.3.

  8. Queatz commented on May 22, 2014

    @Queatz

    We need this, buddy.

  9. modified the milestones: Picasso 2.4, on May 27, 2014
  10. dnkoutso commented on May 27, 2014

    @dnkoutso
    Collaborator

    Moved to 2.4. This will be a good new feature for 2.4.

  11. dnkoutso commented on Jul 8, 2014

    @dnkoutso
    Collaborator

    This will be scoped (and most likely implemented) for 2.4.

    First thing is scroll listener based loading.

  12. odedregev commented on Jul 14, 2014

    @odedregev

    +1

    In the mean time the following line of code will force download from network:
    //force no proxy cache by appending a random number to the image url
    String imageurl = person.getImageUrl() + "?t=" + System.currentTimeMillis();

  13. jfyles commented on Aug 1, 2014

    @jfyles

    That works, but then it caches it that that throwaway url as the key, so it'll reload for each individual one. We absolutely need to be able to invalidate any cache on demand. Is there an updated timeline on this?

  14. JakeWharton commented on Aug 1, 2014

    @JakeWharton
    CollaboratorAuthor

    No timeline.
    On Aug 1, 2014 9:46 AM, "Justin Fyles" notifications@github.com wrote:

    That works, but then it caches it that that throwaway url as the key, so
    it'll reload for each individual one. We absolutely need to be able to
    invalidate any cache on demand. Is there an updated timeline on this?

    —
    Reply to this email directly or view it on GitHub
    #438 (comment).

  15. 17 remaining items

  16. removed this from the milestone on Dec 12, 2014
  17. jvcjunior commented on Feb 12, 2015

    @jvcjunior

    Sorry if this question is silly.
    I am loading images from disk. When I remove the file from gallery the image still loads. What should I do to invalidate this image or clean this specific image from cache? I don't understand how to do it using MemoryPolicy or NetworkPolicy.

  18. dnkoutso commented on Feb 12, 2015

    @dnkoutso
    Collaborator

    Read MemoryPolicy and NetworkPolicy JavaDoc.

    picasso.load(url).memoryPolicy(NO_CACHE).networkPolicy(NO_CACHE) to force the request to reload from the network.

    There is also clearKeyUri in Cache you can use to evict the image from the memory cache.

  19. Karlooie commented on Mar 9, 2015

    @Karlooie

    Hello,
    I'm facing a problem with network policy.

    my code to reload an image from network is:

                Picasso.with(MainActivity.this).load(url)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .into(mPicassoTg);
    

    note: mPicassoTg is a field of my main activity, so I hope it's not garbage collected.

    behavior:
    mPicassoTg.onBitmapLoaded is NOT CALLED. (nor is onBitmapFailed)
    mPicassoTg.onPrepareLoad is called

    Everything works fine if I remove networkPolicy setter,
    It works also on a new image, that is not in the cache (network is working and url is correct).

                Picasso.with(MainActivity.this).load(url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .into(mPicassoTg);
    

    Am I missing something?
    need more details?

    Thank you for the great Picasso and all the support,
    Carlo

  20. oliverhausler commented on Apr 11, 2015

    @oliverhausler

    I want to use disk cache, but not cache any images in memory (in order to keep memory consumption low), but as it looks

    .memoryPolicy(MemoryPolicy.NO_STORE)

    implicitly applies NetworkPolicy.NO_STORE as well. Is this a bug or can I get around this? Does my approach of reducing memory consumption make sense at all?

  21. JakeWharton commented on Apr 11, 2015

    @JakeWharton
    CollaboratorAuthor

    Just set a Cache instance which does nothing. Then you don't have to worry
    about including those options on every call.

    On Sat, Apr 11, 2015, 2:43 PM oliverhausler notifications@github.com
    wrote:

    I want to use disk cache, but not cache any images in memory (in order to
    keep memory consumption low), but as it looks

    .memoryPolicy(MemoryPolicy.NO_STORE)

    implicitly applies NetworkPolicy.NO_STORE as well. Is this a bug or can I
    get around this? Does my approach of reducing memory consumption make sense
    at all?

    —
    Reply to this email directly or view it on GitHub
    #438 (comment).

  22. oliverhausler commented on Apr 11, 2015

    @oliverhausler

    I'm not worried about calling this on every call, I want to use disk cache, but not memory cache. Is that possible and does that make sense at all? (I am handling many large images, which are not accessed often, so I want to cache them on disk, but not in memory. GC is slow sometimes and my app crashes due to high memory consumption, even though I have removed all my references already. So I wanted to have the least load on memory possible.)

    [I'm also trying to work around #972]

  23. JakeWharton commented on Apr 11, 2015

    @JakeWharton
    CollaboratorAuthor

    Picasso only has a memory cache so making it no-op won't affect the disk
    caching (which lives in the HTTP client).

    I can look into the cache API behavior later.

    On Sat, Apr 11, 2015, 2:53 PM oliverhausler notifications@github.com
    wrote:

    I'm not worried about calling this on every call, I want to use disk
    cache, but not memory cache. Is that possible and does that make sense at
    all? (I am handling many large images, which are not accessed often, so I
    want to cache them on disk, but not in memory. GC is slow sometimes and my
    app crashes due to high memory consumption, even though I have removed all
    my references already. So I wanted to have the least load on memory
    possible.)

    —
    Reply to this email directly or view it on GitHub
    #438 (comment).

  24. mrafieej commented on Apr 24, 2015

    @mrafieej

    @JakeWharton
    I am having memory issues also. could you please elaborate on what the right way to set a Cache instance is? Thanks!

  25. mrafieej commented on Apr 24, 2015

    @mrafieej

    Also invalidate is not working for me some reason. I am calling it like this (called from an activity):
    Picasso.with(this).invalidate(image_url);
    I want to load the new image when the user uploads a new one, but it is not working for some reason... I would appreciate any comments on that as well.

  26. nitrodamsel commented on May 3, 2015

    @nitrodamsel

    I have the same problem with @mrafieej . When I use invalidate after the basic method of loading an image, somewhere during the application, the images don't show anything (since there is no error image, which is fine to me),

    I am trying any codes posted here but no luck. A detail of the problem is this: when the user changes the image from another device or platform (Web browser, or in an IOS device), it doesn't update the image in the android app. Can you help me? Thanks!

  27. danieljanes commented on May 15, 2015

    @danieljanes

    How would we use this to cache images only for the lifetime of a component (Fragment, ViewGroup, ...)?
    Use case: We have a fragment with a grid of images which should be cached for the lifetime of the fragment. When the user navigates away from and back to the fragment we want the images to reload.

  28. omid-nazifi commented on Jun 19, 2015

    @omid-nazifi

    Hi, I wanna cache images in memory by Picasso but I have a problem for reloading. Is it possible changing an image when its resource has changed in server. In other words, when network is off, used from cached image in memory and when network is On check the URL, If resource has changed then reload image. if not use a previous image.
    Please let me know it is possible or not? And How can I do it?

    Thanks

  29. parthanjaria commented on Jun 21, 2018

    @parthanjaria

    I am also facing similar problem. I am using 2.5.2 version.

    I have an image(profile_picture) at a url. Now i want that to be cached. But when I re-upload/change the image on the server with same link, i want picasso to update that image.

    I tried using invalidate(url), memoryPolicy(NO_CACHE), networkPolicy(NO_CACHE) but nothing seems to work. need urgent solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions