From dd16182b4b3f107d5c26015622c1cb8c06619ad7 Mon Sep 17 00:00:00 2001 From: Anders Fredlund Date: Fri, 20 May 2011 08:12:37 +0200 Subject: Remove expired dns cache entries before removing oldest A suggestion how to make a smarter delete function when the cache is full. First look through the entire cache and remove all entries which have expired. If none use the old solution and just remove the last entry in the MRU list. Change-Id: I5f997ab35290a55dc6e1ddf37d725759edf83d36 --- libc/netbsd/resolv/res_cache.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'libc') diff --git a/libc/netbsd/resolv/res_cache.c b/libc/netbsd/resolv/res_cache.c index e6302ed..58d60c9 100644 --- a/libc/netbsd/resolv/res_cache.c +++ b/libc/netbsd/resolv/res_cache.c @@ -1396,6 +1396,27 @@ _cache_remove_oldest( Cache* cache ) _cache_remove_p(cache, lookup); } +/* Remove all expired entries from the hash table. + */ +static void _cache_remove_expired(Cache* cache) { + Entry* e; + time_t now = _time_now(); + + for (e = cache->mru_list.mru_next; e != &cache->mru_list;) { + // Entry is old, remove + if (now >= e->expires) { + Entry** lookup = _cache_lookup_p(cache, e); + if (*lookup == NULL) { /* should not happen */ + XLOG("%s: ENTRY NOT IN HTABLE ?", __FUNCTION__); + return; + } + e = e->mru_next; + _cache_remove_p(cache, lookup); + } else { + e = e->mru_next; + } + } +} ResolvCacheStatus _resolv_cache_lookup( struct resolv_cache* cache, @@ -1509,7 +1530,10 @@ _resolv_cache_add( struct resolv_cache* cache, } if (cache->num_entries >= cache->max_entries) { - _cache_remove_oldest(cache); + _cache_remove_expired(cache); + if (cache->num_entries >= cache->max_entries) { + _cache_remove_oldest(cache); + } /* need to lookup again */ lookup = _cache_lookup_p(cache, key); e = *lookup; -- cgit v1.1