mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-03-31 06:02:27 +02:00
Per-file cache entry saving and loading
This commit is contained in:
parent
b9f80edea9
commit
d391b9bd66
@ -20,6 +20,7 @@
|
||||
#include <QDesktopServices>
|
||||
#include <QDir>
|
||||
#include <QSettings>
|
||||
#include <QCryptographicHash>
|
||||
|
||||
#include "infosystemcache.h"
|
||||
|
||||
@ -37,17 +38,17 @@ InfoSystemCache::InfoSystemCache( QObject* parent )
|
||||
, m_cacheRemainingToLoad( 0 )
|
||||
{
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
|
||||
QString cacheBaseDir = QDesktopServices::storageLocation( QDesktopServices::CacheLocation );
|
||||
for( int i = 0; i <= InfoNoInfo; i++ )
|
||||
{
|
||||
InfoType type = (InfoType)(i);
|
||||
QString cacheDir = cacheBaseDir + "/InfoSystemCache/" + QString::number( i );
|
||||
QString cacheFile = cacheDir + '/' + QString::number( i );
|
||||
QDir dir( cacheDir );
|
||||
if( dir.exists() && QFile::exists( cacheFile ) )
|
||||
if( dir.exists() )
|
||||
{
|
||||
m_cacheRemainingToLoad++;
|
||||
QMetaObject::invokeMethod( this, "loadCache", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QString, cacheFile ) );
|
||||
QMetaObject::invokeMethod( this, "loadCache", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QString, cacheDir ) );
|
||||
}
|
||||
}
|
||||
m_syncTimer.setInterval( 60000 );
|
||||
@ -64,7 +65,7 @@ InfoSystemCache::~InfoSystemCache()
|
||||
for ( int i = 0; i <= InfoNoInfo; i++ )
|
||||
{
|
||||
InfoType type = (InfoType)(i);
|
||||
if ( m_dirtySet.contains( type ) && m_dataCache.contains( type ) )
|
||||
if ( m_dirtySet.contains( type ) && !m_dirtySet[type].isEmpty() && m_dataCache.contains( type ) )
|
||||
{
|
||||
QString cacheDir = cacheBaseDir + "/InfoSystemCache/" + QString::number( i );
|
||||
saveCache( type, cacheDir );
|
||||
@ -85,7 +86,7 @@ InfoSystemCache::syncTimerFired()
|
||||
for ( int i = 0; i <= InfoNoInfo; i++ )
|
||||
{
|
||||
InfoType type = (InfoType)(i);
|
||||
if ( m_dirtySet.contains( type ) && m_dataCache.contains( type ) )
|
||||
if ( m_dirtySet.contains( type ) && !m_dirtySet[type].isEmpty() && m_dataCache.contains( type ) )
|
||||
{
|
||||
QString cacheDir = cacheBaseDir + "/InfoSystemCache/" + QString::number( i );
|
||||
saveCache( type, cacheDir );
|
||||
@ -123,7 +124,6 @@ InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCriteriaHash crite
|
||||
m_dataCache[type] = typedatacache;
|
||||
typeinserttimecache.remove( criteria );
|
||||
m_insertTimeCache[type] = typeinserttimecache;
|
||||
m_dirtySet.insert( type );
|
||||
emit notInCache( criteria, caller, type, input, customData );
|
||||
return;
|
||||
}
|
||||
@ -133,7 +133,7 @@ InfoSystemCache::getCachedInfoSlot( Tomahawk::InfoSystem::InfoCriteriaHash crite
|
||||
QHash< InfoCriteriaHash, QDateTime > typemaxtimecache = m_maxTimeCache[type];
|
||||
typemaxtimecache[criteria] = QDateTime::fromMSecsSinceEpoch( QDateTime::currentMSecsSinceEpoch() + newMaxAge );
|
||||
m_maxTimeCache[type] = typemaxtimecache;
|
||||
m_dirtySet.insert( type );
|
||||
m_dirtySet[type].insert( criteria );
|
||||
}
|
||||
|
||||
emit info( caller, type, input, m_dataCache[type][criteria], customData );
|
||||
@ -153,21 +153,30 @@ InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash criteri
|
||||
m_dataCache[type] = typedatacache;
|
||||
m_insertTimeCache[type] = typeinserttimecache;
|
||||
m_maxTimeCache[type] = typemaxtimecache;
|
||||
m_dirtySet.insert( type );
|
||||
m_dirtySet[type].insert( criteria );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
InfoSystemCache::loadCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheFile )
|
||||
InfoSystemCache::loadCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheDir )
|
||||
{
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
QSettings cachedSettings( cacheFile, QSettings::IniFormat );
|
||||
|
||||
foreach ( QString group, cachedSettings.childGroups() )
|
||||
QDir dir( cacheDir );
|
||||
|
||||
QFileInfoList files = dir.entryInfoList( QDir::NoDotAndDotDot );
|
||||
|
||||
foreach ( QFileInfo file, files )
|
||||
{
|
||||
cachedSettings.beginGroup( group );
|
||||
if ( cachedSettings.value( "maxtime" ).toDateTime().toMSecsSinceEpoch() < QDateTime::currentMSecsSinceEpoch() )
|
||||
if ( file.baseName().toLongLong() < QDateTime::currentMSecsSinceEpoch() )
|
||||
{
|
||||
if ( !QFile::remove( file.canonicalFilePath() ) )
|
||||
qDebug() << "Failed to remove stale cache file " << file.canonicalFilePath();
|
||||
continue;
|
||||
}
|
||||
|
||||
QSettings cachedSettings( file.canonicalFilePath(), QSettings::IniFormat );
|
||||
|
||||
QHash< InfoCriteriaHash, QVariant > dataHash = m_dataCache[type];
|
||||
QHash< InfoCriteriaHash, QDateTime > insertDateHash = m_insertTimeCache[type];
|
||||
QHash< InfoCriteriaHash, QDateTime > maxDateHash = m_maxTimeCache[type];
|
||||
@ -183,12 +192,12 @@ InfoSystemCache::loadCache( Tomahawk::InfoSystem::InfoType type, const QString &
|
||||
cachedSettings.endArray();
|
||||
dataHash[criteria] = cachedSettings.value( "data" );
|
||||
insertDateHash[criteria] = cachedSettings.value( "inserttime" ).toDateTime();
|
||||
maxDateHash[criteria] = cachedSettings.value( "maxtime" ).toDateTime();
|
||||
cachedSettings.endGroup();
|
||||
maxDateHash[criteria] = QDateTime::fromMSecsSinceEpoch( file.baseName().toLongLong() );
|
||||
m_dataCache[type] = dataHash;
|
||||
m_insertTimeCache[type] = insertDateHash;
|
||||
m_maxTimeCache[type] = maxDateHash;
|
||||
}
|
||||
|
||||
m_cacheRemainingToLoad--;
|
||||
}
|
||||
|
||||
@ -208,15 +217,17 @@ InfoSystemCache::saveCache( Tomahawk::InfoSystem::InfoType type, const QString &
|
||||
}
|
||||
}
|
||||
|
||||
QSettings cachedSettings( QString( cacheDir + '/' + QString::number( (int)type ) ), QSettings::IniFormat );
|
||||
|
||||
int criteriaNumber = 0;
|
||||
|
||||
foreach( InfoCriteriaHash criteria, m_dataCache[type].keys() )
|
||||
foreach ( InfoCriteriaHash criteria, m_dirtySet[type].values() )
|
||||
{
|
||||
cachedSettings.beginGroup( "group_" + QString::number( criteriaNumber ) );
|
||||
cachedSettings.beginWriteArray( "criteria" );
|
||||
QString maxAge( QString::number( m_maxTimeCache[type][criteria].toMSecsSinceEpoch() ) );
|
||||
QCryptographicHash hash( QCryptographicHash::Md5 );
|
||||
foreach( QString key, criteria.keys() )
|
||||
hash.addData( key.toUtf8() );
|
||||
foreach( QString value, criteria.values() )
|
||||
hash.addData( value.toUtf8() );
|
||||
QSettings cachedSettings( QString( cacheDir + '/' + maxAge + '.' + hash.result() ), QSettings::IniFormat );
|
||||
QStringList keys = criteria.keys();
|
||||
cachedSettings.beginWriteArray( "criteria" );
|
||||
for( int i = 0; i < criteria.size(); i++ )
|
||||
{
|
||||
cachedSettings.setArrayIndex( i );
|
||||
@ -227,12 +238,9 @@ InfoSystemCache::saveCache( Tomahawk::InfoSystem::InfoType type, const QString &
|
||||
cachedSettings.endArray();
|
||||
cachedSettings.setValue( "data", m_dataCache[type][criteria] );
|
||||
cachedSettings.setValue( "inserttime", m_insertTimeCache[type][criteria] );
|
||||
cachedSettings.setValue( "maxtime", m_maxTimeCache[type][criteria] );
|
||||
cachedSettings.endGroup();
|
||||
++criteriaNumber;
|
||||
m_dirtySet[type].remove( criteria );
|
||||
}
|
||||
|
||||
m_dirtySet.remove( type );
|
||||
}
|
||||
|
||||
|
||||
|
@ -51,14 +51,14 @@ public slots:
|
||||
|
||||
private slots:
|
||||
void syncTimerFired();
|
||||
void loadCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheFile );
|
||||
void loadCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheDir );
|
||||
void saveCache( Tomahawk::InfoSystem::InfoType type, const QString &cacheDir );
|
||||
|
||||
private:
|
||||
QHash< InfoType, QHash< InfoCriteriaHash, QVariant > > m_dataCache;
|
||||
QHash< InfoType, QHash< InfoCriteriaHash, QDateTime > > m_insertTimeCache;
|
||||
QHash< InfoType, QHash< InfoCriteriaHash, QDateTime > > m_maxTimeCache;
|
||||
QSet< InfoType > m_dirtySet;
|
||||
QHash< InfoType, QSet< InfoCriteriaHash > > m_dirtySet;
|
||||
QTimer m_syncTimer;
|
||||
int m_cacheRemainingToLoad;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user