1 19 20 package org.netbeans.modules.tasklist.docscan; 21 22 import org.netbeans.modules.tasklist.providers.SuggestionContext; 23 import org.openide.filesystems.FileObject; 24 import org.openide.filesystems.FileUtil; 25 import org.openide.ErrorManager; 26 27 import java.util.*; 28 import java.io.*; 29 30 36 final class Cache { 37 38 private static Map cache; 39 40 public static void put(SuggestionContext ctx, List result) { 41 FileObject fo = ctx.getFileObject(); 42 String name = createKey(fo); 43 if (name == null) return; 44 if (result == null || result.isEmpty()) { 45 long timestamp = fo.lastModified().getTime(); 46 cache().put(name, new long[]{timestamp, System.currentTimeMillis()}); 47 } else { 48 cache().remove(name); 49 } 50 } 51 52 public static List get(SuggestionContext ctx) { 53 FileObject fo = ctx.getFileObject(); 54 String name = createKey(fo); 55 if (name == null) return null; 56 Object hit = cache().get(name); 57 if (hit != null) { 58 if (((long[])hit)[0] >= fo.lastModified().getTime()) { 59 return Collections.EMPTY_LIST; 60 } else { 61 cache().remove(name); 62 } 63 } 64 return null; 65 } 66 67 private static String createKey(FileObject fo) { 68 File file = FileUtil.toFile(fo); 69 if (file != null) { 70 try { 71 return file.getCanonicalPath(); 72 } catch (IOException e) { 73 return null; 74 } 75 } 76 return null; 77 } 78 79 private static Map cache() { 80 if (cache == null) { 81 load(); 82 } 83 return cache; 84 } 85 86 public static void load() { 87 ObjectInputStream ois = null; 88 try { 89 File file = getCacheFile(false); 90 InputStream in = new BufferedInputStream(new FileInputStream(file)); 91 ois = new ObjectInputStream(in); 92 if (ois.readInt() == 1) { 93 cache = (Map) ois.readObject(); 94 } 95 } catch (IOException io) { 96 } catch (ClassNotFoundException e) { 98 } finally { 100 if (ois != null) { 101 try { 102 ois.close(); 103 } catch (IOException e) { 104 } 106 } 107 } 108 if (cache == null) { 109 cache = new HashMap(1113); 110 } 111 112 long staleTime = System.currentTimeMillis() - 1000*60*60*24*17; if (Settings.getDefault().getModificationTime() > staleTime) { 115 staleTime = Settings.getDefault().getModificationTime(); 116 } 117 118 Iterator it = cache.entrySet().iterator(); 119 while (it.hasNext()) { 120 Map.Entry entry = (Map.Entry) it.next(); 121 if (((long[])entry.getValue())[1] < staleTime) { 122 it.remove(); 123 } 124 } 125 126 } 127 128 public static void store() { 131 if (cache == null) return; 132 try { 133 File file = getCacheFile(true); 134 OutputStream os = new FileOutputStream(file); 135 os = new BufferedOutputStream(os); 136 ObjectOutputStream oos = new ObjectOutputStream(os); 137 oos.writeInt(1); 138 139 oos.writeObject(cache); 142 oos.close(); 143 } catch (IOException ex) { 144 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 145 } finally { 146 cache = null; 147 } 148 } 149 150 private static File getCacheFile(boolean create) throws IOException { 151 String loc = System.getProperty("netbeans.user") + File.separatorChar + "var" + File.separatorChar + "cache" + File.separatorChar + "all-todos.ser"; 153 File file = new File(loc); 154 if (create) { 155 if (!file.exists()) { 156 File parent = file.getParentFile(); 157 parent.mkdirs(); 158 file.createNewFile(); 159 } 160 } 161 return file; 162 } 163 164 } 165 | Popular Tags |