wood burning stoves 2.0*
The moose likes Java Micro Edition and the fly likes Image Caching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Image Caching" Watch "Image Caching" New topic
Author

Image Caching

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Steffy's message in a previous thread made me think about whether an image caching framework would be useful.
What I mean by an image caching framework is:
  • There's a remote server serving image descriptor files (XML, property file, binary, something else)
  • The client has a component called "ImageCache", which can be configured to point at a specific URL, the remote server's "image cache root".
  • ImageCache.getImage(String id) checks whether the requested image is locally cached on the device and if not, fetches it from the server. The fetching would actually include fetching the image descriptor and the actual image file. The descriptor would have instructions on how often the client should refresh its copy and other useful information. The idea being that the application developer could define an optimal caching strategy for each particular application using the caching descriptors.


  • Do you see a need for this kind of utility or is it just "cool and unnecessary complexity"?


    Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
    Daryl Wilding-McBride
    Author
    Ranch Hand

    Joined: Jul 18, 2003
    Posts: 60
    Good idea. I've made a start in the ImageViewer example in the book, although it doesn't have your image descriptor idea. The book example uses a web service to retrieve the image and put it in a local cache, which uses an RMS database on the Palm and a binary file on the filesystem on the PocketPC. The web service has two methods: one to retrieve a list of descriptors (which is just a string in my example) for available images on the server, and another to retrieve an image object, which has attributes of the image itself, as well as the timestamp when it was last modified.


    Daryl Wilding-McBride<br />Author of "Java Development on PDAs" (ISBN 0-201-71954-1)<br /><a href="http://www.javaonpdas.com" target="_blank" rel="nofollow">http://www.javaonpdas.com</a>
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    Interesting. Now I really have to get the book
    I wonder what would be an appropriate algorithm for managing the cache regarding memory consumption... In J2SE/J2EE applications using weak references within the cache implementation would probably be enough (cached objects would be gc'd before anything else) but how about a J2ME app?
    Daryl Wilding-McBride
    Author
    Ranch Hand

    Joined: Jul 18, 2003
    Posts: 60
    My example uses the file system for the cache rather than memory, but you still need to know whether it's dirty or not. These approaches come to mind:
  • Retrieve it from the cache until it's a certain age, then ask the server whether there's a new copy available.
  • Whenever you access the server for any reason, ask it whether any cached images need to be refreshed.
  • Poll the server at regular intervals, asking whether any of the images should be refreshed.

  • For the last two, either the server would need to remember which images the client has already seen and when (which probably wouldn't scale very well), or the client would need to tell the server which images were in its cache and how old they were.
    [ July 24, 2003: Message edited by: Daryl Wilding-McBride ]
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    I was kinda settled on the server giving a validity period along with each over-the-network fetch.
    Anyway, about the memory consumption... How could one control the amount of memory/storage used for caching? Here are my initial thoughts:
  • Persist a preferences file with a property "imagecache.maxsize"
  • Upon startup, read the maximum size used for caching and compare it to the files already in the cache --> initialize the "available memory left for caching" variables used to determine whether the ImageCache component needs to make some room for the new entries (if the newcomer has a higher "caching priority")


  • How did you actually use the filesystem? Proprietary API? RMS?
    Daryl Wilding-McBride
    Author
    Ranch Hand

    Joined: Jul 18, 2003
    Posts: 60
    On the Palm I used RMS. On the PocketPC I used FileInputStream and FileOutputStream. I didn't store images in memory, and I imagine on Palm using MIDP you would run out of memory pretty quickly.
    Francis Siu
    Ranch Hand

    Joined: Jan 04, 2003
    Posts: 867
    I imagine on Palm using MIDP you would run out of memory pretty quickly
    Sorry to interrupt
    Is MIDP a file type?
    And the second question is that
    1:Loading the image from the pocketPC
    2:Sending the image to pocketPC
    Which way is the best?
    Do I need to consider the transfer rite and the image size?
    thanks


    Francis Siu
    SCJP, MCDBA
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    Is MIDP a file type?

    MIDP stands for Mobile Information Device Profile and is not a file type. It's a J2ME profile, a kind of a category for certain types of mobile devices.
    And the second question is that
    1:Loading the image from the pocketPC
    2:Sending the image to pocketPC
    Which way is the best? Do I need to consider the transfer rite and the image size?

    What do you mean by "loading" and "sending"?
    Francis Siu
    Ranch Hand

    Joined: Jan 04, 2003
    Posts: 867
    What do you mean by "loading" and "sending"?
    Loading means that when the pocketPC received a signal,then according to the signal that loading the required image from pocketPC.
    Sending means that the server send a image to the pocketPC.
    thanks
    sing
    Ranch Hand

    Joined: Nov 29, 2001
    Posts: 121
    Yes. I agreed that this is a good idea to put images to cache but if using local cache - RMS, will it run out memory space if i need a lot of images?
    if i use client's component - imagecache that suggested by lasse, will the content in the cache loss for centain circumstances like no battery.
    steffy
    [ July 25, 2003: Message edited by: Steffy Sing ]
    sing
    Ranch Hand

    Joined: Nov 29, 2001
    Posts: 121
    On the Palm I used RMS. On the PocketPC I used FileInputStream and FileOutputStream. I didn't store images in memory, and I imagine on Palm using MIDP you would run out of memory pretty quickly.

    I thought the RMS is consider memory?
    if using FileInputStream and FileOutputStream, we will facing slow display if the connection is slow, am i wrong?
    sing
    Ranch Hand

    Joined: Nov 29, 2001
    Posts: 121
    1:Loading the image from the pocketPC
    2:Sending the image to pocketPC
    Which way is the best?

    I think these two are the same.
    sing
    Ranch Hand

    Joined: Nov 29, 2001
    Posts: 121
    Very thank you to two of you, i have some ideas to doing the application.
    Francis Siu
    Ranch Hand

    Joined: Jan 04, 2003
    Posts: 867
    I think these two are the same
    It is different
    Case 1:
    Consideration of transfer rate that only the signal for requesting the image from pocketPC and
    the latence of device loading the image
    Case 2:
    Transfer rate that involving the signal containing the image(large size slower than small size if in the normal condition)
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    I agreed that this is a good idea to put images to cache but if using local cache - RMS, will it run out memory space if i need a lot of images?

    Caches usually have some algorithm for deciding what to keep and what to dump when resources are limited. For example, using the LRU (least recently used) algorithm the ImageCache would keep the cached data under, say, 50kb by throwing out the least recently used cached object when something new is to be cached (and the 50kb limit would be exceeded).
    if i use client's component - imagecache that suggested by lasse, will the content in the cache loss for centain circumstances like no battery.

    That depends. It would be possible to persist the cache's contents every now and then to RMS (avoiding content loss upon battery failure). On the other hand, once you've recharged the battery and open the application, the caching period is probably over and the application needs to refetch the image anyway.
    By the way, let's call our ImageCache with another name from now on (as it could be used for any static resource). MobileCache?
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    I thought the RMS is consider memory?

    RMS is persistent memory. You can think of RMS as an API for accessing the device's "hard disk".
     
    Consider Paul's rocket mass heater.
     
    subject: Image Caching