Separate hostname resolve cache based on ip_type

(cherry picked from commit c1c1ec690e)
This commit is contained in:
Fabio Alessandrelli 2016-12-08 12:18:18 +01:00
parent 00fdcf3cd0
commit 3bb1709fd7

View file

@ -107,6 +107,10 @@ struct _IP_ResolverPrivate {
HashMap<String, IP_Address> cache; HashMap<String, IP_Address> cache;
static String get_cache_key(String p_hostname, IP::Type p_type) {
return itos(p_type) + p_hostname;
}
}; };
@ -115,14 +119,12 @@ IP_Address IP::resolve_hostname(const String& p_hostname, IP::Type p_type) {
GLOBAL_LOCK_FUNCTION; GLOBAL_LOCK_FUNCTION;
if (resolver->cache.has(p_hostname)) String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
if ((resolver->cache[p_hostname].is_ipv4() && p_type != IP::TYPE_IPV6) || if (resolver->cache.has(key))
(!resolver->cache[p_hostname].is_ipv4() && p_type != IP::TYPE_IPV4)) return resolver->cache[key];
return resolver->cache[p_hostname];
// requested type is different from type in cache. continue resolution, if successful it'll overwrite cache
IP_Address res = _resolve_hostname(p_hostname, p_type); IP_Address res = _resolve_hostname(p_hostname, p_type);
resolver->cache[p_hostname]=res; resolver->cache[key]=res;
return res; return res;
} }
@ -137,12 +139,11 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname, IP::Typ
return id; return id;
} }
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
resolver->queue[id].hostname=p_hostname; resolver->queue[id].hostname=p_hostname;
resolver->queue[id].type = p_type; resolver->queue[id].type = p_type;
if (resolver->cache.has(p_hostname) && if (resolver->cache.has(key)) {
((resolver->cache[p_hostname].is_ipv4() && p_type != IP::TYPE_IPV6) || resolver->queue[id].response=resolver->cache[key];
(!resolver->cache[p_hostname].is_ipv4() && p_type != IP::TYPE_IPV4))) {
resolver->queue[id].response=resolver->cache[p_hostname];
resolver->queue[id].status=IP::RESOLVER_STATUS_DONE; resolver->queue[id].status=IP::RESOLVER_STATUS_DONE;
} else { } else {
resolver->queue[id].response=IP_Address(); resolver->queue[id].response=IP_Address();
@ -196,7 +197,10 @@ void IP::clear_cache(const String &p_hostname) {
if (p_hostname.empty()) { if (p_hostname.empty()) {
resolver->cache.clear(); resolver->cache.clear();
} else { } else {
resolver->cache.erase(p_hostname); resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
} }
}; };