rrset_cache.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <string>
  16. #include "rrset_cache.h"
  17. #include <nsas/nsas_entry_compare.h>
  18. #include <nsas/hash_table.h>
  19. #include <nsas/hash_deleter.h>
  20. using namespace isc::nsas;
  21. using namespace isc::dns;
  22. using namespace std;
  23. namespace isc {
  24. namespace cache {
  25. RRsetCache::RRsetCache(uint32_t cache_size,
  26. uint16_t rrset_class):
  27. class_(rrset_class),
  28. rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
  29. rrset_lru_((3 * cache_size),
  30. new HashDeleter<RRsetEntry>(rrset_table_))
  31. {
  32. }
  33. RRsetEntryPtr
  34. RRsetCache::lookup(const isc::dns::Name& qname,
  35. const isc::dns::RRType& qtype)
  36. {
  37. const string entry_name = genCacheEntryName(qname, qtype);
  38. RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name, RRClass(class_)));
  39. if (entry_ptr) {
  40. if (entry_ptr->getExpireTime() > time(NULL)) {
  41. // Only touch the non-expired rrset entries
  42. rrset_lru_.touch(entry_ptr);
  43. return (entry_ptr);
  44. } else {
  45. // the rrset entry has expired, so just remove it from
  46. // hash table and lru list.
  47. rrset_table_.remove(entry_ptr->hashKey());
  48. rrset_lru_.remove(entry_ptr);
  49. }
  50. }
  51. return (RRsetEntryPtr());
  52. }
  53. RRsetEntryPtr
  54. RRsetCache::update(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) {
  55. // TODO: If the RRset is an NS, we should update the NSAS as well
  56. // lookup first
  57. RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
  58. if (entry_ptr) {
  59. if (entry_ptr->getTrustLevel() > level) {
  60. // existed rrset entry is more authoritative, just return it
  61. return (entry_ptr);
  62. } else {
  63. // Remove the old rrset entry from the lru list.
  64. rrset_lru_.remove(entry_ptr);
  65. }
  66. }
  67. entry_ptr.reset(new RRsetEntry(rrset, level));
  68. rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
  69. rrset_lru_.add(entry_ptr);
  70. return (entry_ptr);
  71. }
  72. } // namespace cache
  73. } // namespace isc