rrset_cache.cc 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

Jelte Jansen's avatar
Jelte Jansen committed
15 16
#include <config.h>

17
#include "rrset_cache.h"
18 19
#include "logger.h"
#include <string>
20 21 22 23 24
#include <nsas/nsas_entry_compare.h>
#include <nsas/hash_table.h>
#include <nsas/hash_deleter.h>

using namespace isc::nsas;
25
using namespace isc::dns;
zhanglikun's avatar
zhanglikun committed
26
using namespace std;
27 28 29 30

namespace isc {
namespace cache {

31 32 33
RRsetCache::RRsetCache(uint32_t cache_size,
                       uint16_t rrset_class):
    class_(rrset_class),
34 35 36 37
    rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
    rrset_lru_((3 * cache_size),
                  new HashDeleter<RRsetEntry>(rrset_table_))
{
38
    LOG_DEBUG(logger, DBG_TRACE_BASIC, CACHE_RRSET_INIT).arg(cache_size).
39
        arg(RRClass(rrset_class));
40
}
41

42 43 44
RRsetEntryPtr
RRsetCache::lookup(const isc::dns::Name& qname,
                   const isc::dns::RRType& qtype)
45
{
46
    LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_LOOKUP).arg(qname).
47
        arg(qtype).arg(RRClass(class_));
zhanglikun's avatar
zhanglikun committed
48
    const string entry_name = genCacheEntryName(qname, qtype);
49 50 51

    RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name,
                                                       RRClass(class_)));
52 53 54 55 56 57
    if (entry_ptr) {
        if (entry_ptr->getExpireTime() > time(NULL)) {
            // Only touch the non-expired rrset entries
            rrset_lru_.touch(entry_ptr);
            return (entry_ptr);
        } else {
58
            LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_EXPIRED).arg(qname).
59
                arg(qtype).arg(RRClass(class_));
60 61 62 63 64
            // the rrset entry has expired, so just remove it from
            // hash table and lru list.
            rrset_table_.remove(entry_ptr->hashKey());
            rrset_lru_.remove(entry_ptr);
        }
65
    }
66

67
    LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_NOT_FOUND).arg(qname).
68
        arg(qtype).arg(RRClass(class_));
69
    return (RRsetEntryPtr());
70 71
}

72
RRsetEntryPtr
73
RRsetCache::update(const isc::dns::AbstractRRset& rrset,
74 75 76 77
                   const RRsetTrustLevel& level)
{
    LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_UPDATE).arg(rrset.getName()).
        arg(rrset.getType()).arg(rrset.getClass());
78
    // TODO: If the RRset is an NS, we should update the NSAS as well
79 80
    // lookup first
    RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
81
    if (entry_ptr) {
82
        if (entry_ptr->getTrustLevel() > level) {
83 84 85
            LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_UNTRUSTED).
                arg(rrset.getName()).arg(rrset.getType()).
                arg(rrset.getClass());
86
            // existed rrset entry is more authoritative, just return it
87
            return (entry_ptr);
88
        } else {
89 90 91
            LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_REMOVE_OLD).
                arg(rrset.getName()).arg(rrset.getType()).
                arg(rrset.getClass());
92 93
            // Remove the old rrset entry from the lru list.
            rrset_lru_.remove(entry_ptr);
94 95
        }
    }
96 97 98 99 100

    entry_ptr.reset(new RRsetEntry(rrset, level));
    rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
    rrset_lru_.add(entry_ptr);
    return (entry_ptr);
101 102 103 104 105
}

} // namespace cache
} // namespace isc