| 51 | class TGlobalCachedDns: public IDns, public TNonCopyable { |
| 52 | public: |
| 53 | const TResolvedHost* Resolve(const TResolveTask& rt) override { |
| 54 | //2. search host in cache |
| 55 | { |
| 56 | TReadGuard guard(L_); |
| 57 | |
| 58 | TCache::const_iterator it = C_.find(rt.Info); |
| 59 | |
| 60 | if (it != C_.end()) { |
| 61 | return it->second.Get(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | TResolvedHostPtr res = ResolveA(rt); |
| 66 | |
| 67 | //update cache |
| 68 | { |
| 69 | TWriteGuard guard(L_); |
| 70 | |
| 71 | std::pair<TCache::iterator, bool> updateResult = C_.insert(std::make_pair(TResolveInfo(res->Host, rt.Info.Port), res)); |
| 72 | TResolvedHost* rh = updateResult.first->second.Get(); |
| 73 | |
| 74 | if (updateResult.second) { |
| 75 | //fresh resolved host, set cache record id for it |
| 76 | rh->Id = C_.size() - 1; |
| 77 | } |
| 78 | |
| 79 | return rh; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void AddAlias(const TString& host, const TString& alias) noexcept { |
| 84 | TWriteGuard guard(LA_); |