diff --git a/src/cpp/ripple/TaggedCache.h b/src/cpp/ripple/TaggedCache.h index 2a0e6a719..f8cb5d499 100644 --- a/src/cpp/ripple/TaggedCache.h +++ b/src/cpp/ripple/TaggedCache.h @@ -41,6 +41,10 @@ protected: weak_data_ptr weak_ptr; cache_entry(time_t l, const data_ptr& d) : last_use(l), ptr(d), weak_ptr(d) { ; } + bool isCached() { return !!ptr; } + bool isExpired() { return weak_ptr.expired(); } + data_ptr lock() { return weak_ptr.lock(); } + void touch() { last_use = time(NULL); } }; typedef std::pair cache_pair; @@ -187,18 +191,19 @@ template bool TaggedCache::touch cache_iterator cit = mCache.find(key); if (cit == mCache.end()) // Don't have the object return false; + cache_entry& entry = cit->second; - if (cit->second.ptr) - { // Have the object in cache - cit->second.last_use = time(NULL); + if (entry.isCached()) + { + entry.touch(); return true; } - cit->second.ptr = cit->second.weak_ptr.lock(); - if (cit->second.ptr) + entry.ptr = entry.lock(); + if (entry.isCached()) { // We just put the object back in cache ++mCacheCount; - cit->second.last_use = time(NULL); + entry.touch(); return true; } @@ -214,16 +219,17 @@ template bool TaggedCache::del(c cache_iterator cit = mCache.find(key); if (cit == mCache.end()) return false; + cache_entry& entry = cit->second; bool ret = false; - if (cit->second.ptr) + if (entry.isCached()) { --mCacheCount; - cit->second.reset(); + entry.ptr.reset(); ret = true; } - if (!valid || cit->second.weak_ptr.expired()) + if (!valid || entry.isExpired()) mCache.erase(cit); return true; } @@ -241,40 +247,40 @@ bool TaggedCache::canonicalize(const key_type& key, boost::shared ++mCacheCount; return false; } + cache_entry& entry = cit->second; + entry.touch(); - cit->second.last_use = time(NULL); - - if (cit->second.ptr) + if (entry.isCached()) { if (replace) { - cit->second.ptr = data; - cit->second.weak_ptr = data; + entry.ptr = data; + entry.weak_ptr = data; } else - data = cit->second.ptr; + data = entry.ptr; return true; } - data_ptr cachedData = cit->second.weak_ptr.lock(); + data_ptr cachedData = entry.lock(); if (cachedData) { if (replace) { - cit->second.ptr = data; - cit->second.weak_ptr = data; + entry.ptr = data; + entry.weak_ptr = data; } else { - cit->second.ptr = cachedData; + entry.ptr = cachedData; data = cachedData; } ++mCacheCount; return true; } - cit->second.ptr = data; - cit->second.weak_ptr = data; + entry.ptr = data; + entry.weak_ptr = data; ++mCacheCount; return false; @@ -288,16 +294,17 @@ boost::shared_ptr TaggedCache::fetch(const key_type& key) cache_iterator cit = mCache.find(key); if (cit == mCache.end()) return data_ptr(); - cit->second.last_use = time(NULL); + cache_entry& entry = cit->second; + entry.touch(); - if (cit->second.ptr) - return cit->second.ptr; + if (entry.isCached()) + return entry.ptr; - cit->second.ptr = cit->second.weak_ptr.lock(); - if (cit->second.ptr) + entry.ptr = entry.lock(); + if (entry.isCached()) { ++mCacheCount; - return cit->second.ptr; + return entry.ptr; } mCache.erase(cit); return data_ptr();