@@ -56,17 +56,39 @@ impl DnsCache {
56
56
let rcode = rcode. unwrap_or_else ( || Rcode :: NOERROR ) ;
57
57
58
58
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
+ }
59
63
64
+ /*
60
65
if rcode == Rcode::NXDOMAIN {
61
66
key = CacheKey::Secondary(qclass, domain_name.clone());
62
67
} else {
63
68
key = CacheKey::Primary(qtype.unwrap(), qclass, domain_name.clone());
64
69
}
70
+ */
65
71
66
72
if rcode != Rcode :: NOERROR {
67
73
rr_cache. set_rcode ( rcode) ;
68
74
}
69
75
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
+ /*
70
92
let mut cache_data = self.get_cache();
71
93
let mut rr_cache_vec_opt = cache_data.get_mut(&key).
72
94
map(|rr_cache_vec| rr_cache_vec.clone());
@@ -92,6 +114,8 @@ impl DnsCache {
92
114
93
115
self.set_cache(cache_data);
94
116
// see cache space
117
+
118
+ */
95
119
}
96
120
97
121
/// TODO: Crear test y mejorar función de acuerdo a RFC de Negative caching
0 commit comments