@Test public void testExpireVsEvict() throws Exception { char[] chars = new char[900]; for (int i = 0; i < chars.length; i++) chars[i] = (char) ('A' + i % 26); String v = new String(chars); TestTicker ticker = new TestTicker(); try (OHCache<Integer, String> cache = OHCacheBuilder.<Integer, String>newBuilder() .keySerializer(TestUtils.intSerializer) .valueSerializer(TestUtils.stringSerializer) .capacity(1024 * 1024) .segmentCount(1) .ticker(ticker) .timeouts(true) .build()) { long expireAt = ticker.currentTimeMillis() + 250; int i; for (i = 0; cache.freeCapacity() > cache.capacity() / 2 + 950; i++) cache.put(i, v, expireAt); int k = i; assertEquals(((OHCacheLinkedImpl)cache).usedTimeouts(), k); for (int n = 0; n < k; n++, i++) cache.put(i, v); long remain = expireAt - ticker.currentTimeMillis(); assertTrue(remain >= 100, "Sorry, your machine is a bit too slow..."); assertEquals(cache.stats().getExpireCount(), 0, "wrong expired entries count"); assertEquals(cache.stats().getEvictionCount(), 0L, "cleanup triggered"); ticker.addMillis(remain); assertEquals(((OHCacheLinkedImpl)cache).usedTimeouts(), k); for (int n = 0; n < k; n++, i++) { cache.put(i, v); if ((n % 10000) == 0) assertEquals(cache.stats().getEvictionCount(), 0L, "cleanup triggered"); } assertEquals(cache.stats().getExpireCount(), k, "wrong expired entries count"); assertEquals(cache.stats().getEvictionCount(), 0L, "cleanup triggered"); assertEquals(((OHCacheLinkedImpl)cache).usedTimeouts(), 0); } }
Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } Timeouts(Ticker ticker, int slots, long precision); }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } Timeouts(Ticker ticker, int slots, long precision);  }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } Timeouts(Ticker ticker, int slots, long precision);   }