message_cache.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef __MESSAGE_CACHE_H
  15. #define __MESSAGE_CACHE_H
  16. #include <string>
  17. #include <boost/shared_ptr.hpp>
  18. #include <dns/message.h>
  19. #include "message_entry.h"
  20. #include <nsas/hash_table.h>
  21. #include <nsas/lru_list.h>
  22. #include "rrset_cache.h"
  23. namespace isc {
  24. namespace cache {
  25. /// \brief Message Cache
  26. /// The object of MessageCache represents the cache for class-specific
  27. /// messages.
  28. ///
  29. class MessageCache {
  30. // Noncopyable
  31. private:
  32. MessageCache(const MessageCache& source);
  33. MessageCache& operator=(const MessageCache& source);
  34. public:
  35. /// \param rrset_cache The cache that stores the RRsets that the
  36. /// message entry will points to
  37. /// \param cache_size The size of message cache.
  38. /// \param message_class The class of the message cache
  39. /// \param negative_soa_cache The cache that stores the SOA record
  40. /// that comes from negative response message
  41. MessageCache(const RRsetCachePtr& rrset_cache,
  42. uint32_t cache_size, uint16_t message_class,
  43. const RRsetCachePtr& negative_soa_cache);
  44. /// \brief Destructor function
  45. virtual ~MessageCache() {}
  46. /// \brief Look up message in cache.
  47. /// \param message generated response message if the message entry
  48. /// can be found.
  49. ///
  50. /// \return return true if the message can be found in cache, or else,
  51. /// return false.
  52. //TODO Maybe some user just want to get the message_entry.
  53. bool lookup(const isc::dns::Name& qname,
  54. const isc::dns::RRType& qtype,
  55. isc::dns::Message& message);
  56. /// \brief Update the message in the cache with the new one.
  57. /// If the message doesn't exist in the cache, it will be added
  58. /// directly.
  59. bool update(const isc::dns::Message& msg);
  60. #if 0
  61. /// \brief Dump the message cache to specified file.
  62. /// \todo It should can be dumped to one configured database.
  63. void dump(const std::string& file_name);
  64. /// \brief Load the cache from one file.
  65. /// \todo It should can be loaded from one configured database.
  66. void load(const std::string& file_name);
  67. /// \brief Resize the size of message cache in runtime.
  68. bool resize(uint32_t size);
  69. #endif
  70. protected:
  71. /// \brief Get the hash key for the message entry in the cache.
  72. /// \param name query name of the message.
  73. /// \param type query type of the message.
  74. /// \return return the hash key.
  75. HashKey getEntryHashKey(const isc::dns::Name& name,
  76. const isc::dns::RRType& type) const;
  77. // Make these variants be protected for easy unittest.
  78. protected:
  79. uint16_t message_class_; // The class of the message cache.
  80. RRsetCachePtr rrset_cache_;
  81. RRsetCachePtr negative_soa_cache_;
  82. isc::nsas::HashTable<MessageEntry> message_table_;
  83. isc::nsas::LruList<MessageEntry> message_lru_;
  84. };
  85. typedef boost::shared_ptr<MessageCache> MessageCachePtr;
  86. } // namespace cache
  87. } // namespace isc
  88. #endif // __MESSAGE_CACHE_H