/** * Add an entry to the cache. If there's already an * entry then for this host then the entry will be * replaced. */ public function put(\blaze\lang\String $host, \blaze\lang\Object $address) { $policy = $this->getPolicy(); if ($policy == InetAddressCachePolicy::NEVER) { return $this; } // purge any expired entries if ($policy != InetAddressCachePolicy::FOREVER) { // As we iterate in insertion order we can // terminate when a non-expired entry is found. $expired = new LinkedList(); $i = $cache->keySet()->iterator(); $now = System::currentTimeMillis(); while ($i->hasNext()) { $key = $i->next(); $entry = $cache->get($key); if ($entry->expiration >= 0 && $entry->expiration < $now) { $expired->add($key); } else { break; } } $i = $expired->iterator(); while ($i->hasNext()) { $cache->remove($i->next()); } } // create new entry and add it to the cache // -- as a HashMap replaces existing entries we // don't need to explicitly check if there is // already an entry for this host. $expiration; if ($policy == InetAddressCachePolicy::FOREVER) { $expiration = -1; } else { $expiration = System::currentTimeMillis() + $policy * 1000; } $entry = new CacheEntry($address, $expiration); $cache->put($host, $entry); return $this; }