message_cache.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <nsas/nsas_entry_compare.h>
  16. #include <nsas/hash_table.h>
  17. #include <nsas/hash_deleter.h>
  18. #include "message_cache.h"
  19. #include "cache_entry_key.h"
  20. using namespace isc::nsas;
  21. using namespace isc::dns;
  22. using namespace std;
  23. namespace isc {
  24. namespace cache {
  25. MessageCache::MessageCache(boost::shared_ptr<RRsetCache> rrset_cache,
  26. uint32_t cache_size, uint16_t message_class):
  27. message_class_(message_class),
  28. rrset_cache_(rrset_cache),
  29. message_table_(new NsasEntryCompare<MessageEntry>, cache_size),
  30. message_lru_((3 * cache_size),
  31. new HashDeleter<MessageEntry>(message_table_))
  32. {
  33. }
  34. bool
  35. MessageCache::lookup(const isc::dns::Name& qname,
  36. const isc::dns::RRType& qtype,
  37. isc::dns::Message& response)
  38. {
  39. std::string entry_name = genCacheEntryName(qname, qtype);
  40. HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
  41. MessageEntryPtr msg_entry = message_table_.get(entry_key);
  42. if(msg_entry) {
  43. // Check whether the message entry has expired.
  44. if (msg_entry->getExpireTime() > time(NULL)) {
  45. message_lru_.touch(msg_entry);
  46. return (msg_entry->genMessage(time(NULL), response));
  47. } else {
  48. // message entry expires, remove it from hash table and lru list.
  49. message_table_.remove(entry_key);
  50. message_lru_.remove(msg_entry);
  51. return (false);
  52. }
  53. }
  54. return (false);
  55. }
  56. bool
  57. MessageCache::update(const Message& msg) {
  58. QuestionIterator iter = msg.beginQuestion();
  59. std::string entry_name = genCacheEntryName((*iter)->getName(), (*iter)->getType());
  60. HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
  61. // The simplest way to update is removing the old message entry directly.
  62. // We have find the existed message entry, since we need to delete it
  63. // from lru list too.
  64. // TODO, but there should be a better way, since we here have to remove and
  65. // add the message entry, maybe there is one way to touch it once.
  66. MessageEntryPtr old_msg_entry = message_table_.get(entry_key);
  67. if (old_msg_entry) {
  68. message_lru_.remove(old_msg_entry);
  69. }
  70. MessageEntryPtr msg_entry(new MessageEntry(msg, rrset_cache_));
  71. message_lru_.add(msg_entry);
  72. return (message_table_.add(msg_entry, entry_key, true));
  73. }
  74. #if 0
  75. void
  76. MessageCache::dump(const std::string&) {
  77. //TODO
  78. }
  79. void
  80. MessageCache::load(const std::string&) {
  81. //TODO
  82. }
  83. bool
  84. MessageCache::resize(uint32_t) {
  85. //TODO
  86. return (true);
  87. }
  88. #endif
  89. } // namespace cache
  90. } // namespace isc