added clean-up to DnsCache: will remove very old entries after 60 days
[pingcheck] / src / dns / dnscache.cpp
index 118f802..f607112 100644 (file)
@@ -35,6 +35,7 @@
 #include <boost/serialization/vector.hpp>
 #include <boost/archive/xml_oarchive.hpp>
 #include <boost/archive/xml_iarchive.hpp>
+#include <boost/date_time/gregorian/gregorian_types.hpp>
 
 #include "dns/dnsmaster.h"
 
@@ -129,6 +130,9 @@ void DnsCache::save_to_cachefile()
     {
         try
         {
+            // clean up: remove very old entries
+            remove_old_entries();
+
             // remember time of save
             std::string cache_save_time_str = boost::posix_time::to_iso_string(
                             boost::posix_time::second_clock::universal_time() );
@@ -236,6 +240,51 @@ bool DnsCache::check_timestamps(const boost::posix_time::ptime &cache_save_time)
 }
 
 
+/**
+ * @brief remove entries from cache that are older than a certain threshold
+ *
+ * this also removes entres from IpCache with no IPs
+ */
+void DnsCache::remove_old_entries()
+{
+    boost::posix_time::ptime thresh
+             = boost::posix_time::second_clock::universal_time()
+             - boost::gregorian::days( Config::CACHE_REMOVE_OUTDATED_DAYS );
+
+    // IP cache
+    bool some_ip_up_to_date;
+    BOOST_FOREACH( const ip_map_type::value_type key_and_ip, IpCache )
+    {
+        some_ip_up_to_date = false;
+        BOOST_FOREACH( HostAddress address, key_and_ip.second )
+        {
+            if ( ! address.get_ttl().was_set_before(thresh) )
+            {
+                some_ip_up_to_date = true;
+                break;
+            }
+        }
+
+        if ( ! some_ip_up_to_date )
+        {
+            GlobalLogger.debug() << "DnsCache: Removing empty/outdated IP list "
+                                 << "for " << key_and_ip.first.first;
+            IpCache.erase( key_and_ip.first );
+        }
+    }
+
+    // CNAME cache
+    BOOST_FOREACH( const cname_map_type::value_type key_and_cname, CnameCache )
+    {
+        if ( key_and_cname.second.Ttl.was_set_before( thresh ) )
+        {
+            GlobalLogger.debug() << "DnsCache: Removing outdated CNAME for "
+                                 << key_and_cname.first;
+            CnameCache.erase( key_and_cname.first );
+        }
+    }
+}
+
 // -----------------------------------------------------------------------------
 // UPDATE
 // -----------------------------------------------------------------------------