This project has retired. For details please refer to its Attic page.
Source code
001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.io.network.naming;
020
021import com.google.common.cache.CacheBuilder;
022import org.apache.reef.io.network.Cache;
023import org.apache.reef.wake.Identifier;
024
025import java.net.InetSocketAddress;
026import java.util.concurrent.Callable;
027import java.util.concurrent.ExecutionException;
028import java.util.concurrent.TimeUnit;
029
030/**
031 * Naming cache implementation
032 */
033public class NameCache implements Cache<Identifier, InetSocketAddress> {
034
035  private final com.google.common.cache.Cache<Identifier, InetSocketAddress> cache;
036
037  /**
038   * Constructs a naming cache
039   *
040   * @param timeout a cache entry timeout after access
041   */
042  public NameCache(long timeout) {
043    cache = CacheBuilder.newBuilder()
044        .expireAfterWrite(timeout, TimeUnit.MILLISECONDS)
045        .build();
046  }
047
048  /**
049   * Gets an address for an identifier
050   *
051   * @param key          an identifier
052   * @param valueFetcher a callable to load a value for the corresponding identifier
053   * @return an Internet socket address
054   * @throws ExecutionException
055   */
056  @Override
057  public InetSocketAddress get(Identifier key,
058                               Callable<InetSocketAddress> valueFetcher) throws ExecutionException {
059    return cache.get(key, valueFetcher);
060  }
061
062  /**
063   * Invalidates the entry for an identifier
064   *
065   * @param key an identifier
066   */
067  @Override
068  public void invalidate(Identifier key) {
069    cache.invalidate(key);
070  }
071
072}