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

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

package io.sirix.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;
import io.sirix.page.IndirectPage;
import io.sirix.page.PageReference;
import io.sirix.page.interfaces.Page;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public final class PageCache implements Cache {

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

  public PageCache(final int maxSize) {
    RemovalListener removalListener = (PageReference key, Page value, RemovalCause cause) -> {
      key.setPage(null);
      //      if (value instanceof KeyValueLeafPage keyValueLeafPage) {
      //        keyValueLeafPage.clearPage();
      //      }
    };

    pageCache = Caffeine.newBuilder()
                        .maximumSize(maxSize)
                        .expireAfterAccess(5, TimeUnit.MINUTES)
                        .scheduler(scheduler)
                        .removalListener(removalListener)
                        .build();
  }

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

  @Override
  public Page get(PageReference key) {
    var page = pageCache.getIfPresent(key);

    if (page instanceof IndirectPage indirectPage) {
      page = new IndirectPage(indirectPage);
    }

    return page;
  }

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

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

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

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

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

  @Override
  public void close() {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy