Example usage for com.google.common.cache RemovalListener RemovalListener

List of usage examples for com.google.common.cache RemovalListener RemovalListener

Introduction

In this page you can find the example usage for com.google.common.cache RemovalListener RemovalListener.

Prototype

RemovalListener

Source Link

Usage

From source file:org.objectfabric.GoogleCache.java

@SuppressWarnings("unchecked")
public GoogleCache(CacheBuilder builder) {
    super(true, new GoogleCacheBackend(builder.build().asMap()));

    builder.removalListener(new RemovalListener() {

        @Override/* w  w  w .j  a  v  a 2  s  . c om*/
        public void onRemoval(RemovalNotification notification) {
            onEviction(notification.getValue());
        }
    });
}

From source file:org.rub.nds.saml.samllib.verifier.SAMLIDCache.java

/**
 *
 *//*from   w  w  w  .ja v  a  2 s  .co  m*/
public static void initialize() {
    SAMLIDCache.cachedIDs = CacheBuilder.newBuilder().maximumSize(1000)
            .expireAfterWrite(SAMLIDCache.cacheDuration, TimeUnit.MINUTES)
            .removalListener(new RemovalListener<String, String>() {
                @Override
                public void onRemoval(RemovalNotification<String, String> rn) {
                    _log.debug("SAML Response ID " + rn.getKey() + " has been removed from the cache.");
                }
            }).build(new CacheLoader<String, String>() {
                @Override
                public String load(String key) {
                    return key;
                }
            });
}

From source file:rapture.kernel.cache.KernelCaches.java

private static Cache<RaptureURI, Optional<String>> setupObjectStorageCache() {
    return CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES)
            .removalListener(new RemovalListener<RaptureURI, Optional<String>>() {
                @Override//from  w w  w.  j a  v a  2  s  .  c o m
                public void onRemoval(RemovalNotification<RaptureURI, Optional<String>> notification) {
                    if (notification.getCause() != RemovalCause.REPLACED) {
                        if (log.isTraceEnabled())
                            log.trace("Removed " + notification.getKey() + " from local cache because "
                                    + notification.getCause());
                    }
                }
            }).build();
}

From source file:org.geogig.geoserver.config.RepositoryCache.java

public RepositoryCache(final RepositoryManager repoManager) {

    RemovalListener<String, Repository> disposingListener = new RemovalListener<String, Repository>() {
        @Override//from  ww  w  .  j av  a  2s. c  o  m
        public void onRemoval(RemovalNotification<String, Repository> notification) {
            String repoId = notification.getKey();
            Repository repository = notification.getValue();
            if (repository != null) {
                try {
                    URI location = repository.getLocation();
                    LOGGER.fine(format("Closing cached GeoGig repository instance %s",
                            location != null ? location : repoId));
                    repository.close();
                    LOGGER.finer(format("Closed cached GeoGig repository instance %s",
                            location != null ? location : repoId));
                } catch (RuntimeException e) {
                    LOGGER.log(Level.WARNING,
                            format("Error disposing GeoGig repository instance for id %s", repoId), e);
                }
            }
        }
    };

    final CacheLoader<String, Repository> loader = new CacheLoader<String, Repository>() {
        private final RepositoryManager manager = repoManager;

        @Override
        public Repository load(final String repoId) throws Exception {
            try {
                RepositoryInfo repoInfo = manager.get(repoId);
                URI repoLocation = repoInfo.getLocation();
                // RepositoryResolver.load returns an open repository or fails
                Repository repo = RepositoryResolver.load(repoLocation);
                checkState(repo.isOpen());

                return repo;
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, format("Error loading GeoGig repository instance for id %s", repoId),
                        e);
                throw e;
            }
        }
    };

    repoCache = CacheBuilder.newBuilder()//
            .softValues()//
            .expireAfterAccess(5, TimeUnit.MINUTES)//
            .removalListener(disposingListener)//
            .build(loader);
}

From source file:rub.nds.oidc.oidc_op.OIDCCache.java

/**
 *
 *//*from  w w  w  .  j a va2 s .  co  m*/
public static void initialize() {
    OIDCCache.cachedIDs = CacheBuilder.newBuilder().maximumSize(1000)
            .expireAfterWrite(OIDCCache.cacheDuration, TimeUnit.MINUTES)
            .removalListener(new RemovalListener<String, TokenCollection>() {
                @Override
                public void onRemoval(RemovalNotification<String, TokenCollection> rn) {
                    _log.debug("ID " + rn.getKey() + " has been removed from the cache.");
                }
            }).build(new CacheLoader<String, TokenCollection>() {
                @Override
                public TokenCollection load(String key) throws ExecutionException {
                    return getElement(key);
                }
            });
}

From source file:uk.ac.ed.inf.ace.utils.PreparedStatements.java

public PreparedStatements(final Database database) {
    super("PreparedStatements", CacheBuilder.newBuilder()
            .removalListener(new RemovalListener<Supplier<String>, PreparedStatement>() {
                @Override//from   w  ww.ja v a 2 s  . c  o  m
                public void onRemoval(RemovalNotification<Supplier<String>, PreparedStatement> notification) {
                    try {
                        notification.getValue().close();
                    } catch (SQLException exception) {
                        LOGGER.log(Level.SEVERE, "Failed to close a prepared statement.", exception);
                    }
                }
            }).concurrencyLevel(1).build(new CacheLoader<Supplier<String>, PreparedStatement>() {
                @Override
                public PreparedStatement load(Supplier<String> sqlSupplier) throws Exception {
                    database.open();
                    return database.prepareStatement(sqlSupplier.get());
                }
            }));
}

From source file:org.jevis.rest.IconCache.java

/**
 * Default constructor TODO: make the expire date configurable
 *//*from  w w  w  .  jav a 2s . c o  m*/
public IconCache() {
    _cache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(24, TimeUnit.HOURS)
            .removalListener(new RemovalListener<String, ClassIcon>() {

                @Override
                public void onRemoval(RemovalNotification<String, ClassIcon> notification) {
                    //                        System.out.println("Remove cached icon for: " + notification.getKey() + " because: " + notification.getCause());
                }
            }).build(new CacheLoader<String, ClassIcon>() {

                @Override
                public ClassIcon load(String key) throws Exception {
                    return new ClassIcon(key, null);
                }
            });
}

From source file:org.jeo.data.CachedRepository.java

public CachedRepository(DataRepository reg, final int cacheSize) {
    this.reg = reg;
    objCache = CacheBuilder.newBuilder().maximumSize(cacheSize)
            .removalListener(new RemovalListener<Pair<String, Class>, Object>() {
                @Override/* ww  w .jav a  2s.c om*/
                public void onRemoval(RemovalNotification<Pair<String, Class>, Object> n) {
                    Object obj = n.getValue();
                    if (obj instanceof Disposable) {
                        ((Disposable) obj).close();
                    }
                }
            }).build(new CacheLoader<Pair<String, Class>, Object>() {
                @Override
                public Object load(Pair<String, Class> key) throws Exception {
                    Object obj = CachedRepository.this.reg.get(key.first(), key.second());
                    if (obj instanceof Workspace) {
                        return new CachedWorkspace((Workspace) obj, cacheSize);
                    }
                    return obj;
                }
            });
}

From source file:de.rwhq.io.rm.CachedResourceManager.java

CachedResourceManager(final ResourceManager _rm, final int cacheSize) {
    checkNotNull(_rm);/*from  w  ww  .j ava  2 s  .  co  m*/
    checkArgument(cacheSize > 0, "cacheSize must be > 0");

    this.rm = _rm;
    this.cacheSize = cacheSize;
    this.cache = CacheBuilder.newBuilder().maximumSize(cacheSize)
            .removalListener(new RemovalListener<Integer, RawPage>() {
                @Override
                public void onRemoval(
                        final RemovalNotification<Integer, RawPage> integerRawPageRemovalNotification) {
                    final RawPage rawPage = integerRawPageRemovalNotification.getValue();
                    rawPage.sync();
                }
            }).build(new CacheLoader<Integer, RawPage>() {
                @Override
                public RawPage load(final Integer key) throws Exception {
                    return rm.getPage(key);
                }
            });

}

From source file:name.martingeisse.stackd.server.section.storage.FolderBasedSectionStorage.java

/**
 * Constructor./*  w w w.j  ava 2  s .  c  o  m*/
 * @param clusterSize the cluster-size of sections
 * @param storageFolder the folder that contains world storage files
 */
public FolderBasedSectionStorage(final ClusterSize clusterSize, final File storageFolder) {
    super(clusterSize);
    this.storageFolder = storageFolder;
    this.fileHandleCache = CacheBuilder.newBuilder().maximumSize(100)
            .removalListener(new RemovalListener<SectionId, RandomAccessFile>() {
                @Override
                public void onRemoval(RemovalNotification<SectionId, RandomAccessFile> notification) {
                    try {
                        notification.getValue().close();
                    } catch (IOException e) {
                    }
                }
            }).build(new CacheLoader<SectionId, RandomAccessFile>() {
                @Override
                public RandomAccessFile load(SectionId superSectionId) throws Exception {
                    File file = getSectionFile(superSectionId);

                    // create an empty file if there is none yet
                    if (!file.exists()) {
                        final FileOutputStream fileOutputStream = new FileOutputStream(file);
                        try {
                            final byte[] emptyToc = new byte[16 * 16 * 16 * 3 * 4];
                            fileOutputStream.write(emptyToc);
                        } finally {
                            fileOutputStream.close();
                        }
                    }

                    return new RandomAccessFile(file, "rw");
                }
            });
}