3
votes

I have few issues on caching...

1)I use URLImage as follows

URLImage.createToStorage(profileImg, photoUrl + "__c", photoUrl, URLImage.RESIZE_SCALE_TO_FILL)

But if the image is edited(old one is replaced with new one) in the server,it shows the old img instead of updating to the new image. what should i do to reupdate the cached image.

2)We are developing an news app. Here news are updated daily. The recent ten news are saved in the storage as follows:

    //write the storage object
     do {
          Storage.getInstance().writeObject("newsListingStorage", fetchresponses);
        } while (fetchLastId < 10);

    //read the storage
    public Vector newsListOfflineStorage() {
        Vector newsListingRead = (Vector)Storage.getInstance().readObject("newsListingStorage");
        return fetchresponses;
       }

i am concerned that if a user stores 10 items each day, it'll store much more in a month or a year and the cache items goes on... How can i replace the cache stored every time to the latest 10 items so that there are only ten recent cached items every time one runs the app.

1

1 Answers

1
votes

I don't know if cache expiration is already introduced in codenameone but how I handled this... is caching the images with unique id coming from your database

URLImage.createToStorage(profileImg, "largeImage_" + newsUniqueId + ".jpg", photoUrl, URLImage.RESIZE_SCALE_TO_FILL);

Let's say you stored 10 news earlier, it doesn't matter what ids they have and in your database you have 100 news and you need only the top news with unique ids from 91 to 100. Loop from 0 to 90 and delete those images from storage if they exist.

private void deleteImageFromStorage(String imageName) {
    FileSystemStorage fss = FileSystemStorage.getInstance();
    fss.deleteRetry(fss.getAppHomePath() + imageName, 2);
}

for (int i = 0; i < myTop10NewsLowestId; i++) {
    deleteImageFromStorage("largeImage_" + i + ".jpg");
}