anchor.diff:  %% in the cache.
   do_refresh_cache(CacheDb) ->
       Now = times(),
  -    true = ets:safe_fixtable(CacheDb, true),
  -    Res = do_refresh_cache(ets:first(CacheDb), CacheDb, Now, Now),
  -    true = ets:safe_fixtable(CacheDb, false),
  -    Res.
  -
  -do_refresh_cache('$end_of_table', _, _, OldestT) ->
  -    OldestT;
  -do_refresh_cache(Key, CacheDb, Now, OldestT) ->
  -    Next = ets:next(CacheDb, Key),
  -    OldT =
  -        case ets:lookup(CacheDb, Key) of
  -	    [{_,RR,_}] when RR#dns_rr.tm + RR#dns_rr.ttl < Now ->
  -		ets:delete(CacheDb, Key),
  -		OldestT;
  -	     [{_,#dns_rr{},C}] when C < OldestT ->
  -		C;
  -	     _ ->
  -		OldestT
  -	end,
  -    do_refresh_cache(Next, CacheDb, Now, OldT).
  +    [OldestT | RRsToDelete] =
  +        ets:foldl(
  +          fun (#dns_rr{tm = TM, ttl = TTL} = RR, [T | RRs])
  +                when TM + TTL < Now -> % Expired - delete it
  +                  [T, RR | RRs];
  +              (#dns_rr{cnt = C}, [T | RRs])
  +                when C < T -> % Older than previously oldest - use this cnt
  +                  [C | RRs];
  +              (#dns_rr{}, T_RRs) -> % Keep - do not delete
  +                  T_RRs
  +          end, [Now], CacheDb),
  +    _ = [ets:delete_object(CacheDb, RR) || RR <- RRsToDelete],
  +    OldestT.
  +
   
   %% -------------------------------------------------------------------
   %% Allocate room for a new entry in the cache.
  
positive.code: %% Case fold upper-case to lower-case according to RFC 4343
  %% "Domain Name System (DNS) Case Insensitivity Clarification".
  %%
  %% NOTE: this code is in kernel and we don't want to relay
  %% to much on stdlib. Furthermore string:to_lower/1
  %% does not follow RFC 4343.
  %%
  
negative.code: gethostbyaddr(IP) ->
      case dnip(IP) of
  	{ok, {IP1, HType, HLen, DnIP}} ->
              RRs = match_rr(dns_rr_match(DnIP, in, ptr)),
  	    ent_gethostbyaddr(RRs,  IP1, HType, HLen);
  	Error -> Error
      end.
  