问题
I am trying to use amazon S3 Client for my files uploading and downloading. For caching i am trying to use Picasso right now.
There are few points to note. 1- In S3, there is no single link for a file. I have to randomly generate a url for each image. This url will expire most likely in 1 min or i can set it to expire in 1 hour.
In my onCreateView of my adapter i am calling something like this
String url = getAmazonTempUrl()
2- Each file name will be same but they can be modified on server. e.g abc.png is a file name and i can be 1mb today and tomorrow there could be one more image there.
Now i want to ask how can picasso handle the key for such scenario. The url is changing all the time IN A Single scroll. Also name is same. I do not want picasso to check name and don't download new updated file.
回答1:
What you probably want is to keep cache of the generated urls in say a HashMap, and if there is no value for a certain key, then generate the url with getAmazonTempUrl()
and store it in the cache.
If you set the URL expiry time to an hour then you can record the activity load time into a variable, and check if that has been longer than an hour. If so, clear the cache.
What you can also do is do a callback with Picasso in case the image fails and recreate the url:
Picasso.with(this).load(imageUrl).into(imgaeView, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
//recreate the url here and store in cache
}
}
);
Edit:
So in your case you could persist the above cache and load it next time the app is started. To deal with image changing under the same url you can try do it the dirty way or the clean way.
The clean way is to change the url for the image if you can. If that's not an option - you can perhaps check the image's last modified date and append that as a timestamp on the url. e.g http://www.sample.com/image.jpg?14869246263 - this works fine with Picasso's cache as long as the the url is exactly the same.
The dirty way is to load the cached version and then immediately after force-reload the same image around the cache (used to be done with skipCache() I believe, but was deprecated in favour of somethingCachePolicy).
来源:https://stackoverflow.com/questions/31354601/setting-key-for-picasso-for-s3-amazon