message_cache.cc 3.0 KB

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