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