OffHeapCache extends AbstractSearchableCache<K, V> { protected void cleanBuffers(final float bufferCleanerThreshold) { final Set<Entry<K, Pointer>> pointers = pointerMap.entrySet(); final Map<OffHeapByteBuffer, Float> buffers = new HashMap<>(); final Set<Integer> buffersToBeCleaned = new HashSet<Integer>(); for (final Entry<K, Pointer> pointerEntry : pointers) { Float ratio = buffers.get(pointerEntry.getValue().getOffHeapByteBuffer()); if (ratio == null) { ratio = getDirtyRatio(pointerEntry.getValue()); buffers.put(pointerEntry.getValue().getOffHeapByteBuffer(), ratio); } if (ratio - bufferCleanerThreshold > DELTA) { buffersToBeCleaned.add(pointerEntry.getValue().getOffHeapByteBuffer().getIndex()); writeLock(pointerEntry.getKey()); try { final byte[] payload = bufferStore.retrieve(pointerEntry.getValue()); final Pointer newPointer = bufferStore .store(payload, pointerEntry.getValue().getExpiry()); pointerMap.put(pointerEntry.getKey(), newPointer); } finally { writeUnlock(pointerEntry.getKey()); } } } for (final int bufferIndex : buffersToBeCleaned) { bufferStore.free(bufferIndex); } } OffHeapCache(final String name, final CacheLoader<K, V> cacheLoader,
      final EvictionListener<K, V> evictionListener,
      final IndexHandler<K, V> indexHandler, final OffHeapByteBufferStore byteBufferStore,
      final Serializer<V> serializer,
      final long bufferCleanerPeriod, final float bufferCleanerThreshold,
      final int concurrencyLevel,
      final long evictionPeriod); void put(K key, V value); @Override void put(final K key, final V value, final TimeUnit timeUnit, final long duration); V get(K key); V invalidate(K key); boolean contains(K key); void clear(); @Override long size(); @Override CacheStats stats(); static final long DEFAULT_BUFFER_CLEANER_PERIOD; static final long DEFAULT_EVICTION_PERIOD; static final float DEFAULT_BUFFER_CLEANER_THRESHOLD; static final int DEFAULT_CONCURRENCY_LEVEL; }
@Test @SuppressWarnings("unchecked") public void cleanBuffers() { cache.put("a", "b"); cache.put("a", "ab"); cache.put("c", "d"); cache.put("c", "cd"); cache.cleanBuffers(0.01f); verify(bufferStore, times(2)).retrieve(any()); }