Example usage for com.google.common.cache LoadingCache putAll

List of usage examples for com.google.common.cache LoadingCache putAll

Introduction

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

Prototype

void putAll(Map<? extends K, ? extends V> m);

Source Link

Document

Copies all of the mappings from the specified map to the cache.

Usage

From source file:com.android.builder.shrinker.JavaSerializationShrinkerGraph.java

@SuppressWarnings("unchecked") // readObject() returns an Object, we need to cast it.
@Override/*from   w ww.  ja  va2 s.c  om*/
public void loadState() throws IOException {
    ObjectInputStream stream = new ObjectInputStream(
            new BufferedInputStream(new FileInputStream(getStateFile())));

    try {
        mClasses = (ConcurrentMap<String, ClassInfo>) stream.readObject();
        mMembers = (SetMultimap<String, String>) stream.readObject();
        mDependencies = (SetMultimap<String, Dependency<String>>) stream.readObject();
        Map<ShrinkType, Map<String, Counter>> countersMap = (Map<ShrinkType, Map<String, Counter>>) stream
                .readObject();

        for (Map.Entry<ShrinkType, Map<String, Counter>> entry : countersMap.entrySet()) {
            LoadingCache<String, Counter> cache = mReferenceCounters.get(entry.getKey());
            cache.invalidateAll();
            cache.putAll(entry.getValue());
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        stream.close();
    }
}

From source file:org.apache.cassandra.auth.AuthCache.java

private LoadingCache<K, V> initCache(LoadingCache<K, V> existing) {
    if (!enableCache.get())
        return null;

    if (getValidity() <= 0)
        return null;

    logger.info("(Re)initializing {} (validity period/update interval/max entries) ({}/{}/{})", name,
            getValidity(), getUpdateInterval(), getMaxEntries());

    LoadingCache<K, V> newcache = CacheBuilder.newBuilder()
            .refreshAfterWrite(getUpdateInterval(), TimeUnit.MILLISECONDS)
            .expireAfterWrite(getValidity(), TimeUnit.MILLISECONDS).maximumSize(getMaxEntries())
            .build(new CacheLoader<K, V>() {
                public V load(K k) {
                    return loadFunction.apply(k);
                }//from w w  w . java 2s . c  o m

                public ListenableFuture<V> reload(final K k, final V oldV) {
                    ListenableFutureTask<V> task = ListenableFutureTask.create(() -> {
                        try {
                            return loadFunction.apply(k);
                        } catch (Exception e) {
                            logger.trace("Error performing async refresh of auth data in {}", name, e);
                            throw e;
                        }
                    });
                    cacheRefreshExecutor.execute(task);
                    return task;
                }
            });
    if (existing != null)
        newcache.putAll(existing.asMap());
    return newcache;
}

From source file:org.apache.cassandra.auth.RolesCache.java

private LoadingCache<RoleResource, Set<RoleResource>> initCache(
        LoadingCache<RoleResource, Set<RoleResource>> existing) {
    if (DatabaseDescriptor.getAuthenticator() instanceof AllowAllAuthenticator)
        return null;

    if (DatabaseDescriptor.getRolesValidity() <= 0)
        return null;

    LoadingCache<RoleResource, Set<RoleResource>> newcache = CacheBuilder.newBuilder()
            .refreshAfterWrite(DatabaseDescriptor.getRolesUpdateInterval(), TimeUnit.MILLISECONDS)
            .expireAfterWrite(DatabaseDescriptor.getRolesValidity(), TimeUnit.MILLISECONDS)
            .maximumSize(DatabaseDescriptor.getRolesCacheMaxEntries())
            .build(new CacheLoader<RoleResource, Set<RoleResource>>() {
                public Set<RoleResource> load(RoleResource primaryRole) {
                    return roleManager.getRoles(primaryRole, true);
                }/*from  w w w. j av  a2s.  co  m*/

                public ListenableFuture<Set<RoleResource>> reload(final RoleResource primaryRole,
                        final Set<RoleResource> oldValue) {
                    ListenableFutureTask<Set<RoleResource>> task;
                    task = ListenableFutureTask.create(new Callable<Set<RoleResource>>() {
                        public Set<RoleResource> call() throws Exception {
                            try {
                                return roleManager.getRoles(primaryRole, true);
                            } catch (Exception e) {
                                logger.trace("Error performing async refresh of user roles", e);
                                throw e;
                            }
                        }
                    });
                    cacheRefreshExecutor.execute(task);
                    return task;
                }
            });
    if (existing != null)
        newcache.putAll(existing.asMap());
    return newcache;
}

From source file:org.apache.cassandra.auth.PermissionsCache.java

private LoadingCache<Pair<AuthenticatedUser, IResource>, Set<Permission>> initCache(
        LoadingCache<Pair<AuthenticatedUser, IResource>, Set<Permission>> existing) {
    if (authorizer instanceof AllowAllAuthorizer)
        return null;

    if (DatabaseDescriptor.getPermissionsValidity() <= 0)
        return null;

    LoadingCache<Pair<AuthenticatedUser, IResource>, Set<Permission>> newcache = CacheBuilder.newBuilder()
            .refreshAfterWrite(DatabaseDescriptor.getPermissionsUpdateInterval(), TimeUnit.MILLISECONDS)
            .expireAfterWrite(DatabaseDescriptor.getPermissionsValidity(), TimeUnit.MILLISECONDS)
            .maximumSize(DatabaseDescriptor.getPermissionsCacheMaxEntries())
            .build(new CacheLoader<Pair<AuthenticatedUser, IResource>, Set<Permission>>() {
                public Set<Permission> load(Pair<AuthenticatedUser, IResource> userResource) {
                    return authorizer.authorize(userResource.left, userResource.right);
                }// ww  w  .  ja va  2  s. com

                public ListenableFuture<Set<Permission>> reload(
                        final Pair<AuthenticatedUser, IResource> userResource, final Set<Permission> oldValue) {
                    ListenableFutureTask<Set<Permission>> task = ListenableFutureTask
                            .create(new Callable<Set<Permission>>() {
                                public Set<Permission> call() throws Exception {
                                    try {
                                        return authorizer.authorize(userResource.left, userResource.right);
                                    } catch (Exception e) {
                                        logger.trace("Error performing async refresh of user permissions", e);
                                        throw e;
                                    }
                                }
                            });
                    cacheRefreshExecutor.execute(task);
                    return task;
                }
            });
    if (existing != null)
        newcache.putAll(existing.asMap());
    return newcache;
}