Skip to content

Commit 16d230b

Browse files
committed
refactor: simplify DnsCache::add method for better clarity
1 parent 77c2408 commit 16d230b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/dns_cache.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,39 @@ impl DnsCache {
5656
let rcode = rcode.unwrap_or_else(|| Rcode::NOERROR);
5757

5858
let key;
59+
match rcode {
60+
Rcode::NXDOMAIN => key = CacheKey::Secondary(qclass, domain_name.clone()),
61+
_ => key = CacheKey::Primary(qtype.unwrap(), qclass, domain_name.clone()),
62+
}
5963

64+
/*
6065
if rcode == Rcode::NXDOMAIN {
6166
key = CacheKey::Secondary(qclass, domain_name.clone());
6267
} else {
6368
key = CacheKey::Primary(qtype.unwrap(), qclass, domain_name.clone());
6469
}
70+
*/
6571

6672
if rcode != Rcode::NOERROR {
6773
rr_cache.set_rcode(rcode);
6874
}
6975

76+
if let Some(vec) = self.cache.get_mut(&key) {
77+
// If the key is already cached
78+
if let Some(stored) = vec.iter_mut()
79+
.find(|stored| stored.get_resource_record() == rr_cache.get_resource_record()) {
80+
// If a stored record with the same resource record exists, replace it
81+
*stored = rr_cache;
82+
} else {
83+
// If no such record is found, push the new record
84+
vec.push(rr_cache);
85+
}
86+
} else {
87+
// If the key is not cached, insert a new entry
88+
self.cache.put(key, vec![rr_cache]);
89+
}
90+
91+
/*
7092
let mut cache_data = self.get_cache();
7193
let mut rr_cache_vec_opt = cache_data.get_mut(&key).
7294
map(|rr_cache_vec| rr_cache_vec.clone());
@@ -92,6 +114,8 @@ impl DnsCache {
92114
93115
self.set_cache(cache_data);
94116
// see cache space
117+
118+
*/
95119
}
96120

97121
/// TODO: Crear test y mejorar función de acuerdo a RFC de Negative caching

0 commit comments

Comments
 (0)