mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Remove the JSONCache. It was basically a failed experiment.
This commit is contained in:
@@ -88,8 +88,6 @@ static const uint64 tenTo17m1 = tenTo17 - 1;
|
||||
#include "protocol/ripple_STAmount.cpp"
|
||||
#include "protocol/ripple_STAmountRound.cpp"
|
||||
|
||||
#include "utility/ripple_JSONCache.cpp"
|
||||
|
||||
}
|
||||
|
||||
// These must be outside the namespace because of boost
|
||||
|
||||
@@ -53,7 +53,6 @@ namespace ripple
|
||||
#include "protocol/ripple_TxFormat.h"
|
||||
#include "protocol/ripple_TxFormats.h"
|
||||
|
||||
#include "utility/ripple_JSONCache.h"
|
||||
#include "utility/ripple_UptimeTimerAdapter.h"
|
||||
|
||||
}
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
JSONCache::Key::Key (int op, uint256 const& ledger, uint160 const& object, int lastUse)
|
||||
: mLedger (ledger)
|
||||
, mObject (object)
|
||||
, mOperation (op)
|
||||
, mLastUse (lastUse)
|
||||
{
|
||||
mHash = static_cast <std::size_t> (mOperation);
|
||||
|
||||
mLedger.hash_combine (mHash);
|
||||
|
||||
mObject.hash_combine (mHash);
|
||||
}
|
||||
|
||||
int JSONCache::Key::compare (Key const& other) const
|
||||
{
|
||||
if (mHash < other.mHash) return -1;
|
||||
|
||||
if (mHash > other.mHash) return 1;
|
||||
|
||||
if (mOperation < other.mOperation) return -1;
|
||||
|
||||
if (mOperation > other.mOperation) return 1;
|
||||
|
||||
if (mLedger < other.mLedger) return -1;
|
||||
|
||||
if (mLedger > other.mLedger) return 1;
|
||||
|
||||
if (mObject < other.mObject) return -1;
|
||||
|
||||
if (mObject > other.mObject) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool JSONCache::Key::operator< (Key const& rhs) const
|
||||
{
|
||||
return compare (rhs) < 0;
|
||||
}
|
||||
bool JSONCache::Key::operator> (Key const& rhs) const
|
||||
{
|
||||
return compare (rhs) > 0;
|
||||
}
|
||||
bool JSONCache::Key::operator<= (Key const& rhs) const
|
||||
{
|
||||
return compare (rhs) <= 0;
|
||||
}
|
||||
bool JSONCache::Key::operator>= (Key const& rhs) const
|
||||
{
|
||||
return compare (rhs) >= 0;
|
||||
}
|
||||
bool JSONCache::Key::operator!= (Key const& rhs) const
|
||||
{
|
||||
return compare (rhs) != 0;
|
||||
}
|
||||
bool JSONCache::Key::operator== (Key const& rhs) const
|
||||
{
|
||||
return compare (rhs) == 0;
|
||||
}
|
||||
|
||||
void JSONCache::Key::touch (Key const& key) const
|
||||
{
|
||||
mLastUse = key.mLastUse;
|
||||
}
|
||||
|
||||
bool JSONCache::Key::isExpired (int expireTimeSeconds) const
|
||||
{
|
||||
return mLastUse < expireTimeSeconds;
|
||||
}
|
||||
|
||||
std::size_t JSONCache::Key::getHash () const
|
||||
{
|
||||
return mHash;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
JSONCache::JSONCache (int expirationTimeInSeconds)
|
||||
: m_expirationTime (expirationTimeInSeconds)
|
||||
, mHits (0)
|
||||
, mMisses (0)
|
||||
{
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
float JSONCache::getHitRate ()
|
||||
{
|
||||
boost::recursive_mutex::scoped_lock sl (m_lock);
|
||||
|
||||
return (static_cast <float> (mHits) * 100.f) / (1.0f + mHits + mMisses);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int JSONCache::getNumberOfEntries ()
|
||||
{
|
||||
boost::recursive_mutex::scoped_lock sl (m_lock);
|
||||
|
||||
return m_cache.size ();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
JSONCache::data_t JSONCache::getEntry (Kind kind, LedgerHash const& ledger, uint160 const& object)
|
||||
{
|
||||
JSONCache::data_t result; // default constructor indicates not found
|
||||
|
||||
Key key (kind, ledger, object, getUptime ());
|
||||
|
||||
{
|
||||
boost::recursive_mutex::scoped_lock sl (m_lock);
|
||||
|
||||
boost::unordered_map <Key, data_t>::iterator it = m_cache.find (key);
|
||||
|
||||
if (it != m_cache.end ())
|
||||
{
|
||||
++mHits;
|
||||
|
||||
it->first.touch (key);
|
||||
|
||||
result = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
++mMisses;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void JSONCache::storeEntry (Kind kind, uint256 const& ledger, uint160 const& object, data_t const& data)
|
||||
{
|
||||
Key key (kind, ledger, object, getUptime ());
|
||||
|
||||
{
|
||||
boost::recursive_mutex::scoped_lock sl (m_lock);
|
||||
|
||||
m_cache.insert (std::pair <Key, data_t> (key, data));
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void JSONCache::sweep ()
|
||||
{
|
||||
int sweepTime = getUptime ();
|
||||
|
||||
if (sweepTime >= m_expirationTime)
|
||||
{
|
||||
sweepTime -= m_expirationTime;
|
||||
|
||||
{
|
||||
boost::recursive_mutex::scoped_lock sl (m_lock);
|
||||
|
||||
boost::unordered_map <Key, data_t>::iterator it = m_cache.begin ();
|
||||
|
||||
while (it != m_cache.end ())
|
||||
{
|
||||
if (it->first.isExpired (sweepTime))
|
||||
{
|
||||
it = m_cache.erase (it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int JSONCache::getUptime () const
|
||||
{
|
||||
return UptimeTimer::getInstance ().getElapsedSeconds ();
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_JSCONCACHE_H
|
||||
#define RIPPLE_JSCONCACHE_H
|
||||
|
||||
/** A simple cache for JSON.
|
||||
|
||||
@note All member functions are thread-safe.
|
||||
*/
|
||||
class JSONCache
|
||||
{
|
||||
public:
|
||||
class Key
|
||||
{
|
||||
public:
|
||||
Key (int op, const uint256& ledger, const uint160& object, int lastUse);
|
||||
int compare (const Key& k) const;
|
||||
bool operator< (const Key& k) const;
|
||||
bool operator> (const Key& k) const;
|
||||
bool operator<= (const Key& k) const;
|
||||
bool operator>= (const Key& k) const;
|
||||
bool operator!= (const Key& k) const;
|
||||
bool operator== (const Key& k) const;
|
||||
|
||||
void touch (Key const& key) const;
|
||||
bool isExpired (int expireTime) const;
|
||||
|
||||
std::size_t getHash () const;
|
||||
|
||||
private:
|
||||
uint256 mLedger;
|
||||
uint160 mObject;
|
||||
int mOperation;
|
||||
mutable int mLastUse;
|
||||
std::size_t mHash;
|
||||
};
|
||||
|
||||
public:
|
||||
typedef boost::shared_ptr <Json::Value> data_t;
|
||||
|
||||
public:
|
||||
enum Kind
|
||||
{
|
||||
kindLines,
|
||||
kindOffers
|
||||
};
|
||||
|
||||
/** Construct the cache.
|
||||
|
||||
@param expirationTimeInSeconds The time until cached items expire, in seconds.
|
||||
*/
|
||||
explicit JSONCache (int expirationTimeInSeconds);
|
||||
|
||||
/** Return the fraction of cache hits.
|
||||
*/
|
||||
float getHitRate ();
|
||||
|
||||
/** Return the number of cached items.
|
||||
*/
|
||||
int getNumberOfEntries ();
|
||||
|
||||
/** Retrieve a cached item.
|
||||
|
||||
@return The item, or a default constructed container if it was not found.
|
||||
*/
|
||||
data_t getEntry (Kind kind, LedgerHash const& ledger, uint160 const& object);
|
||||
|
||||
/** Store an item in the cache.
|
||||
*/
|
||||
void storeEntry (Kind kind, LedgerHash const& ledger, uint160 const& object, data_t const& data);
|
||||
|
||||
/** Purge expired items.
|
||||
|
||||
This must be called periodically.
|
||||
*/
|
||||
void sweep ();
|
||||
|
||||
private:
|
||||
int getUptime () const;
|
||||
|
||||
private:
|
||||
int const m_expirationTime;
|
||||
boost::unordered_map <Key, data_t> m_cache;
|
||||
boost::recursive_mutex m_lock;
|
||||
uint64 mHits;
|
||||
uint64 mMisses;
|
||||
};
|
||||
|
||||
inline std::size_t hash_value (JSONCache::Key const& key)
|
||||
{
|
||||
return key.getHash ();
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user