mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-06 14:16:32 +02:00
Add a cache versioning system, and invalidate the cache in the upgrade
to version 1
This commit is contained in:
@@ -102,11 +102,13 @@ enum InfoType { // as items are saved in cache, mark them here to not change the
|
|||||||
InfoNowResumed = 82,
|
InfoNowResumed = 82,
|
||||||
InfoNowStopped = 83,
|
InfoNowStopped = 83,
|
||||||
|
|
||||||
InfoNoInfo = 90,
|
|
||||||
InfoLove = 91,
|
InfoLove = 90,
|
||||||
InfoUnLove = 92,
|
InfoUnLove = 91,
|
||||||
|
|
||||||
InfoNotifyUser = 100
|
InfoNotifyUser = 100,
|
||||||
|
|
||||||
|
InfoNoInfo = 101 //WARNING: *ALWAYS* keep this last!
|
||||||
};
|
};
|
||||||
|
|
||||||
struct InfoRequestData {
|
struct InfoRequestData {
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
|
|
||||||
#include "infosystemcache.h"
|
#include "infosystemcache.h"
|
||||||
|
#include "tomahawksettings.h"
|
||||||
|
|
||||||
|
|
||||||
namespace Tomahawk
|
namespace Tomahawk
|
||||||
@@ -35,9 +36,26 @@ namespace InfoSystem
|
|||||||
InfoSystemCache::InfoSystemCache( QObject* parent )
|
InfoSystemCache::InfoSystemCache( QObject* parent )
|
||||||
: QObject( parent )
|
: QObject( parent )
|
||||||
, m_cacheBaseDir( QDesktopServices::storageLocation( QDesktopServices::CacheLocation ) + "/InfoSystemCache/" )
|
, m_cacheBaseDir( QDesktopServices::storageLocation( QDesktopServices::CacheLocation ) + "/InfoSystemCache/" )
|
||||||
|
, m_cacheVersion( 1 )
|
||||||
{
|
{
|
||||||
qDebug() << Q_FUNC_INFO;
|
qDebug() << Q_FUNC_INFO;
|
||||||
|
|
||||||
|
TomahawkSettings *s = TomahawkSettings::instance();
|
||||||
|
if( s->infoSystemCacheVersion() != m_cacheVersion )
|
||||||
|
{
|
||||||
|
qDebug() << "Cache version outdated, old:" << s->infoSystemCacheVersion()
|
||||||
|
<< "new:" << m_cacheVersion
|
||||||
|
<< "Doing upgrade, if any...";
|
||||||
|
|
||||||
|
uint current = s->infoSystemCacheVersion();
|
||||||
|
while( current < m_cacheVersion )
|
||||||
|
{
|
||||||
|
doUpgrade( current, current + 1 );
|
||||||
|
current++;
|
||||||
|
}
|
||||||
|
s->setInfoSystemCacheVersion( m_cacheVersion );
|
||||||
|
}
|
||||||
|
|
||||||
m_pruneTimer.setInterval( 300000 );
|
m_pruneTimer.setInterval( 300000 );
|
||||||
m_pruneTimer.setSingleShot( false );
|
m_pruneTimer.setSingleShot( false );
|
||||||
connect( &m_pruneTimer, SIGNAL( timeout() ), SLOT( pruneTimerFired() ) );
|
connect( &m_pruneTimer, SIGNAL( timeout() ), SLOT( pruneTimerFired() ) );
|
||||||
@@ -50,6 +68,29 @@ InfoSystemCache::~InfoSystemCache()
|
|||||||
qDebug() << Q_FUNC_INFO;
|
qDebug() << Q_FUNC_INFO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
InfoSystemCache::doUpgrade( uint oldVersion, uint newVersion )
|
||||||
|
{
|
||||||
|
Q_UNUSED( newVersion );
|
||||||
|
qDebug() << Q_FUNC_INFO;
|
||||||
|
if ( oldVersion == 0 )
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO << "Wiping cache";
|
||||||
|
|
||||||
|
for ( int i = 0; i <= InfoNoInfo; i++ )
|
||||||
|
{
|
||||||
|
InfoType type = (InfoType)(i);
|
||||||
|
const QString cacheDirName = m_cacheBaseDir + QString::number( (int)type );
|
||||||
|
QFileInfoList fileList = QDir( cacheDirName ).entryInfoList( QDir::Files | QDir::NoDotAndDotDot );
|
||||||
|
foreach ( QFileInfo file, fileList )
|
||||||
|
{
|
||||||
|
if ( !QFile::remove( file.canonicalFilePath() ) )
|
||||||
|
qDebug() << "During upgrade, failed to remove cache file " << file.canonicalFilePath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
InfoSystemCache::pruneTimerFired()
|
InfoSystemCache::pruneTimerFired()
|
||||||
@@ -94,6 +135,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
|
|||||||
if ( !fileLocationHash.isEmpty() )
|
if ( !fileLocationHash.isEmpty() )
|
||||||
{
|
{
|
||||||
//We already know of some values, so no need to re-read the directory again as it's already happened
|
//We already know of some values, so no need to re-read the directory again as it's already happened
|
||||||
|
qDebug() << Q_FUNC_INFO << " notInCache -- filelocationhash empty";
|
||||||
emit notInCache( requestId, criteria, requestData );
|
emit notInCache( requestId, criteria, requestData );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -103,6 +145,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
|
|||||||
if ( !dir.exists() )
|
if ( !dir.exists() )
|
||||||
{
|
{
|
||||||
//Dir doesn't exist so clearly not in cache
|
//Dir doesn't exist so clearly not in cache
|
||||||
|
qDebug() << Q_FUNC_INFO << " notInCache -- dir doesn't exist";
|
||||||
emit notInCache( requestId, criteria, requestData );
|
emit notInCache( requestId, criteria, requestData );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -118,7 +161,8 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
|
|||||||
m_fileLocationCache[ requestData.type ] = fileLocationHash;
|
m_fileLocationCache[ requestData.type ] = fileLocationHash;
|
||||||
if ( !fileLocationHash.contains( criteriaHashVal ) )
|
if ( !fileLocationHash.contains( criteriaHashVal ) )
|
||||||
{
|
{
|
||||||
//Still didn't fine it? It's really not in the cache then
|
//Still didn't find it? It's really not in the cache then
|
||||||
|
qDebug() << Q_FUNC_INFO << " notInCache -- filelocationhash doesn't contain criteria val";
|
||||||
emit notInCache( requestId, criteria, requestData );
|
emit notInCache( requestId, criteria, requestData );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -137,7 +181,8 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
|
|||||||
fileLocationHash.remove( criteriaHashVal );
|
fileLocationHash.remove( criteriaHashVal );
|
||||||
m_fileLocationCache[ requestData.type ] = fileLocationHash;
|
m_fileLocationCache[ requestData.type ] = fileLocationHash;
|
||||||
m_dataCache.remove( criteriaHashVal );
|
m_dataCache.remove( criteriaHashVal );
|
||||||
|
|
||||||
|
qDebug() << Q_FUNC_INFO << " notInCache -- file was stale";
|
||||||
emit notInCache( requestId, criteria, requestData );
|
emit notInCache( requestId, criteria, requestData );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -147,7 +192,7 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
|
|||||||
|
|
||||||
if ( !QFile::rename( file.canonicalFilePath(), newFilePath ) )
|
if ( !QFile::rename( file.canonicalFilePath(), newFilePath ) )
|
||||||
{
|
{
|
||||||
qDebug() << "Failed to move old cache file to new location!";
|
qDebug() << Q_FUNC_INFO << " notInCache -- failed to move old cache file to new location";
|
||||||
emit notInCache( requestId, criteria, requestData );
|
emit notInCache( requestId, criteria, requestData );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -161,12 +206,12 @@ InfoSystemCache::getCachedInfoSlot( uint requestId, Tomahawk::InfoSystem::InfoCr
|
|||||||
QSettings cachedSettings( fileLocationHash[ criteriaHashVal ], QSettings::IniFormat );
|
QSettings cachedSettings( fileLocationHash[ criteriaHashVal ], QSettings::IniFormat );
|
||||||
QVariant output = cachedSettings.value( "data" );
|
QVariant output = cachedSettings.value( "data" );
|
||||||
m_dataCache.insert( criteriaHashVal, new QVariant( output ) );
|
m_dataCache.insert( criteriaHashVal, new QVariant( output ) );
|
||||||
|
|
||||||
emit info( requestId, requestData, output );
|
emit info( requestId, requestData, output );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
emit info( requestId, requestData, QVariant( *(m_dataCache[criteriaHashVal]) ) );
|
emit info( requestId, requestData, QVariant( *( m_dataCache[ criteriaHashVal ] ) ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,18 +225,18 @@ InfoSystemCache::updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash criteri
|
|||||||
const QString cacheDir = m_cacheBaseDir + QString::number( (int)type );
|
const QString cacheDir = m_cacheBaseDir + QString::number( (int)type );
|
||||||
const QString settingsFilePath( cacheDir + '/' + criteriaHashVal + '.' + QString::number( QDateTime::currentMSecsSinceEpoch() + maxAge ) );
|
const QString settingsFilePath( cacheDir + '/' + criteriaHashVal + '.' + QString::number( QDateTime::currentMSecsSinceEpoch() + maxAge ) );
|
||||||
|
|
||||||
QHash< QString, QString > fileLocationHash = m_fileLocationCache[type];
|
QHash< QString, QString > fileLocationHash = m_fileLocationCache[ type ];
|
||||||
if ( fileLocationHash.contains( criteriaHashVal ) )
|
if ( fileLocationHash.contains( criteriaHashVal ) )
|
||||||
{
|
{
|
||||||
if ( !QFile::rename( fileLocationHash[criteriaHashVal], settingsFilePath ) )
|
if ( !QFile::rename( fileLocationHash[ criteriaHashVal ], settingsFilePath ) )
|
||||||
{
|
{
|
||||||
qDebug() << "Failed to move old cache file to new location!";
|
qDebug() << "Failed to move old cache file to new location!";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fileLocationHash[criteriaHashVal] = settingsFilePath;
|
fileLocationHash[ criteriaHashVal ] = settingsFilePath;
|
||||||
m_fileLocationCache[type] = fileLocationHash;
|
m_fileLocationCache[ type ] = fileLocationHash;
|
||||||
|
|
||||||
QSettings cachedSettings( fileLocationHash[criteriaHashVal], QSettings::IniFormat );
|
QSettings cachedSettings( fileLocationHash[ criteriaHashVal ], QSettings::IniFormat );
|
||||||
cachedSettings.setValue( "data", output );
|
cachedSettings.setValue( "data", output );
|
||||||
|
|
||||||
m_dataCache.insert( criteriaHashVal, new QVariant( output ) );
|
m_dataCache.insert( criteriaHashVal, new QVariant( output ) );
|
||||||
|
@@ -54,12 +54,15 @@ private slots:
|
|||||||
void pruneTimerFired();
|
void pruneTimerFired();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void doUpgrade( uint oldVersion, uint newVersion );
|
||||||
const QString criteriaMd5( const Tomahawk::InfoSystem::InfoCriteriaHash &criteria ) const;
|
const QString criteriaMd5( const Tomahawk::InfoSystem::InfoCriteriaHash &criteria ) const;
|
||||||
|
|
||||||
QString m_cacheBaseDir;
|
QString m_cacheBaseDir;
|
||||||
QHash< InfoType, QHash< QString, QString > > m_fileLocationCache;
|
QHash< InfoType, QHash< QString, QString > > m_fileLocationCache;
|
||||||
QTimer m_pruneTimer;
|
QTimer m_pruneTimer;
|
||||||
QCache< QString, QVariant > m_dataCache;
|
QCache< QString, QVariant > m_dataCache;
|
||||||
|
|
||||||
|
uint m_cacheVersion;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace InfoSystem
|
} //namespace InfoSystem
|
||||||
|
@@ -147,6 +147,34 @@ TomahawkSettings::doUpgrade( int oldVersion, int newVersion )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
TomahawkSettings::setAcceptedLegalWarning( bool accept )
|
||||||
|
{
|
||||||
|
setValue( "acceptedLegalWarning", accept );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
TomahawkSettings::acceptedLegalWarning() const
|
||||||
|
{
|
||||||
|
return value( "acceptedLegalWarning", false ).toBool();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
TomahawkSettings::setInfoSystemCacheVersion( uint version )
|
||||||
|
{
|
||||||
|
setValue( "infosystemcacheversion", version );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint
|
||||||
|
TomahawkSettings::infoSystemCacheVersion() const
|
||||||
|
{
|
||||||
|
return value( "infosystemcacheversion", 0 ).toUInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QStringList
|
QStringList
|
||||||
TomahawkSettings::scannerPaths()
|
TomahawkSettings::scannerPaths()
|
||||||
{
|
{
|
||||||
@@ -238,20 +266,6 @@ TomahawkSettings::setWatchForChanges( bool watch )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
TomahawkSettings::setAcceptedLegalWarning( bool accept )
|
|
||||||
{
|
|
||||||
setValue( "acceptedLegalWarning", accept );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
|
||||||
TomahawkSettings::acceptedLegalWarning() const
|
|
||||||
{
|
|
||||||
return value( "acceptedLegalWarning", false ).toBool();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
TomahawkSettings::httpEnabled() const
|
TomahawkSettings::httpEnabled() const
|
||||||
{
|
{
|
||||||
|
@@ -49,6 +49,8 @@ public:
|
|||||||
void setScannerMode( ScannerMode mode );
|
void setScannerMode( ScannerMode mode );
|
||||||
uint scannerTime() const;
|
uint scannerTime() const;
|
||||||
void setScannerTime( uint time );
|
void setScannerTime( uint time );
|
||||||
|
uint infoSystemCacheVersion() const;
|
||||||
|
void setInfoSystemCacheVersion( uint version );
|
||||||
|
|
||||||
bool watchForChanges() const;
|
bool watchForChanges() const;
|
||||||
void setWatchForChanges( bool watch );
|
void setWatchForChanges( bool watch );
|
||||||
|
Reference in New Issue
Block a user