Example usage for com.google.common.cache Cache getIfPresent

List of usage examples for com.google.common.cache Cache getIfPresent

Introduction

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

Prototype

@Nullable
V getIfPresent(Object key);

Source Link

Document

Returns the value associated with key in this cache, or null if there is no cached value for key .

Usage

From source file:org.eclipse.osee.jaxrs.client.internal.JaxRsClientRuntime.java

private static OAuthFactory newOAuthFactory() {
    return new OAuthFactory() {

        @Override/*from w  ww  .  ja  v  a 2s .com*/
        public OAuth2ClientRequestFilter newOAuthClientFilter(String username, String password, String clientId,
                String clientSecret, String authorizeUri, String tokenUri, String tokenValidationUri) {
            OwnerCredentials owner = newOwner(username, password);
            OAuthClientUtils.Consumer client = new OAuthClientUtils.Consumer(clientId, clientSecret);
            OAuth2Transport transport = new OAuth2Transport();
            OAuth2Flows flowManager = new OAuth2Flows(transport, owner, client, authorizeUri, tokenUri,
                    tokenValidationUri);
            OAuth2Serializer serializer = new OAuth2Serializer();
            return new OAuth2ClientRequestFilter(flowManager, serializer);
        }

        @Override
        public ClientAccessTokenCache newClientAccessTokenCache(int cacheMaxSize,
                long cacheEvictTimeoutMillis) {
            final Cache<URI, ClientAccessToken> cache = newCache(cacheMaxSize, cacheEvictTimeoutMillis);
            return new ClientAccessTokenCache() {

                @Override
                public ClientAccessToken get(URI key) {
                    return cache.getIfPresent(key);
                }

                @Override
                public void store(URI key, ClientAccessToken value) {
                    cache.put(key, value);
                }
            };
        }

    };
}

From source file:app.data.parse.WebPageUtil.java

public static WebPageInfo parse(String url, Cache<String, WebPageInfo> urlInfoCache) throws IOException {
    String original = url;//from   w  w w  . j a v a  2  s . co m

    // hit toutiao.io
    // fixme http://toutiao.io/shares/640539/url
    if (original.startsWith("https://toutiao.io/posts/")) {
        original = original.replace("/posts/", "/k/");
    }

    // check cache
    WebPageInfo info = urlInfoCache != null ? urlInfoCache.getIfPresent(original) : null;
    if (info != null) {
        return info;
    } else {
        info = new WebPageInfo();
        info.url = original;
    }

    // attach url
    Document doc = requestUrl(info.url);
    info.url = doc.baseUri(); // or doc.location()

    // hit gold.xitu.io
    if (info.url.startsWith("http://gold.xitu.io/entry/")) {
        Elements origin = doc.select("div[class=ellipsis]");
        Elements originLink = origin.select("a[class=share-link]");
        info.url = originLink.attr("href");

        // reconnect
        doc = requestUrl(info.url);
        info.url = doc.baseUri(); // or doc.location()
    }

    info.url = smartUri(info.url);

    // get title
    Elements metaTitle = doc.select("meta[property=og:title]");
    if (metaTitle != null) {
        info.title = metaTitle.attr("content");
    }
    if (StringUtils.isEmpty(info.title)) {
        metaTitle = doc.select("meta[property=twitter:title]");
        if (metaTitle != null) {
            info.title = metaTitle.attr("content");
        }
        info.title = StringUtils.isEmpty(info.title) ? doc.title() : info.title;
    }

    // get desc
    Elements metaDesc = doc.select("meta[property=og:description]");
    if (metaDesc != null) {
        info.description = metaDesc.attr("content");
    }
    if (StringUtils.isEmpty(info.description)) {
        metaDesc = doc.select("meta[property=twitter:description]");
        if (metaDesc != null) {
            info.description = metaDesc.attr("content");
        }
        if (StringUtils.isEmpty(info.description)) {
            metaDesc = doc.select("meta[name=description]");
            if (metaDesc != null) {
                info.description = metaDesc.attr("content");
            }
            if (StringUtils.isEmpty(info.description)) {
                metaDesc = doc.body().select("p");
                if (metaDesc != null) {
                    for (Element element : metaDesc) {
                        info.description = element.text();
                        if (info.description != null && info.description.length() >= 20) {
                            break;
                        }
                    }
                }
            }
        }
    }
    info.description = ellipsis(info.description, 140, "...");

    // cache info
    if (urlInfoCache != null) {
        urlInfoCache.put(original, info);
    }
    return info;
}

From source file:net.minecraftforge.common.ForgeChunkManager.java

@SuppressWarnings("unchecked")
public static Chunk fetchDormantChunk(long coords, World world) {
    Cache<Long, Chunk> cache = dormantChunkCache.get(world);
    if (cache == null) {
        return null;
    }/*from  ww w.  j av a2  s.co  m*/
    Chunk chunk = cache.getIfPresent(coords);
    if (chunk != null) {
        for (ClassInheritanceMultiMap eList : chunk.getEntityLists()) {
            Iterator<Entity> itr = (Iterator<Entity>) eList.iterator();
            while (itr.hasNext()) {
                (itr.next()).resetEntityId();
            }
        }
    }
    return chunk;
}

From source file:org.geogit.di.caching.ObjectDatabaseGetCacheInterceptor.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    final ObjectId oid = (ObjectId) invocation.getArguments()[0];

    final Cache<ObjectId, RevObject> cache = cacheProvider.get().get();

    Object object = cache.getIfPresent(oid);
    if (object == null) {
        object = invocation.proceed();/*from   ww w . jav  a  2s . co m*/
        if (isCacheable(object)) {
            cache.put(oid, (RevObject) object);
        }
    }
    return object;
}

From source file:com.watchrabbit.executor.service.CacheServiceImpl.java

private <V> Callable<V> wrap(Callable<V> callable, Cache<String, Object> cache, String key) {
    return () -> {
        try {/*from  w  ww  .j av  a 2 s  .  com*/
            V value = (V) cache.getIfPresent(key);
            if (value == null) {
                value = callable.call();
                if (value != null) {
                    cache.put(key, value);
                }
            }
            return value;
        } catch (ExecutionException ex) {
            LOGGER.debug("Error on cache loading", ex);
            throw ex;
        }
    };
}

From source file:org.neo4j.nlp.impl.cache.RelationshipCache.java

private List<Long> getLongs(Long start, GraphDatabaseService db, GraphManager graphManager) {
    List<Long> relList;

    Cache<Long, List<Long>> relCache = getRelationshipCache();
    relList = relCache.getIfPresent(start);
    Node startNode;/* w  ww .  ja  v a 2  s. c om*/
    try (Transaction tx = db.beginTx()) {
        String pattern = (String) NodeManager.getNodeAsMap(start, db).get("pattern");
        startNode = graphManager.getOrCreateNode(pattern, db);
        tx.success();
    }

    if (relList == null)
        relList = getLongs(start, db, null, startNode);

    return relList;
}

From source file:org.sfs.elasticsearch.containerkey.ListReEncryptableContainerKeys.java

protected Observable<PersistentAccount> loadAccount(VertxContext<Server> vertxContext, String accountId,
        Cache<String, PersistentAccount> accountCache) {
    PersistentAccount persistentAccount = accountCache.getIfPresent(accountId);
    if (persistentAccount != null) {
        return just(persistentAccount);
    } else {/*from w w  w. j  a  v a2s . c o  m*/
        return just(accountId).flatMap(new LoadAccount(vertxContext)).map(Optional::get)
                .map(persistentAccount1 -> {
                    accountCache.put(accountId, persistentAccount1);
                    return persistentAccount1;
                });
    }
}

From source file:org.elasticsearch.index.cache.field.data.support.AbstractConcurrentMapFieldDataCache.java

@Override
public long sizeInBytes(String fieldName) {
    long sizeInBytes = 0;
    for (Cache<String, FieldData> map : cache.values()) {
        FieldData fieldData = map.getIfPresent(fieldName);
        if (fieldData != null) {
            sizeInBytes += fieldData.sizeInBytes();
        }/*from   w w  w  . j  ava2  s.c o m*/
    }
    return sizeInBytes;
}

From source file:tech.beshu.ror.acl.definitions.DefinitionsFactory.java

private <T> T getOrCreate(NamedSettings settings, Cache<String, T> cache, Supplier<T> creator) {
    T cached = cache.getIfPresent(settings.getName());
    if (cached != null)
        return cached;

    T created = creator.get();//from w  ww.j av a2  s . co  m
    cache.put(settings.getName(), created);
    return created;
}

From source file:org.sfs.elasticsearch.containerkey.ListReEncryptableContainerKeys.java

protected Observable<PersistentContainer> loadContainer(VertxContext<Server> vertxContext,
        PersistentAccount persistentAccount, String containerId,
        Cache<String, PersistentContainer> containerCache) {
    PersistentContainer persistentContainer = containerCache.getIfPresent(containerId);
    if (persistentContainer != null) {
        return just(persistentContainer);
    } else {//  ww w.  j a  v a 2s  . com
        return just(containerId).flatMap(new LoadContainer(vertxContext, persistentAccount)).map(Optional::get)
                .map(persistentContainer1 -> {
                    containerCache.put(containerId, persistentContainer1);
                    return persistentContainer1;
                });
    }
}