Cosmetic improvements.

This commit is contained in:
JoelKatz
2013-01-31 00:28:23 -08:00
parent 1f3479a87d
commit 50fce0f079

View File

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