Example #1
0
 /**
  * Adds to both: the memory and the disk
  */
 public synchronized void add(String key, Serializable n) {
     synchronized(cache){
         super.add(key, n);
         File fileForKey = getFileForKey(key);
         if(DEBUG){
             System.out.println("Disk cache - Adding: "+key+" file: "+fileForKey);
         }
         REF.writeToFile(n, fileForKey);
         keys.add(key);
     }
 }
Example #2
0
 public synchronized Serializable getObj(String key) {
     synchronized(cache){
         Serializable v = super.getObj(key);
         if(v == null && keys.contains(key)){
             //miss in memory... get from disk
             File file = getFileForKey(key);
             if(file.exists()){
                 v = (Serializable) REF.readFromFile(file);
             }else{
                 if(DEBUG){
                     System.out.println("File: "+file+" is in the cache but does not exist (so, it will be removed).");
                 }
             }
             if(v == null){
                 this.remove(key);
                 return null;
             }
             //put it back in memory
             super.add(key, v);
         }
         return v;
     }
 }