Пример #1
0
  @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());
  }
Пример #2
0
  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());
    }
  }
Пример #3
0
  @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());
  }
Пример #4
0
  @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());
  }
Пример #5
0
  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;
  }
Пример #6
0
  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;
  }
Пример #7
0
 /**
  * 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;
   }
 }
Пример #8
0
 public void flush(long address) throws IOException {
   CacheElement c = map.get(address);
   if (c != null) c.flush();
 }
Пример #9
0
 @Override
 protected String selfGet(String key) {
   return memoryMap.get(key);
 }