@Test(timeOut = 10_000) public void testEntriesAge() { final int entries = 1000; LongCache cache = new LongCache(entries, 16); int removals = 0; long totalAge = 0; double tempStdDev = 0; double tempAvg = 0; int i = 0; int largestDeletedTimestamp = 0; for (; i < entries * 10; ++i) { long removed = cache.set(random.nextLong(), i); if (removed > largestDeletedTimestamp) { largestDeletedTimestamp = (int) removed; } } long time = System.nanoTime(); for (; i < entries * 100; ++i) { long removed = cache.set(random.nextLong(), i); if (removed > largestDeletedTimestamp) { largestDeletedTimestamp = (int) removed; } int gap = i - largestDeletedTimestamp; removals++; totalAge += gap; double oldAvg = tempAvg; tempAvg += (gap - tempAvg) / removals; tempStdDev += (gap - oldAvg) * (gap - tempAvg); } long elapsed = System.nanoTime() - time; LOG.info("Elapsed (ms): " + (elapsed / (double) 1000)); double avgGap = totalAge / (double) removals; LOG.info("Avg gap: " + (tempAvg)); LOG.info("Std dev gap: " + Math.sqrt((tempStdDev / entries))); assertTrue(avgGap > entries * 0.6, "avgGap should be greater than entries * 0.6"); }
public long set(long key, long value) { final int index = index(key); int oldestIndex = 0; long oldestValue = Long.MAX_VALUE; for (int i = 0; i < associativity; ++i) { int currIndex = 2 * (index + i); if (cache[currIndex] == key) { oldestValue = 0; oldestIndex = currIndex; break; } if (cache[currIndex + 1] <= oldestValue) { oldestValue = cache[currIndex + 1]; oldestIndex = currIndex; } } cache[oldestIndex] = key; cache[oldestIndex + 1] = value; return oldestValue; }
LongCache { public long set(long key, long value) { final int index = index(key); int oldestIndex = 0; long oldestValue = Long.MAX_VALUE; for (int i = 0; i < associativity; ++i) { int currIndex = 2 * (index + i); if (cache[currIndex] == key) { oldestValue = 0; oldestIndex = currIndex; break; } if (cache[currIndex + 1] <= oldestValue) { oldestValue = cache[currIndex + 1]; oldestIndex = currIndex; } } cache[oldestIndex] = key; cache[oldestIndex + 1] = value; return oldestValue; } }
LongCache { public long set(long key, long value) { final int index = index(key); int oldestIndex = 0; long oldestValue = Long.MAX_VALUE; for (int i = 0; i < associativity; ++i) { int currIndex = 2 * (index + i); if (cache[currIndex] == key) { oldestValue = 0; oldestIndex = currIndex; break; } if (cache[currIndex + 1] <= oldestValue) { oldestValue = cache[currIndex + 1]; oldestIndex = currIndex; } } cache[oldestIndex] = key; cache[oldestIndex + 1] = value; return oldestValue; } LongCache(int size, int associativity); }
LongCache { public long set(long key, long value) { final int index = index(key); int oldestIndex = 0; long oldestValue = Long.MAX_VALUE; for (int i = 0; i < associativity; ++i) { int currIndex = 2 * (index + i); if (cache[currIndex] == key) { oldestValue = 0; oldestIndex = currIndex; break; } if (cache[currIndex + 1] <= oldestValue) { oldestValue = cache[currIndex + 1]; oldestIndex = currIndex; } } cache[oldestIndex] = key; cache[oldestIndex + 1] = value; return oldestValue; } LongCache(int size, int associativity); long set(long key, long value); long get(long key); }
LongCache { public long set(long key, long value) { final int index = index(key); int oldestIndex = 0; long oldestValue = Long.MAX_VALUE; for (int i = 0; i < associativity; ++i) { int currIndex = 2 * (index + i); if (cache[currIndex] == key) { oldestValue = 0; oldestIndex = currIndex; break; } if (cache[currIndex + 1] <= oldestValue) { oldestValue = cache[currIndex + 1]; oldestIndex = currIndex; } } cache[oldestIndex] = key; cache[oldestIndex + 1] = value; return oldestValue; } LongCache(int size, int associativity); long set(long key, long value); long get(long key);  }