mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-27 06:25:51 +00:00
Cleanups.
This commit is contained in:
@@ -26,24 +26,29 @@ extern LogPartition TaggedCachePartition;
|
||||
template <typename c_Key, typename c_Data> class TaggedCache
|
||||
{
|
||||
public:
|
||||
typedef c_Key key_type;
|
||||
typedef c_Data data_type;
|
||||
|
||||
typedef boost::weak_ptr<data_type> weak_data_ptr;
|
||||
typedef boost::shared_ptr<data_type> data_ptr;
|
||||
typedef std::pair<time_t, data_ptr> cache_entry;
|
||||
typedef std::pair<key_type, cache_entry> cache_pair;
|
||||
typedef c_Key key_type;
|
||||
typedef c_Data data_type;
|
||||
typedef boost::weak_ptr<data_type> weak_data_ptr;
|
||||
typedef boost::shared_ptr<data_type> data_ptr;
|
||||
|
||||
protected:
|
||||
|
||||
typedef std::pair<time_t, data_ptr> cache_entry;
|
||||
typedef std::pair<key_type, cache_entry> cache_pair;
|
||||
typedef boost::unordered_map<key_type, cache_entry> cache_type;
|
||||
typedef typename cache_type::iterator cache_iterator;
|
||||
typedef boost::unordered_map<key_type, weak_data_ptr> map_type;
|
||||
typedef typename map_type::iterator map_iterator;
|
||||
|
||||
mutable boost::recursive_mutex mLock;
|
||||
|
||||
std::string mName;
|
||||
int mTargetSize, mTargetAge;
|
||||
std::string mName; // Used for logging
|
||||
int mTargetSize; // Desired number of cache entries
|
||||
int mTargetAge; // Desired maximum cache age
|
||||
|
||||
boost::unordered_map<key_type, cache_entry> mCache; // Hold strong reference to recent objects
|
||||
time_t mLastSweep;
|
||||
|
||||
boost::unordered_map<key_type, weak_data_ptr> mMap; // Track stored objects
|
||||
cache_type mCache; // Hold strong reference to recent objects
|
||||
map_type mMap; // Track stored objects
|
||||
time_t mLastSweep;
|
||||
|
||||
public:
|
||||
TaggedCache(const char *name, int size, int age)
|
||||
@@ -103,7 +108,7 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
|
||||
|
||||
// Pass 1, remove old objects from cache
|
||||
int cacheRemovals = 0;
|
||||
typename boost::unordered_map<key_type, cache_entry>::iterator cit = mCache.begin();
|
||||
cache_iterator cit = mCache.begin();
|
||||
while (cit != mCache.end())
|
||||
{
|
||||
if (cit->second.first < target)
|
||||
@@ -117,7 +122,7 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
|
||||
|
||||
// Pass 2, remove dead objects from map
|
||||
int mapRemovals = 0;
|
||||
typename boost::unordered_map<key_type, weak_data_ptr>::iterator mit = mMap.begin();
|
||||
map_iterator mit = mMap.begin();
|
||||
while (mit != mMap.end())
|
||||
{
|
||||
if (mit->second.expired())
|
||||
@@ -139,7 +144,7 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
// Is the object in the map?
|
||||
typename boost::unordered_map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
|
||||
map_iterator mit = mMap.find(key);
|
||||
if (mit == mMap.end())
|
||||
return false;
|
||||
if (mit->second.expired())
|
||||
@@ -149,7 +154,7 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch
|
||||
}
|
||||
|
||||
// Is the object in the cache?
|
||||
typename boost::unordered_map<key_type, cache_entry>::iterator cit = mCache.find(key);
|
||||
map_iterator cit = mCache.find(key);
|
||||
if (cit != mCache.end())
|
||||
{ // in both map and cache
|
||||
cit->second.first = time(NULL);
|
||||
@@ -167,13 +172,13 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(c
|
||||
|
||||
if (!valid)
|
||||
{ // remove from map too
|
||||
typename boost::unordered_map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
|
||||
map_iterator mit = mMap.find(key);
|
||||
if (mit == mMap.end()) // not in map, cannot be in cache
|
||||
return false;
|
||||
mMap.erase(mit);
|
||||
}
|
||||
|
||||
typename boost::unordered_map<key_type, cache_entry>::iterator cit = mCache.find(key);
|
||||
cache_iterator cit = mCache.find(key);
|
||||
if (cit == mCache.end())
|
||||
return false;
|
||||
mCache.erase(cit);
|
||||
@@ -186,7 +191,7 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
|
||||
// Return values: true=we had the data already
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
typename boost::unordered_map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
|
||||
map_iterator mit = mMap.find(key);
|
||||
if (mit == mMap.end())
|
||||
{ // not in map
|
||||
mCache.insert(cache_pair(key, cache_entry(time(NULL), data)));
|
||||
@@ -194,7 +199,7 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::shared_ptr<c_Data> cachedData = mit->second.lock();
|
||||
data_ptr cachedData = mit->second.lock();
|
||||
if (!cachedData)
|
||||
{ // in map, but expired. Update in map, insert in cache
|
||||
mit->second = data;
|
||||
@@ -209,7 +214,7 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
|
||||
data = cachedData;
|
||||
|
||||
// Valid in map, is it in cache?
|
||||
typename boost::unordered_map<key_type, cache_entry>::iterator cit = mCache.find(key);
|
||||
cache_iterator cit = mCache.find(key);
|
||||
if (cit != mCache.end())
|
||||
{
|
||||
cit->second.first = time(NULL); // Yes, refesh
|
||||
@@ -228,11 +233,11 @@ boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
|
||||
boost::recursive_mutex::scoped_lock sl(mLock);
|
||||
|
||||
// Is it in the map?
|
||||
typename boost::unordered_map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
|
||||
map_iterator mit = mMap.find(key);
|
||||
if (mit == mMap.end())
|
||||
return data_ptr(); // No, we're done
|
||||
|
||||
boost::shared_ptr<c_Data> cachedData = mit->second.lock();
|
||||
data_ptr cachedData = mit->second.lock();
|
||||
if (!cachedData)
|
||||
{ // in map, but expired. Sorry, we don't have it
|
||||
mMap.erase(mit);
|
||||
@@ -240,7 +245,7 @@ boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
|
||||
}
|
||||
|
||||
// Valid in map, is it in the cache?
|
||||
typename boost::unordered_map<key_type, cache_entry>::iterator cit = mCache.find(key);
|
||||
cache_iterator cit = mCache.find(key);
|
||||
if (cit != mCache.end())
|
||||
cit->second.first = time(NULL); // Yes, refresh
|
||||
else // No, add to cache
|
||||
@@ -252,17 +257,17 @@ boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
|
||||
template<typename c_Key, typename c_Data>
|
||||
bool TaggedCache<c_Key, c_Data>::store(const key_type& key, const c_Data& data)
|
||||
{
|
||||
boost::shared_ptr<c_Data> d = boost::make_shared<c_Data>(boost::cref(data));
|
||||
data_ptr d = boost::make_shared<c_Data>(boost::cref(data));
|
||||
return canonicalize(key, d);
|
||||
}
|
||||
|
||||
template<typename c_Key, typename c_Data>
|
||||
bool TaggedCache<c_Key, c_Data>::retrieve(const key_type& key, c_Data& data)
|
||||
{ // retrieve the value of the stored data
|
||||
boost::shared_ptr<c_Data> dataPtr = fetch(key);
|
||||
if (!dataPtr)
|
||||
data_ptr entry = fetch(key);
|
||||
if (!entry)
|
||||
return false;
|
||||
data = *dataPtr;
|
||||
data = *entry;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user