@Test public void testGetWithLoader() throws Exception { ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); try (OHCache<Integer, String> cache = OHCacheBuilder.<Integer, String>newBuilder() .keySerializer(TestUtils.intSerializer) .valueSerializer(TestUtils.stringSerializer) .executorService(executorService) .timeouts(true) .build()) { Future<String> f = cache.getWithLoaderAsync(1, new CacheLoader<Integer, String>() { public String load(Integer key) throws Exception { return "one"; } }, System.currentTimeMillis() - 1); assertNull(f.get()); assertNull(cache.get(1)); f = cache.getWithLoaderAsync(2, new CacheLoader<Integer, String>() { public String load(Integer key) throws Exception { Thread.sleep(50); return "two"; } }, System.currentTimeMillis() + 100); Thread.sleep(60); assertEquals(f.get(0, TimeUnit.MILLISECONDS), "two"); assertEquals(cache.get(2), "two"); Thread.sleep(60); assertNull(cache.get(2)); f = cache.getWithLoaderAsync(3, new CacheLoader<Integer, String>() { public String load(Integer key) throws Exception { Thread.sleep(5); return "three"; } }, System.currentTimeMillis() + 2); assertNull(f.get()); assertNull(cache.get(3)); } finally { executorService.shutdown(); } }
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);   }