All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sirix.cache.PageCache Maven / Gradle / Ivy

package org.sirix.cache;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.sirix.page.PageReference;
import org.sirix.page.interfaces.Page;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;

public final class PageCache implements Cache {

  private final com.github.benmanes.caffeine.cache.Cache mPageCache;

  public PageCache() {
    RemovalListener removalListener =
        (PageReference key, Page value, RemovalCause cause) -> key.setPage(null);

    mPageCache = Caffeine.newBuilder()
                         .maximumSize(1000)
                         .expireAfterWrite(5000, TimeUnit.SECONDS)
                         .expireAfterAccess(5000, TimeUnit.SECONDS)
                         .removalListener(removalListener)
                         .build();
  }

  @Override
  public void clear() {
    mPageCache.invalidateAll();
  }

  @Override
  public Page get(PageReference key) {
    return mPageCache.getIfPresent(key);
  }

  @Override
  public void put(PageReference key, Page value) {
    mPageCache.put(key, value);
  }

  @Override
  public void putAll(Map map) {
    mPageCache.putAll(map);
  }

  @Override
  public void toSecondCache() {
    throw new UnsupportedOperationException();
  }

  @Override
  public Map getAll(Iterable keys) {
    return mPageCache.getAllPresent(keys);
  }

  @Override
  public void remove(PageReference key) {
    mPageCache.invalidate(key);
  }

  @Override
  public void close() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy