message_cache.h 3.7 KB

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