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 org.apache.reef.util.cache.Cache; 022import org.apache.reef.util.cache.CacheImpl; 023import org.apache.reef.util.cache.SystemTime; 024import org.apache.reef.wake.Identifier; 025 026import java.net.InetSocketAddress; 027import java.util.concurrent.Callable; 028import java.util.concurrent.ExecutionException; 029 030/** 031 * Naming cache implementation. 032 */ 033public class NameCache implements Cache<Identifier, InetSocketAddress> { 034 035 private final Cache<Identifier, InetSocketAddress> cache; 036 037 /** 038 * Constructs a naming cache. 039 * 040 * @param timeout a cache entry timeout after write 041 */ 042 public NameCache(final long timeout) { 043 cache = new CacheImpl<>(new SystemTime(), timeout); 044 } 045 046 /** 047 * Gets an address for an identifier. 048 * 049 * @param key an identifier 050 * @param valueFetcher a callable to load a value for the corresponding identifier 051 * @return an Internet socket address 052 * @throws ExecutionException 053 */ 054 @Override 055 public InetSocketAddress get(final Identifier key, 056 final Callable<InetSocketAddress> valueFetcher) throws ExecutionException { 057 return cache.get(key, valueFetcher); 058 } 059 060 /** 061 * Invalidates the entry for an identifier. 062 * 063 * @param key an identifier 064 */ 065 @Override 066 public void invalidate(final Identifier key) { 067 cache.invalidate(key); 068 } 069 070}