1

in my app I download an XML file. Then that XML file gets assigned to variables. The problem I'm now encountering is that, I want to cache that XML file.

I thought about saving every XML File into different variables, but the problem there would be, that the XML files I download aren't the same. It can be a combination of 4x2x3 possibilites of XML data that can be retrieved. So I thought I would go and only download the file once and then cache it for about 60 seconds. How would I do that?

Currently, I'm downloading a XML file like that:

DocumentBuilderFactory dbf  = DocumentBuilderFactory.newInstance();
try{
    db          = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    e.printStackTrace();
}
try{
   docComplete      = db.parse(new InputSource(new URL( BLOGS_ONLY ).openStream()));
} catch (IOException e) {
    e.printStackTrace();
}  catch (SAXException e) {
    e.printStackTrace();
}

try {
    hashMap                     = getAccordingData( docComplete     );
} catch ( NullPointerException npe ){
    mainAsyncTask.cancel(true);
    mainAsyncTask.execute( );
}

and the getAccordingData method just gets the ElementsByTagName into a NodeList and then with a for-loop adds it to a HashMap that gets returned.

So this is what is happening at the moment. What would your way be to cache it?

Musterknabe
  • 5,763
  • 14
  • 61
  • 117

1 Answers1

1

If you are looking for a Memory Cache use a LruCache instead of HashMap.

If you only want to cache it for 60 seconds, that you may should consider do write a CacheEntry Class that holds the data and a Timestamp when you have loaded the resource.

But keep in mind that you could run out of memory because the XML Document will be hold in Memory. You may consider to use a disk cache like DiskLruCache (depending on the length of your document). Disk Lru Cache


UPDATE: I think you should use a library like Volley that will do xml parsing and caching for you. I also would recommend you to set the caching period for each xml file in the http headers (which volley will respect). Volley Example

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • Whupsi. I meant 60 minutes, not 60 seconsd haha. Yeah, I heard about that problem with the space. But the XML Files all together are 100kb, so the cache shouldn't be a problem, right? For switching completely from HashMap to LruCache I think I'm too far in already. I have already about 4-5 thousand lines of code and the XML caching is the only thing that is still needed. For the time I could just check the time like that, right? http://stackoverflow.com/questions/2389225/android-how-to-get-a-files-creation-date Then I could delete the file after 60 minuets – Musterknabe Nov 19 '13 at 14:27
  • Yes, normally there should be enough Memory for XML Elements (100kb is the xml file, but at the end you have XML Nodes etc.) but if you realy think about using a MemoryCache with 60 minutes, than you simply should skip the 60 Minute check, because its quite possible that the app has been closed within this 60 Minutes. This means that the App has been terminated, which means that also the memory cache has been destroyed. So if your user restarts the App (i.e. after 10 minutes) the memory cache is empty ... So I guess a DiskCache (is persistent) as is more what you should looking for – sockeqwe Nov 19 '13 at 14:36
  • Hey Thanks for the answer. Yeah, thought about that, but seems like I can't check the creation time of the file since we don't get it. And the last modified date is also wrong. Always 0 ms. Is there a way to check if the file is older than a given time? I thought about using a small SQLite database, but that would be way, too much, wouldn't it? – Musterknabe Nov 19 '13 at 14:59
  • To beprecise: you don't know how old the file is but the timestamp when you have loaded it for the last time. Easy way: Let the webserver set the cache-controll http header. So you can simply use volley (or any other http library) and you dont have to worry about caching. Other way: Save the timestamp of the last retrieving. I would recomment you to use the DiskLruCache (mentioned above). You store your xml data with the timestamp on the phone. Before starting a HTTP GET Request you check the DiskCache if its present and not expired (accoring the timestamp), otherwise load xml from web server. – sockeqwe Nov 19 '13 at 16:09