This discussion is archived
9 Replies Latest reply: Feb 6, 2013 7:16 PM by user12679330 RSS

creating cache for multiple property files run time/dynamically.

user12679330 Newbie
Currently Being Moderated
Hi,
I have a requirement, where in I need to create cache for each property file present in a folder at server side or in the lib or resources directory. Please help me how I can do this?

Thanks.
  • 1. Re: creating cache for multiple property files run time/dynamically.
    r035198x Pro
    Currently Being Moderated
    What have you tried? What are your ideas on how to do this? Have you researched how others have solved this problem before?
  • 2. Re: creating cache for multiple property files run time/dynamically.
    user12679330 Newbie
    Currently Being Moderated
    Hi,

    I know single file caching, given the absolute file path with the full property file location. Now there will be multiple files get added run time in a folder location,from there I need to see whenever a new property file is added or an existing property file get updated, server cache needs to be loaded with the updated or newly added files. As of now not having any idea on how can this be done.

    Thanks for your help.

    Edited by: user12679330 on 25-Jan-2013 03:03
  • 3. Re: creating cache for multiple property files run time/dynamically.
    r035198x Pro
    Currently Being Moderated
    You can watch folders using the api described here:http://docs.oracle.com/javase/tutorial/essential/io/notification.html
    For caching you can implement yourself manually or use caching APIs like ehcache.
    Whether you can use any of these techniques or how to use them depends on the environment your application runs in e.g application server or standalone.
  • 4. Re: creating cache for multiple property files run time/dynamically.
    user12679330 Newbie
    Currently Being Moderated
    Hi,
    Thank you. Its an application server, Oracle application server 11g.

    Thanks a lot.
  • 5. Re: creating cache for multiple property files run time/dynamically.
    user12679330 Newbie
    Currently Being Moderated
    Hi,
    Any more ideas on this please. I will be getting multiple property files at run time, and I need to maintain cache for them, as and when they are modified or newly added. Basically a multi dimensional Hasmap with multiple files storing each file key value pairs.

    Thanks in advance.

    Edited by: user12679330 on 27-Jan-2013 20:54
  • 6. Re: creating cache for multiple property files run time/dynamically.
    EJP Guru
    Currently Being Moderated
    Use a java.util.ResourceBundle per file, always access them via the static getBundle() method (don't store references to them), and call clearCache() when any of the files change, which you can detect in several ways using java.io or NIO2.
  • 7. Re: creating cache for multiple property files run time/dynamically.
    user12679330 Newbie
    Currently Being Moderated
    ok thank you.

    I follwed this method implementation:

    static HashMap<String, HashMap<Object, String>> cacheHolder = new HashMap<String, HashMap<Object, String>>();
         static HashMap<Object, String>[] cache = new HashMap[2];
         static Integer fileCount = 0;
         static int incrementSize = 2;
    public method1(Map<Object,String>map){
    File file = new File((new StringBuilder(
                             "ABC/XYZ/")).append(value)
                             .toString()); // where value is the file name returned from the external method
                   int newSize = existingMapLength+incrementSize;
                   if (someVal== null) {                    
                        synchronized (fileCount) {
                             int oldSize = cache.length;
                             if(fileCount==cache.length){
                                  HashMap[] oldData = new HashMap[oldSize];
                                  oldData = cache;                              
                                  cache = new HashMap[newSize];
                                  LOGGER.info("New Size added:==>"+cache.length);
                                  for(int i=0;i<oldSize;i++){                                   
                                       cache[i] = oldData;
                                       }
                                  cache[fileCount] = readExternalPropertiesFile(file); // external method which returns the properties of the file in hashmap
                                  cacheHolder.put(value, cache[fileCount]);                    
                                  keys = cache[fileCount].keySet();
                                                           }
                             else{                         
                             cache[fileCount] = readPropertiesFile(file);
                             cacheHolder.put(value, cache[fileCount]);                    
                             keys = cache[fileCount].keySet();
                             }
                             someVal= cache[fileCount];
    fileCount = fileCount + 1;

                        }
    }
    }

    Please let me know if any improvemnets are possible.
  • 8. Re: creating cache for multiple property files run time/dynamically.
    EJP Guru
    Currently Being Moderated
    I have no idea why you are asking me to review a solution that has absolutely nothing to do with what I recommended.
  • 9. Re: creating cache for multiple property files run time/dynamically.
    user12679330 Newbie
    Currently Being Moderated
    HI EJP,

    Sorry, I didn't get it, how to implement the way you explained. so tried it in a different approach.

    A sample example can help.

    Later I have used concurrentHashmap for the cache implementation.

    Thanks for your help.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points