/** * @see java.util.LinkedHashMap#removeEldestEntry */ @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return size() > CACHE_ENTRIES; }
/** * Print out cache stats to the specified stream. Note that in many cases * treating puts as misses gives a better version of hit percentage than * actually using misses, since it's possible that some of your misses are * because you wind up choosing not to cache the particular value (we output * both versions). Stats are reset when the cache is loaded in from disk * but are otherwise cumulative. */ public void printStats(PrintStream out) { out.println("cache stats: size: " + size() + ", hits: " + hits + ", misses: " + misses + ", puts: " + puts + ", hit % (using misses): " + ((float)hits)/(hits + misses) + ", hit % (using puts): " + ((float)hits)/(hits + puts)); }
/** * Dump out the contents of the cache to the backing file. */ public void write() { // Do this even if not writing so we printStats() at good times entriesSinceLastWritten = 0; if (frequencyToWrite < CACHE_ENTRIES/4) frequencyToWrite *= 2; if (backingFile == null) return; try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(backingFile))) { log.info("Writing cache (size: " + size() + ") to " + backingFile); oos.writeObject(this); } catch (Exception ex) { log.info("Error writing cache to file: " + backingFile + '!'); log.info(ex); } }
/** * Creates a new file-backed CacheMap or loads it in from the specified file * if it already exists. The parameters passed in are the same as the * constructor. If useFileParams is true and the file exists, all of your * parameters will be ignored (replaced with those stored in the file * itself). If useFileParams is false then we override the settings in the * file with the ones you specify (except loadFactor and accessOrder) and * reset the stats. */ public static <K,V> CacheMap<K,V> create(int numEntries, float loadFactor, boolean accessOrder, String file, boolean useFileParams) { try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { CacheMap<K, V> c = ErasureUtils.uncheckedCast(ois.readObject()); log.info("Read cache from " + file + ", contains " + c.size() + " entries. Backing file is " + c.backingFile); if (!useFileParams) { c.backingFile = file; c.hits = c.misses = c.puts = 0; c.CACHE_ENTRIES = numEntries; } return c; } catch (FileNotFoundException ex) { log.info("Cache file " + file + " has not been created yet. Making new one."); return new CacheMap<>(numEntries, loadFactor, accessOrder, file); } catch (Exception ex) { log.info("Error reading cache file " + file + ". Making a new cache and NOT backing to file."); return new CacheMap<>(numEntries, loadFactor, accessOrder); } }
/** * @see java.util.LinkedHashMap#removeEldestEntry */ @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return size() > CACHE_ENTRIES; }
/** * @see java.util.LinkedHashMap#removeEldestEntry */ @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { if (size() > CACHE_ENTRIES) { return true; } else { return false; } }
/** * @see java.util.LinkedHashMap#removeEldestEntry */ @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return size() > CACHE_ENTRIES; }
/** * Print out cache stats to the specified stream. Note that in many cases * treating puts as misses gives a better version of hit percentage than * actually using misses, since it's possible that some of your misses are * because you wind up choosing not to cache the particular value (we output * both versions). Stats are reset when the cache is loaded in from disk * but are otherwise cumulative. */ public void printStats(PrintStream out) { out.println("cache stats: size: " + size() + ", hits: " + hits + ", misses: " + misses + ", puts: " + puts + ", hit % (using misses): " + ((float)hits)/(hits + misses) + ", hit % (using puts): " + ((float)hits)/(hits + puts)); }
/** * Print out cache stats to the specified stream. Note that in many cases * treating puts as misses gives a better version of hit percentage than * actually using misses, since it's possible that some of your misses are * because you wind up choosing not to cache the particular value (we output * both versions). Stats are reset when the cache is loaded in from disk * but are otherwise cumulative. */ public void printStats(PrintStream out) { out.println("cache stats: size: " + size() + ", hits: " + hits + ", misses: " + misses + ", puts: " + puts + ", hit % (using misses): " + ((float)hits)/(hits + misses) + ", hit % (using puts): " + ((float)hits)/(hits + puts)); }
/** * Print out cache stats to the specified stream. Note that in many cases * treating puts as misses gives a better version of hit percentage than * actually using misses, since it's possible that some of your misses are * because you wind up choosing not to cache the particular value (we output * both versions). Stats are reset when the cache is loaded in from disk * but are otherwise cumulative. */ public void printStats(PrintStream out) { out.println("cache stats: size: " + size() + ", hits: " + hits + ", misses: " + misses + ", puts: " + puts + ", hit % (using misses): " + ((float)hits)/(hits + misses) + ", hit % (using puts): " + ((float)hits)/(hits + puts)); } }
/** * Dump out the contents of the cache to the backing file. */ public void write() { // Do this even if not writing so we printStats() at good times entriesSinceLastWritten = 0; if (frequencyToWrite < CACHE_ENTRIES/4) frequencyToWrite *= 2; if (backingFile == null) return; try { System.err.println("Writing cache (size: " + size() + ") to " + backingFile); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(backingFile)); oos.writeObject(this); } catch (Exception ex) { System.err.println("Error writing cache to file: " + backingFile + "!"); System.err.println(ex); } }
/** * Dump out the contents of the cache to the backing file. */ public void write() { // Do this even if not writing so we printStats() at good times entriesSinceLastWritten = 0; if (frequencyToWrite < CACHE_ENTRIES/4) frequencyToWrite *= 2; if (backingFile == null) return; try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(backingFile))) { log.info("Writing cache (size: " + size() + ") to " + backingFile); oos.writeObject(this); } catch (Exception ex) { log.info("Error writing cache to file: " + backingFile + '!'); log.info(ex); } }
/** * Dump out the contents of the cache to the backing file. */ public void write() { // Do this even if not writing so we printStats() at good times entriesSinceLastWritten = 0; if (frequencyToWrite < CACHE_ENTRIES/4) frequencyToWrite *= 2; if (backingFile == null) return; try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(backingFile))) { log.info("Writing cache (size: " + size() + ") to " + backingFile); oos.writeObject(this); } catch (Exception ex) { log.info("Error writing cache to file: " + backingFile + '!'); log.info(ex); } }
/** * Creates a new file-backed CacheMap or loads it in from the specified file * if it already exists. The parameters passed in are the same as the * constructor. If useFileParams is true and the file exists, all of your * parameters will be ignored (replaced with those stored in the file * itself). If useFileParams is false then we override the settings in the * file with the ones you specify (except loadFactor and accessOrder) and * reset the stats. */ public static <K,V> CacheMap<K,V> create(int numEntries, float loadFactor, boolean accessOrder, String file, boolean useFileParams) { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); CacheMap<K, V> c = ErasureUtils.uncheckedCast(ois.readObject()); System.err.println("Read cache from " + file + ", contains " + c.size() + " entries. Backing file is " + c.backingFile); if (!useFileParams) { c.backingFile = file; c.hits = c.misses = c.puts = 0; c.CACHE_ENTRIES = numEntries; } return c; } catch (FileNotFoundException ex) { System.err.println("Cache file " + file + " has not been created yet. Making new one."); return new CacheMap<K, V>(numEntries, loadFactor, accessOrder, file); } catch (Exception ex) { System.err.println("Error reading cache file " + file + ". Making a new cache and NOT backing to file."); return new CacheMap<K, V>(numEntries, loadFactor, accessOrder); } }
/** * Creates a new file-backed CacheMap or loads it in from the specified file * if it already exists. The parameters passed in are the same as the * constructor. If useFileParams is true and the file exists, all of your * parameters will be ignored (replaced with those stored in the file * itself). If useFileParams is false then we override the settings in the * file with the ones you specify (except loadFactor and accessOrder) and * reset the stats. */ public static <K,V> CacheMap<K,V> create(int numEntries, float loadFactor, boolean accessOrder, String file, boolean useFileParams) { try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { CacheMap<K, V> c = ErasureUtils.uncheckedCast(ois.readObject()); log.info("Read cache from " + file + ", contains " + c.size() + " entries. Backing file is " + c.backingFile); if (!useFileParams) { c.backingFile = file; c.hits = c.misses = c.puts = 0; c.CACHE_ENTRIES = numEntries; } return c; } catch (FileNotFoundException ex) { log.info("Cache file " + file + " has not been created yet. Making new one."); return new CacheMap<>(numEntries, loadFactor, accessOrder, file); } catch (Exception ex) { log.info("Error reading cache file " + file + ". Making a new cache and NOT backing to file."); return new CacheMap<>(numEntries, loadFactor, accessOrder); } }
/** * Creates a new file-backed CacheMap or loads it in from the specified file * if it already exists. The parameters passed in are the same as the * constructor. If useFileParams is true and the file exists, all of your * parameters will be ignored (replaced with those stored in the file * itself). If useFileParams is false then we override the settings in the * file with the ones you specify (except loadFactor and accessOrder) and * reset the stats. */ public static <K,V> CacheMap<K,V> create(int numEntries, float loadFactor, boolean accessOrder, String file, boolean useFileParams) { try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { CacheMap<K, V> c = ErasureUtils.uncheckedCast(ois.readObject()); log.info("Read cache from " + file + ", contains " + c.size() + " entries. Backing file is " + c.backingFile); if (!useFileParams) { c.backingFile = file; c.hits = c.misses = c.puts = 0; c.CACHE_ENTRIES = numEntries; } return c; } catch (FileNotFoundException ex) { log.info("Cache file " + file + " has not been created yet. Making new one."); return new CacheMap<>(numEntries, loadFactor, accessOrder, file); } catch (Exception ex) { log.info("Error reading cache file " + file + ". Making a new cache and NOT backing to file."); return new CacheMap<>(numEntries, loadFactor, accessOrder); } }