@Test public void testFlushUpdatesAccessStats() throws CacheAccessException { final TestTimeSource timeSource = new TestTimeSource(); final Expiry<Object, Object> expiry = Expirations.timeToIdleExpiration(new Duration(15L, TimeUnit.MILLISECONDS)); final AbstractOffHeapStore<String, String> store = createAndInitStore(timeSource, expiry); try { final String key = "foo"; final String value = "bar"; store.put(key, value); final Store.ValueHolder<String> firstValueHolder = store.getAndFault(key); store.put(key, value); final Store.ValueHolder<String> secondValueHolder = store.getAndFault(key); timeSource.advanceTime(10); ((AbstractValueHolder) firstValueHolder) .accessed(timeSource.getTimeMillis(), expiry.getExpiryForAccess(key, value)); timeSource.advanceTime(10); ((AbstractValueHolder) secondValueHolder) .accessed(timeSource.getTimeMillis(), expiry.getExpiryForAccess(key, value)); assertThat(store.flush(key, new DelegatingValueHolder<String>(firstValueHolder)), is(false)); assertThat(store.flush(key, new DelegatingValueHolder<String>(secondValueHolder)), is(true)); timeSource.advanceTime(10); // this should NOT affect assertThat( store.getAndFault(key).lastAccessTime(TimeUnit.MILLISECONDS), is(secondValueHolder.creationTime(TimeUnit.MILLISECONDS) + 20)); } finally { destroyStore(store); } }
private OffHeapValueHolder<V> newCreateValueHolder(K key, V value, long now) { Duration duration = expiry.getExpiryForCreation(key, value); if (Duration.ZERO.equals(duration)) { return null; } if (duration.isForever()) { return new OffHeapValueHolder<V>(value, now, OffHeapValueHolder.NO_EXPIRE); } else { return new OffHeapValueHolder<V>(value, now, safeExpireTime(now, duration)); } }
private void setAccessTimeAndExpiry(K key, OffHeapValueHolder<V> valueHolder, long now) { valueHolder.setLastAccessTime(now, OffHeapValueHolder.TIME_UNIT); Duration duration = expiry.getExpiryForAccess(key, valueHolder.value()); if (duration != null) { if (duration.isForever()) { valueHolder.setExpirationTime(ValueHolder.NO_EXPIRE, null); } else { valueHolder.setExpirationTime(safeExpireTime(now, duration), OffHeapValueHolder.TIME_UNIT); } } valueHolder.writeBack(); }
private OffHeapValueHolder<V> newUpdatedValueHolder( K key, V value, OffHeapValueHolder<V> existing, long now) { Duration duration = expiry.getExpiryForUpdate(key, existing.value(), value); if (Duration.ZERO.equals(duration)) { return null; } if (duration == null) { return new OffHeapValueHolder<V>( value, now, existing.expirationTime(OffHeapValueHolder.TIME_UNIT)); } else if (duration.isForever()) { return new OffHeapValueHolder<V>(value, now, OffHeapValueHolder.NO_EXPIRE); } else { return new OffHeapValueHolder<V>(value, now, safeExpireTime(now, duration)); } }