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