1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-11 16:44:05 +02:00

* Style fixes and removed spamy logging from TomahawkCache.

This commit is contained in:
Christian Muehlhaeuser
2013-06-05 20:26:46 +02:00
parent 725486a4e4
commit 7053c50067
2 changed files with 47 additions and 30 deletions

View File

@@ -30,6 +30,7 @@ using namespace TomahawkUtils;
Cache*Cache::s_instance = 0; Cache*Cache::s_instance = 0;
Cache* Cache::instance() Cache* Cache::instance()
{ {
if ( !s_instance ) if ( !s_instance )
@@ -38,6 +39,7 @@ Cache* Cache::instance()
return s_instance; return s_instance;
} }
Cache::Cache() Cache::Cache()
: QObject ( 0 ) : QObject ( 0 )
, m_cacheBaseDir ( TomahawkSettings::instance()->storageCacheLocation() + "/GenericCache/" ) , m_cacheBaseDir ( TomahawkSettings::instance()->storageCacheLocation() + "/GenericCache/" )
@@ -49,12 +51,14 @@ Cache::Cache()
m_pruneTimer.start(); m_pruneTimer.start();
} }
Cache::~Cache() Cache::~Cache()
{ {
} }
void Cache::pruneTimerFired()
void
Cache::pruneTimerFired()
{ {
QMutexLocker mutex_locker( &m_mutex ); QMutexLocker mutex_locker( &m_mutex );
@@ -62,15 +66,18 @@ void Cache::pruneTimerFired()
qlonglong currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch(); qlonglong currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
QVariantList clients = m_cacheManifest.value ( "clients" ).toList(); QVariantList clients = m_cacheManifest.value ( "clients" ).toList();
foreach ( const QVariant &client, clients ) { foreach ( const QVariant& client, clients )
{
const QString client_identifier = client.toString(); const QString client_identifier = client.toString();
const QString cache_dir = m_cacheBaseDir + client_identifier; const QString cache_dir = m_cacheBaseDir + client_identifier;
QSettings cached_settings ( cache_dir, QSettings::IniFormat ); QSettings cached_settings ( cache_dir, QSettings::IniFormat );
const QStringList keys = cached_settings.allKeys(); const QStringList keys = cached_settings.allKeys();
foreach ( const QString &key, keys ) { foreach ( const QString& key, keys )
{
CacheData data = cached_settings.value ( key ).value<TomahawkUtils::CacheData>(); CacheData data = cached_settings.value ( key ).value<TomahawkUtils::CacheData>();
if ( data.maxAge < currentMSecsSinceEpoch ) { if ( data.maxAge < currentMSecsSinceEpoch )
{
cached_settings.remove ( key ); cached_settings.remove ( key );
tLog() << Q_FUNC_INFO << "Removed stale entry: " << client_identifier << key; tLog() << Q_FUNC_INFO << "Removed stale entry: " << client_identifier << key;
} }
@@ -82,30 +89,35 @@ void Cache::pruneTimerFired()
} }
QVariant Cache::getData ( const QString& identifier, const QString& key ) QVariant
Cache::getData( const QString& identifier, const QString& key )
{ {
QMutexLocker mutex_locker( &m_mutex ); QMutexLocker mutex_locker( &m_mutex );
const QString cacheDir = m_cacheBaseDir + identifier; const QString cacheDir = m_cacheBaseDir + identifier;
QSettings cached_settings ( cacheDir, QSettings::IniFormat ); QSettings cached_settings ( cacheDir, QSettings::IniFormat );
if ( cached_settings.contains ( key ) ) { if ( cached_settings.contains ( key ) )
{
CacheData data = cached_settings.value ( key ).value<TomahawkUtils::CacheData>(); CacheData data = cached_settings.value ( key ).value<TomahawkUtils::CacheData>();
if ( data.maxAge < QDateTime::currentMSecsSinceEpoch() ) { if ( data.maxAge < QDateTime::currentMSecsSinceEpoch() )
{
cached_settings.remove ( key ); cached_settings.remove ( key );
tLog() << Q_FUNC_INFO << "Removed stale entry: " << identifier << key; tLog() << Q_FUNC_INFO << "Removed stale entry:" << identifier << key;
return QVariant(); return QVariant();
} }
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Fetched data for" << identifier << key; // tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Fetched data for" << identifier << key;
return data.data; return data.data;
} }
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "No such key" << key; // tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "No such key" << key;
return QVariant(); return QVariant();
} }
void Cache::putData ( const QString& identifier, qint64 maxAge, const QString& key, const QVariant& value )
void
Cache::putData( const QString& identifier, qint64 maxAge, const QString& key, const QVariant& value )
{ {
QMutexLocker mutex_locker( &m_mutex ); QMutexLocker mutex_locker( &m_mutex );
@@ -113,13 +125,16 @@ void Cache::putData ( const QString& identifier, qint64 maxAge, const QString& k
addClient ( identifier ); addClient ( identifier );
QSettings cached_settings ( cacheDir, QSettings::IniFormat ); QSettings cached_settings ( cacheDir, QSettings::IniFormat );
cached_settings.setValue ( key, QVariant::fromValue ( CacheData ( QDateTime::currentMSecsSinceEpoch() + maxAge, value ) ) ); cached_settings.setValue ( key, QVariant::fromValue ( CacheData ( QDateTime::currentMSecsSinceEpoch() + maxAge, value ) ) );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Storing from client " << identifier << maxAge << key << value; tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Storing from client" << identifier << maxAge << key << value;
} }
void Cache::addClient ( const QString& identifier )
void
Cache::addClient( const QString& identifier )
{ {
QVariantList clients = m_cacheManifest.value ( "clients" ).toList(); QVariantList clients = m_cacheManifest.value ( "clients" ).toList();
foreach ( const QVariant &client, clients ) { foreach ( const QVariant& client, clients )
{
const QString client_identifier = client.toString(); const QString client_identifier = client.toString();
if ( identifier == client_identifier ) return; if ( identifier == client_identifier ) return;
} }
@@ -130,24 +145,24 @@ void Cache::addClient ( const QString& identifier )
m_cacheManifest.sync(); m_cacheManifest.sync();
} }
void Cache::removeClient ( const QString& identifier )
void
Cache::removeClient( const QString& identifier )
{ {
QVariantList clients = m_cacheManifest.value ( "clients" ).toList(); QVariantList clients = m_cacheManifest.value ( "clients" ).toList();
QVariantList::iterator it = clients.begin(); QVariantList::iterator it = clients.begin();
while ( it != clients.end() ) { while ( it != clients.end() )
{
const QString client_identifier = it->toString(); const QString client_identifier = it->toString();
if ( identifier == client_identifier ) { if ( identifier == client_identifier )
{
tLog() << Q_FUNC_INFO << "removing client" << identifier; tLog() << Q_FUNC_INFO << "removing client" << identifier;
clients.erase ( it ); clients.erase ( it );
break; break;
} }
++it; ++it;
} }
m_cacheManifest.setValue ( "clients", clients );
m_cacheManifest.setValue( "clients", clients );
m_cacheManifest.sync(); m_cacheManifest.sync();
} }

View File

@@ -27,12 +27,14 @@
#include <QObject> #include <QObject>
#include <QTimer> #include <QTimer>
namespace TomahawkUtils { namespace TomahawkUtils
{
/** /**
* Internal data structure. Don't use. * Internal data structure. Don't use.
*/ */
struct CacheData { struct CacheData
{
CacheData(){} CacheData(){}
CacheData( qint64 maxAg, QVariant dat ) CacheData( qint64 maxAg, QVariant dat )
: maxAge( maxAg ) : maxAge( maxAg )
@@ -67,7 +69,7 @@ public:
* @param key the key to store the data * @param key the key to store the data
* @param value the data to store * @param value the data to store
*/ */
void putData( const QString &identifier, qint64 maxAge, const QString &key, const QVariant& value ); void putData( const QString& identifier, qint64 maxAge, const QString& key, const QVariant& value );
/** /**
* Retrieve data from the cache. * Retrieve data from the cache.
@@ -75,7 +77,7 @@ public:
* @param key the key to store the data * @param key the key to store the data
* @return the data, if found, if not found an invalid QVariant is returned. * @return the data, if found, if not found an invalid QVariant is returned.
*/ */
QVariant getData( const QString &identifier, const QString &key ); QVariant getData( const QString& identifier, const QString& key );
private slots: private slots:
void pruneTimerFired(); void pruneTimerFired();
@@ -88,13 +90,13 @@ private:
* Adds a client to the manifest. * Adds a client to the manifest.
* Does not lock the mutex. * Does not lock the mutex.
*/ */
void addClient( const QString &identifier ); void addClient( const QString& identifier );
/** /**
* Removes a client to the manifest. * Removes a client to the manifest.
* Does not lock the mutex. * Does not lock the mutex.
*/ */
void removeClient( const QString &identifier ); void removeClient( const QString& identifier );
QString m_cacheBaseDir; QString m_cacheBaseDir;
QSettings m_cacheManifest; QSettings m_cacheManifest;