@Test public void testPutReturnValue() { cache.put(1, "apple"); assertNotNull(cache.put(1, "banana")); Clock.setTime(3000); assertNull(cache.put(1, "mango")); }
@Test public void testSize() throws Exception { assertEquals(0, cache.size()); cache.put(1, "apple"); assertEquals(1, cache.size()); Clock.setTime(3000); assertEquals(0, cache.size()); }
@Test public void testExpiry() throws Exception { cache.put(1, "apple"); assertEquals("apple", cache.get(1)); assertFalse(cache.isEmpty()); Clock.setTime(3000); assertNull(cache.get(1)); assertTrue(cache.isEmpty()); }
@Test public void testRemove() throws Exception { assertNull(cache.remove(new Integer(1))); cache.put(new Integer(1), "apple"); assertEquals("apple", cache.remove(new Integer(1))); assertNull(cache.get(new Integer(1))); assertEquals(0, cache.size()); }
@SuppressWarnings("unchecked") public Object getInstance(Line line, Class clas) throws InstantiationException, IllegalAccessException { Fixture annotation = (Fixture) clas.getAnnotation(Fixture.class); Lifespan lifespan = annotation.lifespan(); Object instance; if (lifespan == Lifespan.FULL_SUITE) { instance = fullSuiteCache.getInstance(clas); } else if (lifespan == Lifespan.FEATURE) { Feature feature = line.getScenario().getFeature(); instance = featureCache.cache(clas, feature); } else if (lifespan == Lifespan.SCENARIO) { Scenario scenario = line.getScenario(); instance = scenarioCache.cache(clas, scenario); } else { LOG.info("create new instance of '{}'", clas); instance = clas.newInstance(); } return instance; }
public byte[] get(Long key, byte[] data) throws IOException { byte[] value = cache.get(key); if (value != null) { return value; } value = overwriteBackup.get(key); if (value != null) { return value; } value = f.readByHash(key, data); return value; }
public void put(Long key, byte[] data) throws IOException { Integer index = f.hashToIndex(key); if (index != null) { return; } index = f.getIndex(); f.incrementIndex(); Long oldHash = f.indexToHash(index); byte[] oldData = new byte[(int) size]; oldData = f.readByIndex(index, oldData); if (oldData != null) { overwriteBackup.put(oldHash, oldData); overwriteQueue.add(oldHash); } f.write(index, key, data); if (!cache.contains(key)) { byte[] dataCopy = new byte[data.length]; System.arraycopy(data, 0, dataCopy, 0, data.length); cache.put(key, dataCopy); } }
private CacheElement put(long address) throws IOException { /** get a CacheElement from the stack object pool */ CacheElement c = map.pop(); /** read the element from the device */ c.read(address); /** and insert the element into the LinkedHashMap */ map.put(c); /** * stack "must" contains at least one entry the placeholder ... so let it throw an exception if * this is false */ CacheElement e = map.peek(); // if an element was discarded from the LRU cache // now we can free it ... this will send the element // to storage if is marked as dirty if (!e.isFree()) e.free(); return c; }
/** * Reads the object from the storage, returns null if the object isn't there * * @param name name of the store * @return object stored under that name */ public Object readObject(String name) { name = fixFileName(name); Object o = cache.get(name); if (o != null) { return o; } DataInputStream d = null; try { if (!exists(name)) { return null; } d = new DataInputStream(createInputStream(name)); o = Util.readObject(d); d.close(); cache.put(name, o); return o; } catch (Exception err) { err.printStackTrace(); Util.getImplementation().cleanup(d); return null; } }
public static void main(String args[]) { CacheMap<Integer, String> myCache = new CacheMap<Integer, String>(5); myCache.put(1, "a"); myCache.put(2, "b"); myCache.put(3, "c"); myCache.put(4, "d"); myCache.put(5, "e"); myCache.put(3, "f"); myCache.put(6, "h"); System.out.println(myCache.get(1)); System.out.println(myCache.get(1)); System.out.println(myCache.get(1)); for (Map.Entry<Integer, String> entry : myCache.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } }
/** * Writes the given object to storage assuming it is an externalizable type or one of the * supported types * * @param name store name * @param o object to store * @return true for success, false for failue */ public boolean writeObject(String name, Object o) { name = fixFileName(name); cache.put(name, o); DataOutputStream d = null; try { d = new DataOutputStream(createOutputStream(name)); Util.writeObject(o, d); d.close(); return true; } catch (Exception err) { err.printStackTrace(); Util.getImplementation().deleteStorageFile(name); Util.getImplementation().cleanup(d); return false; } }
private CacheElement get(long address) throws IOException { CacheElement c = map.get(address); access++; // if the cache contains the element just return it, we have a cache hit // this will update the LRU order: the LinkedHashMap will make it the // newest // // the cache element cannot be null so we can avoid to call // containsKey(); if (c != null) hit++; // otherwise put a new element inside the cache // possibly flushing and discarding the eldest element else c = put(address); return c; }
@Test public void testPartialExpiry() throws Exception { // Add an apple, it will expire at 2000 // System.out.println(cache.getTimeToLive()); cache.put(1, "apple"); Clock.setTime(1500); // System.out.println(cache.getTimeToLive()); // Add an orange, it will expire at 2500 cache.put(2, "orange"); assertEquals("apple", cache.get(1)); assertEquals("orange", cache.get(2)); assertEquals(2, cache.size()); // Set time to 2300 and check that only the apple has disappeared Clock.setTime(2300); assertNull(cache.get(1)); assertEquals("orange", cache.get(2)); assertEquals(1, cache.size()); }
public int getCacheSize() { return map.getCacheSize(); }
public String flushOrder() { return map.flushOrder(); }
public void flush() throws IOException { for (CacheElement c : map.values()) { c.flush(); } }
public void flush(long address) throws IOException { CacheElement c = map.get(address); if (c != null) c.flush(); }
public byte[] removeOverwriteBackup(Long key) { cache.remove(key); return overwriteBackup.remove(key); }
/** * Storage is cached for faster access, however this might cause a problem with refreshing objects * since they are not cloned. Clearing the cache allows to actually reload from the storage file. */ public void clearCache() { cache.clearAllCache(); }
@Override protected boolean isSelfExists(String key) { return memoryMap.containsKey(key); }
/** * Indicates the caching size, storage can be pretty slow * * @param size size in elements (not kb!) */ public void setHardCacheSize(int size) { cache.setCacheSize(size); }
@Override protected String selfGet(String key) { return memoryMap.get(key); }
@Before public void setUp() throws Exception { Clock.setTime(1000); cache = new CacheMapImpl<>(); cache.setTimeToLive(TIME_TO_LIVE); }
/** Deletes all the files in the application storage */ public void clearStorage() { Util.getImplementation().clearStorage(); cache.clearAllCache(); }
/** * Deletes the given file name from the storage * * @param name the name of the storage file */ public void deleteStorageFile(String name) { name = fixFileName(name); Util.getImplementation().deleteStorageFile(name); cache.delete(name); }
public int usedEntries() { return map.usedEntries(); }
public int freeEntries() { return map.freeEntries(); }
@Override protected void selfPut(String key, String value, long expiredTime) { memoryMap.put(key, value, expiredTime); }
@Test public void testContainsKeyAndContainsValue() { assertFalse(cache.containsKey(1)); assertFalse(cache.containsValue("apple")); assertFalse(cache.containsKey(2)); assertFalse(cache.containsValue("orange")); cache.put(1, "apple"); assertTrue(cache.containsKey(1)); assertTrue(cache.containsValue("apple")); assertFalse(cache.containsKey(2)); assertFalse(cache.containsValue("orange")); Clock.setTime(3000); assertFalse(cache.containsKey(1)); assertFalse(cache.containsValue("apple")); assertFalse(cache.containsKey(2)); assertFalse(cache.containsValue("orange")); }
@Override protected Long selfRemove(String key) { return memoryMap.remove(key) != null ? 1L : 0L; }